MybatisPlus简单CRUD

四、简单CRUD

  1. 查询

      //根据id批量查询
        @Test
        void testSelectByBatchId(){
            List<User> users = userMapper.selectBatchIds(Arrays.asList(1,2,3));
            users.forEach(System.out::println);
        }
    	//根据条件查询
        @Test
        void testSelectByMap(){
            Map map = new HashMap();
            map.put("name","jx");
            userMapper.selectByMap(map);
        }
    	//分页查询
        @Test
        void testPage(){
            Page<User> userPage = new Page<>(1,5);//查询页、每页记录数
            userMapper.selectPage(userPage,null);
        }
    
     //分页组件
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            return paginationInterceptor;
        }
    
  2. 删除

      //根据id批量删除
        @Test
        void testDeleteByIds(){
            userMapper.deleteBatchIds(Arrays.asList(1,2,3));
        }
        //条件删除
        @Test
        void testDeleteByMap(){
            Map map = new HashMap();
            map.put("name","jx");
            userMapper.deleteByMap(map);
        }
        //逻辑删除
        @Test
        void testDeleteLogic(){
            Map map = new HashMap();
            map.put("name","jx");
           userMapper.deleteByMap(map);
        }
    
      //逻辑删除组件
        @Bean
        public ISqlInjector sqlInjector(){
            return new LogicSqlInjector();
        }
    
    #配置逻辑删除参数
    mybatis-plus.global-config.db-config.logic-delete-value=1
    mybatis-plus.global-config.db-config.logic-not-delete-value=0
    
  3. 插入

     //测试插入
      @Test
      void testInsert(){
          User user = new User();//自动生成id
          user.setName("jx");
          user.setAge(22);
          user.setEmail("12345@qq.com");
          userMapper.insert(user);
      }
    
  4. 更新

      @Test
        void testUpdate(){
            User user = new User();
            user.setId(1l);
            user.setName("jx");
            user.setAge(20);
            userMapper.updateById(user);
        }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值