public void testUpdateById() { User user = new User(); user.setId(1L); //条件,根据id更新 user.setAge(19); //更新的字段 user.setPassword("666666"); int result = this.userMapper.updateById(user); }
public void testUpdate() { User user = new User(); user.setAge(20); //更新的字段 user.setPassword("8888888"); QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("user_name", "zhangsan"); //匹配user_name = zhangsan 的用户数据 //根据条件做更新 int result = this.userMapper.update(user, wrapper); }
public void testUpdate2() { UpdateWrapper<User> wrapper = new UpdateWrapper<>(); wrapper.set("age", 21).set("password", "999999") //更新的字段 .eq("user_name", "zhangsan"); //更新的条件 //根据条件做更新 int result = this.userMapper.update(null, wrapper); }
删除
根据id删除
1 2 3 4
public void testDeleteById(){ // 根据id删除数据 int result = this.userMapper.deleteById(2L); }
根据条件删除
1 2 3 4 5 6 7 8 9
public void testDeleteByMap(){ Map<String,Object> map = new HashMap<>(); map.put("user_name", "zhangsan"); map.put("password", "999999"); // 根据map删除数据,多条件之间是and关系 int result = this.userMapper.deleteByMap(map); }
根据包装条件删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public void testDelete(){ //用法一: QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("user_name", "caocao1") .eq("password", "123456"); //用法二: User user = new User(); user.setPassword("123456"); user.setUserName("caocao"); QueryWrapper<User> wrapper = new QueryWrapper<>(user); // 根据包装条件做删除 int result = this.userMapper.delete(wrapper); }
根据id批量删除数据
1 2 3 4
public void testDeleteBatchIds(){ // 根据id批量删除数据 int result = this.userMapper.deleteBatchIds(Arrays.asList(10L, 11L)); }
查询
根据id查询
1 2 3 4
public void testSelectById() { User user = this.userMapper.selectById(2L); System.out.println(user); }
根据id批量查询
1 2 3 4 5 6 7
public void testSelectBatchIds(){ // 根据id批量查询数据 List<User> users = this.userMapper.selectBatchIds(Arrays.asList(2L, 3L, 4L, 100L)); for (User user : users) { System.out.println(user); } }
查询一条数据
1 2 3 4 5 6 7 8
public void testSelectOne(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); //查询条件 wrapper.eq("password", "123456"); // 查询的数据超过一条时,会抛出异常 User user = this.userMapper.selectOne(wrapper); }
public void testSelectList(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); //设置查询条件 wrapper.like("email", "itcast"); List<User> users = this.userMapper.selectList(wrapper); for (User user : users) { System.out.println(user); } }
分页查询
MybatisPlusConfig.java
1 2 3 4 5 6 7 8 9 10 11
@Configuration @MapperScan("cn.itcast.mp.mapper") //设置mapper接口的扫描包 public class MybatisPlusConfig { @Bean //配置分页插件 public PaginationInterceptor paginationInterceptor(){ return new PaginationInterceptor(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public void testSelectPage(){ Page<User> page = new Page<>(3,1); //查询第3页,查询1条数据 QueryWrapper<User> wrapper = new QueryWrapper<>(); //设置查询条件 wrapper.like("email", "itcast"); IPage<User> iPage = this.userMapper.selectPage(page, wrapper); System.out.println("数据总条数: " + iPage.getTotal()); System.out.println("数据总页数: " + iPage.getPages()); System.out.println("当前页数: " + iPage.getCurrent()); List<User> records = iPage.getRecords(); for (User record : records) { System.out.println(record); } }