Mybatis-plus

LambdaQueryWrapper的操作

等于条件

// 查询username等于"John"的记录
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getUsername, "John");
List userList = userService.list(queryWrapper);

不等于条件

// 查询年龄不等于18的记录
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.ne(User::getAge, 18);
List userList = userService.list(queryWrapper);

模糊查询条件

// 查询username包含"John"的记录
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(User::getUsername, "John");
List userList = userService.list(queryWrapper);

范围查询条件

// 查询年龄在18到30之间的记录
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.between(User::getAge, 18, 30);
List userList = userService.list(queryWrapper);

排序条件

// 按年龄降序查询记录
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(User::getAge);
List userList = userService.list(queryWrapper);

分组条件

// 按部门进行分组查询记录
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.groupBy(User::getDepartment);
List userList = userService.list(queryWrapper);

MyBatis Plus的Service层操作

查询操作

// 查询所有数据
List userList = userService.list();

// 根据条件查询单个数据
User user = userService.getOne(new QueryWrapper().eq("username", "John"));

// 分页查询数据
IPage userPage = userService.page(new Page<>(1, 10));

插入操作

// 插入单个数据
User user = new User();
user.setUsername("John");
user.setPassword("123456");
userService.save(user);

// 插入多个数据
List userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
userService.saveBatch(userList);

更新操作

// 更新单个数据
User user = userService.getById(1L);
user.setPassword("654321");
userService.updateById(user);

// 根据条件更新数据
userService.update(new UpdateWrapper().set("password", "654321").eq("id", 1L));

删除操作

// 根据ID删除数据
userService.removeById(1L);

// 根据条件删除数据
userService.remove(new QueryWrapper().eq("username", "John"));

你可能感兴趣的:(mybatis,java,开发语言)