You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.9 KiB
104 lines
2.9 KiB
package com.unicom.test.service;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.unicom.test.dao.UserMapper;
|
|
import com.unicom.test.entity.User;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.List;
|
|
|
|
@Service
|
|
public class UserService extends ServiceImpl<UserMapper, User> {
|
|
|
|
@Autowired
|
|
private UserMapper userMapper;
|
|
|
|
// 插入用户
|
|
public boolean insertUser(User user) {
|
|
try {
|
|
int rows = userMapper.insert(user);
|
|
return rows > 0;
|
|
} catch (Exception e) {
|
|
// 可以添加日志记录异常信息
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 根据 ID 查询用户
|
|
public User getUserById(Long id) {
|
|
try {
|
|
return userMapper.selectById(id);
|
|
} catch (Exception e) {
|
|
// 可以添加日志记录异常信息
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 查询所有用户
|
|
public List<User> getAllUsers() {
|
|
try {
|
|
return userMapper.selectList(null);
|
|
} catch (Exception e) {
|
|
// 可以添加日志记录异常信息
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 根据条件查询用户
|
|
public List<User> getUsersByCondition(String name) {
|
|
try {
|
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
|
if (name!= null) {
|
|
queryWrapper.like("name", name);
|
|
}
|
|
return userMapper.selectList(queryWrapper);
|
|
} catch (Exception e) {
|
|
// 可以添加日志记录异常信息
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 分页查询用户
|
|
public IPage<User> getUsersByPage(int pageNum, int pageSize) {
|
|
try {
|
|
Page<User> page = new Page<>(pageNum, pageSize);
|
|
return userMapper.selectPage(page, null);
|
|
} catch (Exception e) {
|
|
// 可以添加日志记录异常信息
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 更新用户信息
|
|
public boolean updateUser(User user) {
|
|
try {
|
|
int rows = userMapper.updateById(user);
|
|
return rows > 0;
|
|
} catch (Exception e) {
|
|
// 可以添加日志记录异常信息
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 删除用户
|
|
public boolean deleteUser(Long id) {
|
|
try {
|
|
int rows = userMapper.deleteById(id);
|
|
return rows > 0;
|
|
} catch (Exception e) {
|
|
// 可以添加日志记录异常信息
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|