MP的增删改查基本操作

0.接口结构图

 

1.MP的增加操作

 @Test
    public void insert(){
        User user = new User();
        user.setName("向中");
        user.setAge(23);
        user.setEmail("[email protected]");
        user.setManagerId(1088248166370832385L);
        user.setCreateTime(LocalDateTime.now());
        //返回影响记录数
        int rows = userMapper.insert(user);
        System.out.println("影响记录数:"+rows);
    }

2.MP的查询操作

(1)selectById 根据 ID 查询

  @Test
    public void selectById(){
        User user = userMapper.selectById(1088248166370832385L);
        System.out.println(user);
    }

(2)selectBatchIds 查询(根据ID 批量查询)

  @Test
    public void selectByIds(){
        List asList = Arrays.asList(1088250446457389058L, 1094592041087729666L, 1186555441707827201L);
        List userList = userMapper.selectBatchIds(asList);
        userList.forEach(System.out::println);
    }

 (3)selectByMap 查询(根据 columnMap 条件)

    @Test
    public void selectByMap(){
        //map.put("name","王天风")
        //map.put("age",25)
        //where name = "王天风" and age=30
        Map columnMap = new HashMap<>();
        columnMap.put("name","王天风");
        columnMap.put("age",25);
        List userList = userMapper.selectByMap(columnMap);
        userList.forEach(System.out::println);
    }

(4)LambdaQueryWrapper

   @Test
     void TestLambdaQueryWrapper() {
        //1、查询单条
         LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(User::getName,"liangd1");
         User selectOne = userMapper.selectOne(queryWrapper);
        System.out.println(selectOne);
 
         //2、查询list以及统计条数
         queryWrapper.eq(User::getName, "zcx");
        List userList = userMapper.selectList(queryWrapper);
         System.out.println("userList:" + userList);
         Integer result = userMapper.selectCount(queryWrapper);
         System.out.println("result:" + result);
     }
    

(5)LambdaQueryChainWrapper 

     @Test
    void TestLambdaQueryChainWrapper() {
         //1、eq查询单条
         User one = new LambdaQueryChainWrapper<>(userMapper)
                 .eq(User::getName, "liangd1")
                 .one();
         System.out.println("UserOne:" + one);
 
         //2、查询list
         List users = new LambdaQueryChainWrapper<>(userMapper)
                .eq(User::getName, "zcx")
                 .list();
        System.out.println("UserList:" + users);
 
         //3、模糊查询
         List LikeList = new LambdaQueryChainWrapper<>(userMapper)
                 .like(User::getEmail, "test")
                 .list();
         System.out.println("LikeUser:" + LikeList);
     }

3.MP的更新操作

(1)updateById 根据主键id更新

  @Test
    public void updateById(){
      User user = new User();
      user.setId(1088248166370832385L);
      user.setAge(30);
      user.setEmail("[email protected]");
      int rows = userMapper.updateById(user);
      System.out.println("影响记录数:"+ rows);
    }

(2)UpdateWrapper 更新,多条属性操作
1.多条属性更新

   @Test
    public void update(){
      //  User whereUser = new User();
      //  whereUser.setName("大boss");
      //  UpdateWrapper updateWrapper = new UpdateWrapper<>(whereUser);
      //  updateWrapper.eq("age",41);
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name","大boss").eq("age",41);
        User user = new User();
        user.setAge(41);
        user.setEmail("[email protected]");
        int rows = userMapper.update(user, updateWrapper);
        System.out.println("影响记录数:" + rows);
    }

2.少数属性更新,加上set

  @Test
    public void update2(){
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name","大boss").eq("age",41).set("age",42);
        int rows = userMapper.update(null, updateWrapper);
        System.out.println("影响记录数:" + rows);
    }

(3)LambdaUpdateWrapper 更新操作,可以防止sql注入
1.lambdaUpdateWrapper 更新

  @Test
    public void updateByLambdaWrapper(){
        LambdaUpdateWrapper lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(User::getName,"大boss").eq(User::getAge,42).set(User::getAge,43);
        int update = userMapper.update(null, lambdaUpdateWrapper);
        System.out.println(update);
    }

2.LambdaUpdateChainWrapper 链式更新

 @Test
    public void updateByLambdaWrapperChain(){
        boolean update = new LambdaUpdateChainWrapper(userMapper)
                .eq(User::getName, "大boss").eq(User::getAge, 43).set(User::getAge, 44).update();
        System.out.println(update);
    }

4.MP的删除操作

(1)deleteById

 @Test
    public void deleteById() {
        int delete = userMapper.deleteById(1186569110046949378L);
        System.out.println(delete);
    }

(2)deleteByMap

  @Test
    public void deleteByMap() {
        Map map = new HashMap<>();
        map.put("name","向前");
        map.put("age",23);
        int deleteByMap = userMapper.deleteByMap(map);
        System.out.println(deleteByMap);
    }

(3)deleteBatchIds

  @Test
    public void deleteByBatchIds() {
        List list = Arrays.asList(1186568191955804162L, 1186561629153402882L);
        int ids = userMapper.deleteBatchIds(list);
        System.out.println(ids);
    }

(4)deleteByLambdaQueryWrapper

 @Test
    public void deleteByWrappersLamdba(){
        LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(User::getAge,33).eq(User::getEmail,"[email protected]");
        int delete = userMapper.delete(lambdaQueryWrapper);
        System.out.println("删除记录:"+delete);
    }

(5)deleteByQueryWrapper

    @Test
    public void deleteByWrapper2(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("age",23).eq("name","向中");
        int delete = userMapper.delete(queryWrapper);
        System.out.println("删除记录数:"+delete);
    }

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