-
背景
在做项目时,用到mybatisPlus,好用到哭,所以索性记录一下 -
mybatisPlus查询妙用
代码奉上
//查询所有
List<User> list = userMapper.selectList(null);
System.out.println(list);
//多个id批量查询
List<User> list = userMapper.selectBatchIds(Arrays.asList(1,2,3));
System.out.println(list);
//简单条件查询
HashMap<String, Object> map = new HashMap<>();
map.put("name","牛马");
List<User> list = userMapper.selectByMap(map);
System.out.println(list);
//根据对象某个字段查询返回对象
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("phone",phone);
User user = userMapper.selectOne(wrapper);
System.out.println(user);
//最大最小值
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.select("max(id) as id");
User user = mapper.selectOne(wrapper);
//查询时自定义sql条件 SELECT * FROM `sys_user` WHERE FIND_IN_SET(6, post)
LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper();
sysUserLambdaQueryWrapper.apply( identity != null,"FIND_IN_SET ( '"+ identity +"',post )");
List<SysUser> list = sysUserService.getBaseMapper().selectList(sysUserLambdaQueryWrapper);
//批量添加
List<User> entityList = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
User user = new User();
user.setName("张三" + i);
user.setAge(20 + i);
entityList.add(user);
}
return userService.saveBatch(entityList);
//批量修改
List<User> entityList = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
User user = new User();
user.setId(i + 1);
user.setName("拜月" + i);
user.setAge(30 + i);
entityList.add(user);
}
return userService.updateBatchById(entityList);
//批量删除
List<User> entityList = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
User user = new User();
user.setId(i + 1);
entityList.add(user);
}
return userService.removeBatchByIds(entityList);
/**这个注解表示不走具体数据库的表*/
@TableField(exist = false)
private String xx;
如果发现博文有问题,欢迎各位老鸟多多指点一二