mybatis之动态SQL

一、if标签

  • if标签中的set属性是必须的。
  • if标签中的test属性如果是true,SQL语句就会拼接。反之则不拼接
  • 在mybatis中的动态SQL语句当中,不能使用&&,只能使用and、

【CarMapper.xml样例】

【测试样例】:

@Test
    public void testSelectByMultiCondition() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
        List cars = mapper.selectByMultiCondition("比亚迪", 2.0, "新能源");
        cars.forEach(car -> System.out.println(car));
        sqlSession.close();
    }

二、where标签

      【作用】:让where标签更加动态智能。

  • 所有条件都为空时,where标签保证不会生成where语句
  • 自动某些条件前面(注意条件后面跟的and是去不掉的多余的and或or

【CarMapper.xml样例】:

【测试样例】:

 @Test
    public void testSelectByMultiConditionWithWhere() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
        //List cars = mapper.selectByMultiConditionWithWhere("比亚迪", 2.0, "新能源");
        List cars = mapper.selectByMultiConditionWithWhere("", null, "");
        cars.forEach(car -> System.out.println(car));
        sqlSession.close();
    }

三、trim标签

  • prefix:在trim标签中的语句前面添加内容
  • suffix:    在trim标签中的语句后面添加内容
  • prefixOverrides:前缀覆盖掉(去除)
  • suffixOverrides:后缀覆盖掉(去除)

【CarMapper.xml样例】:

 

【测试样例】:

@Test
    public void testselectByMultiConditionWithTrim() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
        //List cars = mapper.selectByMultiConditionWithWhere("比亚迪", 2.0, "新能源");
        List cars = mapper.selectByMultiConditionWithTrim("比亚迪", null, "");
        cars.forEach(car -> System.out.println(car));
        sqlSession.close();
    }

四、set标签

        【作用】:主要使用在update语句当中,用来生成set关键字,同时去掉最后多余的" ,",就是说只更新我们提交的不为空的数据

【CarMapper.xml样例】


        update t_car
        
            
                car_num=#{carNum},
            
            
                brand=#{brand},
            
            
                guide_price=#{guidePrice},
            
            
                produce_time=#{produceTime},
            
            
                car_type=#{carType},
            
        
        where id=#{id}

【测试样例】:

 public void testUpdateBySet() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
        Car car = new Car(19L, null, "丰田霸道", null, null, "燃油车");
        int i = mapper.updateBySet(car);
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();
    }

五、choose标签

【注意】:choose when otherwise三个标签是在一起的


        
        
        
        
    
if () {

        } else if () {

        } else if () {

        } else if () {
          
        } else {

        }

只有一个条件会被选择

【需求示范】:先根据品牌查询,如果没有提供品牌,再根据指导价格查询,如果没有提供指导价格,就根据知道日期查询。

【CarMapper.xml样例】:

 

【测试样例】:

 @Test
    public void testSelectByChoose() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
        List cars = mapper.selectByChoose("东风", null, null);
        cars.forEach(car -> System.out.println(car));
        sqlSession.close();
    }

六、foreach标签

       foreach标签属性:

  • collection:指定数组或集合
  • item:代表数组或集合中的元素
  • separator:循环之间的分隔符
  • open:foreach循环拼接的SQL语句的最前面以什么开始
  • close:foreach循环拼接的SQL语句的最前面以什么结束
①批量删除

【CarMapper.xml样例】:


        delete from t_car where id in(
        
        
            #{id}
        
        
        )
    

【测试样例】:

 @Test
    public void testDeleteById() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
        Long[] ids={12L,9L};
        int i = mapper.deleteByIds(ids);
        System.out.println("i = " + i);
    }
②批量插入

【CarMapper.xml样例】:


        insert  into t_car values
        
             (NULL,
             #{car.carNum},
             #{car.brand},
             #{car.guidePrice},
             #{car.produceTime},
             #{car.carType})
        
 

【测试样例】:

 @Test
    public void testInsertBatch() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        com.mapper.CarMapper mapper = sqlSession.getMapper(com.mapper.CarMapper.class);
        List cars=new ArrayList<>();
        Car car1 = new Car(null,"1211","帕萨特",23.0,"2022-10-22","燃油车");
        Car car2 = new Car(null,"1212","梅赛德斯",21.0,"2022-11-23","燃油车");
        cars.add(car1);
        cars.add(car2);
        int i = mapper.insertBatch(cars);
        System.out.println("i = " + i);
    }

你可能感兴趣的:(mybatis,sql,数据库)