mybatis-plus的crud
2021-12-02 本文已影响0人
virtual灬zzZ
@GetMapping("/page")
public IPage<User> page(Integer current ,Integer size, User user) {
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper
.like(StringUtils.isNotEmpty(user.getAccount()), User::getAccount, user.getAccount())
.like(StringUtils.isNotEmpty(user.getName()), User::getName, user.getName())
.like(StringUtils.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail())
.like(StringUtils.isNotEmpty(user.getTel()), User::getTel, user.getTel())
.like(Objects.nonNull(user.getId()), User::getId, user.getId())
.ge(Objects.nonNull(user.getGeBirthday()), User::getBirthday, user.getGeBirthday())
.le(Objects.nonNull(user.getLeBirthday()), User::getBirthday, user.getLeBirthday())
.eq(Objects.nonNull(user.getStatus()), User::getStatus, user.getStatus());
IPage<User> page = new Page<>(current, size);
return userService.page(page, wrapper);
}
@GetMapping("/list")
public List<User> list(User user) {
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper
.like(StringUtils.isNotEmpty(user.getAccount()), User::getAccount, user.getAccount())
.like(StringUtils.isNotEmpty(user.getName()), User::getName, user.getName())
.like(StringUtils.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail())
.like(StringUtils.isNotEmpty(user.getTel()), User::getTel, user.getTel())
.like(Objects.nonNull(user.getId()), User::getId, user.getId())
.ge(Objects.nonNull(user.getGeBirthday()), User::getBirthday, user.getGeBirthday())
.le(Objects.nonNull(user.getLeBirthday()), User::getBirthday, user.getLeBirthday())
.eq(Objects.nonNull(user.getStatus()), User::getStatus, user.getStatus());
return userService.list(wrapper);
}
@GetMapping("/save")
public boolean save(User entity) {
return userService.save(entity);
}
@GetMapping("/saveOrUpdate")
public boolean saveOrUpdate(User entity) {
return userService.saveOrUpdate(entity);
}
@GetMapping("/update")
public boolean update(User user) {
LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
wrapper
.set(StringUtils.isNotEmpty(user.getAccount()), User::getAccount, user.getAccount())
.set(StringUtils.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail())
.set(StringUtils.isNotEmpty(user.getTel()), User::getTel, user.getTel())
.set(Objects.nonNull(user.getStatus()), User::getStatus, user.getStatus())
.eq(User::getName,user.getName());
return userService.update(null, wrapper);
}
@GetMapping("/updateById")
public boolean updateById(User entity) {
return userService.updateById(entity);
}
@GetMapping("/getById")
public User getById(Integer id) {
return userService.getById(id);
}
@GetMapping("/getOne")
public User getOne(User user) {
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.select(User::getAccount, User::getId, User::getStatus);
wrapper
.like(StringUtils.isNotEmpty(user.getAccount()), User::getAccount, user.getAccount())
.like(StringUtils.isNotEmpty(user.getName()), User::getName, user.getName())
.like(StringUtils.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail())
.like(StringUtils.isNotEmpty(user.getTel()), User::getTel, user.getTel())
.like(Objects.nonNull(user.getId()), User::getId, user.getId())
.ge(Objects.nonNull(user.getGeBirthday()), User::getBirthday, user.getGeBirthday())
.le(Objects.nonNull(user.getLeBirthday()), User::getBirthday, user.getLeBirthday())
.eq(Objects.nonNull(user.getStatus()), User::getStatus, user.getStatus());
return userService.getOne(wrapper);
}
分页配置,有官网参考:
@Configuration
@MapperScan(value = "com.orion.dao")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
注意一定要设置id增长策略,不然save报错,id是一个很大的整数,可以在application文件中配置,以下是自增长:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: auto