123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package com.xin.shardingspherejdbcdemo.service.impl;
- import com.xin.shardingspherejdbcdemo.config.util.SnowFlake;
- import com.xin.shardingspherejdbcdemo.entity.User;
- import com.xin.shardingspherejdbcdemo.entity.UserExample;
- import com.xin.shardingspherejdbcdemo.entity.vo.PageVo;
- import com.xin.shardingspherejdbcdemo.mapper.UserMapper;
- import com.xin.shardingspherejdbcdemo.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.List;
- @Service
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserMapper userMapper;
- @Override
- public PageVo list(int pageNum, int pageSize, String name, Integer age) {
- UserExample example = new UserExample();
- int count = userMapper.countByExample(example);
- List<User> list = userMapper.selectByExample(example);
- return PageVo.data(list, count);
- }
- private void getUser(List<User> users, List<Long> list) {
- int listCount = list.size();
- int pageSize = 1000;//一页数量
- int pageNumer = listCount % pageSize == 0 ? listCount / pageSize : listCount / pageSize + 1;
- for (int i = 0; i < pageNumer; i++) {
- int start = i * pageSize;
- int end = start + pageSize;
- //防止索引越界
- if (i == (pageNumer - 1)) {
- end = list.size();
- }
- //实现分页
- List<Long> ids = list.subList(start, end);
- users.addAll(userMapper.selectAll(ids));
- //写业务逻辑传参pageList参数
- System.out.println("分页list长度:" + ids.size());
- }
- }
- @Override
- @Transactional
- public User save(User user) {
- SnowFlake snowFlake = new SnowFlake(2, 3);
- user.setId(snowFlake.nextId());
- userMapper.insert(user);
- return user;
- }
- @Override
- @Transactional
- public String saveList(List<User> list) {
- int listCount = list.size();
- int pageSize = 10000;//一页数量
- int pageNumer = listCount % pageSize == 0 ? listCount / pageSize : listCount / pageSize + 1;
- for (int i = 0; i < pageNumer; i++) {
- int start = i * pageSize;
- int end = start + pageSize;
- //防止索引越界
- if (i == (pageNumer - 1)) {
- end = list.size();
- }
- //实现分页
- List<User> users = list.subList(start, end);
- userMapper.insertAll(users);
- //写业务逻辑传参pageList参数
- System.out.println("分页list长度:" + users.size());
- }
- return "保存成功";
- }
- @Override
- public User info(Long id) {
- return userMapper.selectByPrimaryKey(id);
- }
- @Override
- @Transactional
- public User update(User user) {
- User userRe = userMapper.selectByPrimaryKey(user.getId());
- UserExample example = new UserExample();
- example.createCriteria().andIdEqualTo(userRe.getId()).andAgeEqualTo(userRe.getAge());
- int i = userMapper.updateByExampleSelective(user, example);
- return user;
- }
- @Override
- @Transactional
- public Long delete(Long id) {
- userMapper.deleteByPrimaryKey(id);
- return id;
- }
- }
|