mybatis-动态sql

动态sql

  • 1、if标签
  • 2、where标签
  • 3、trim标签
  • 4、set标签
  • [5、choose when otherwise](#5、choose when otherwise)
  • [6 、foreach](#6 、foreach)
    • [6.1 用in来删除](#6.1 用in来删除)
    • [6.2 用or来删除](#6.2 用or来删除)
    • [6.3 批量添加](#6.3 批量添加)
  • [7、 sql标签与include标签](#7、 sql标签与include标签)

1、if标签

需求:多条件查询。

可能的条件包括:品牌(brand)、指导价格(guide_price)、汽车类型(car_type)

java 复制代码
List<Car> selectCar(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);
xml 复制代码
 <select id="selectCar" resultType="car">
        select  * from t_car where
        <if test="brand != null and brand != ''">
            brand like "%"#{brand}"%"
        </if>
        <if test="guidePrice != null and guidePrice != ''">
           and guide_price >#{guidePrice}
        </if>
        <if test="carType != null and carType != ''">
            and  car_type=#{carType}
        </if>
  
    </select>
java 复制代码
 @Test
    public void testmanyParams(){
        SqlSession sqlSession = MybatisUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> cars = mapper.selectCar("丰田", 1.0, "氢能源");
        cars.forEach(car -> System.out.println(car));
        sqlSession.close();
    }

但是如果第一个为空,第二三个不为空呢?

我们可以看到 出现了错误,即前边多了一个and。

此时就需要有where 标签

2、where标签

where标签的作用:让where子句更加动态智能。

● 所有条件都为空时,where标签保证不会生成where子句。

● 自动去除某些条件前面多余的and或or。

继续使用if标签中的需求。

xml 复制代码
 <select id="selectCar" resultType="car">
        select  * from t_car
        <where>
        <if test="brand != null and brand != ''">
            brand like "%"#{brand}"%"
        </if>
        <if test="guidePrice != null and guidePrice != ''">
           and guide_price >#{guidePrice}
        </if>
        <if test="carType != null and carType != ''">
            and  car_type=#{carType}
        </if>
        </where>
    </select>

如果让第一个为空,此时不会出现错误,会自动帮我们去掉多余的and。

如果and 在后边,则无法去掉.只能去掉前边的and或者or

xml 复制代码
  <select id="selectCar" resultType="car">
        select  * from t_car
        <where>
        <if test="brand != null and brand != ''">
            brand like "%"#{brand}"%" and
        </if>
        <if test="guidePrice != null and guidePrice != ''">
           guide_price >#{guidePrice} and
        </if>
        <if test="carType != null and carType != ''">
             car_type=#{carType}
        </if>
        </where>
    </select>

3、trim标签

可以自动增加前缀,后缀,删除前缀,删除后缀。

举例

会自动帮助我们增加前缀 where 删除多余的后缀and

xml 复制代码
 <select id="selectCar" resultType="car">
        select  * from t_car
        <trim prefix="where" suffixOverrides="and">
            <if test="brand != null and brand != ''">
                brand like "%"#{brand}"%" and
            </if>
            <if test="guidePrice != null and guidePrice != ''">
                guide_price >#{guidePrice} and
            </if>
            <if test="carType != null and carType != ''">
                car_type=#{carType}
            </if>
        </trim>

    </select>
java 复制代码
  @Test
    public void testmanyParams(){
        SqlSession sqlSession = MybatisUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> cars = mapper.selectCar("丰田", null, "");
        cars.forEach(car -> System.out.println(car));
        sqlSession.close();
    }

4、set标签

主要使用在update语句当中,用来生成set关键字,同时去掉最后多余的","

比如我们只更新提交的不为空的字段,如果提交的数据是空或者"",那么这个字段我们将不更新。

java 复制代码
/**
* 更新信息,使用set标签
* @param car
* @return
*/
int updateWithSet(Car car);
xml 复制代码
<update id="updateWithSet">
  update t_car
  <set>
    <if test="carNum != null and carNum != ''">car_num = #{carNum},</if>
    <if test="brand != null and brand != ''">brand = #{brand},</if>
    <if test="guidePrice != null and guidePrice != ''">guide_price = #{guidePrice},</if>
    <if test="produceTime != null and produceTime != ''">produce_time = #{produceTime},</if>
    <if test="carType != null and carType != ''">car_type = #{carType},</if>
  </set>
  where id = #{id}
</update>
java 复制代码
  @Test
    public void testUpdateWithSet(){
        SqlSession sqlSession = MybatisUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(8,"1001","丰田霸道2",10.0,"",null);
        int count = mapper.updateWithSet(car);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
    }

5、choose when otherwise

这三个标签是在一起使用的:

xml 复制代码
<choose>
  <when></when>
  <when></when>
  <when></when>
  <otherwise></otherwise>
</choose>

等同于

xml 复制代码
if(){
    
}else if(){
    
}else if(){
    
}else if(){
    
}else{

}

只有一个分支会被选择!!!!

需求:先根据品牌查询,如果没有提供品牌,再根据指导价格查询,如果没有提供指导价格,就根据生产日期查询。

java 复制代码
List<Car> selectWithChoose(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("produceTime") String produceTime);
xml 复制代码
<select id="selectWithChoose" resultType="car">
  select * from t_car
  <where>
    <choose>
      <when test="brand != null and brand != ''">
        brand like #{brand}"%"
      </when>
      <when test="guidePrice != null and guidePrice != ''">
        guide_price >= #{guidePrice}
      </when>
      <otherwise>
        produce_time >= #{produceTime}
      </otherwise>
    </choose>
  </where>
</select>
java 复制代码
@Test
public void testSelectWithChoose(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    //List<Car> cars = mapper.selectWithChoose("丰田霸道", 20.0, "2000-10-10");
    //List<Car> cars = mapper.selectWithChoose("", 20.0, "2000-10-10");
    //List<Car> cars = mapper.selectWithChoose("", null, "2000-10-10");
    List<Car> cars = mapper.selectWithChoose("", null, "");
    System.out.println(cars);
}

只会执行一个分支!!一定会执行一个,如果前几个都没有对应上,就一定会执行最后一个。

6 、foreach

循环数组或集合,动态生成sql,比如这样的SQL:

批量删除

java 复制代码
delete from t_car where id in(1,2,3);
delete from t_car where id = 1 or id = 2 or id = 3;

批量添加

java 复制代码
insert into t_car values
  (null,'1001','凯美瑞',35.0,'2010-10-11','燃油车'),
  (null,'1002','比亚迪唐',31.0,'2020-11-11','新能源'),
  (null,'1003','比亚迪宋',32.0,'2020-10-11','新能源')

6.1 用in来删除

java 复制代码
    int deleteBatchByForeach(@Param("ids") Integer[] ids);
java 复制代码
    <delete id="deleteBatchByForeach">
        delete from t_car where id in
        (
        <foreach collection="ids" item="id" separator=",">#{id}
        </foreach
        >)
    </delete>

也可以

java 复制代码
    <delete id="deleteBatchByForeach">
        delete from t_car where id in
        
        <foreach collection="ids" item="id" separator="," open="(" close=")">#{id}
        </foreach
        >
    </delete>
java 复制代码
   @Test
    public void testDeleteBatchByForeach(){
        SqlSession sqlSession = MybatisUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        int count = mapper.deleteBatchByForeach(new Integer[]{2, 3, 4});
        System.out.println("删除了几条记录:" + count);
        sqlSession.commit();
    }

6.2 用or来删除

xml 复制代码
    <delete id="deleteBatchByForeach">
        delete from t_car where
        <foreach collection="ids" item="id" separator="or">
         id=#{id}
        </foreach
        >
    </delete>
java 复制代码
    @Test
    public void testDeleteBatchByForeach(){
        SqlSession sqlSession = MybatisUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        int count = mapper.deleteBatchByForeach(new Integer[]{2, 3, 4});
        System.out.println("删除了几条记录:" + count);
        sqlSession.commit();
    }

6.3 批量添加

java 复制代码
int insertBatchByForeach(@Param("cars") List<Car> cars);
xml 复制代码
 <insert id="insertBatchByForeach">
        insert into t_car values
        <foreach collection="cars" item="car" separator=",">
            (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
        </foreach>
    </insert>
java 复制代码
   @Test
    public void testInsertBatchByForeach(){
        SqlSession sqlSession = MybatisUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car1 = new Car(null, "2001", "兰博基尼", 100.0, "1998-10-11", "燃油车");
        Car car2 = new Car(null, "2001", "兰博基尼", 100.0, "1998-10-11", "燃油车");
        Car car3 = new Car(null, "2001", "兰博基尼", 100.0, "1998-10-11", "燃油车");
        List<Car> cars = Arrays.asList(car1, car2, car3);
        int count = mapper.insertBatchByForeach(cars);
        System.out.println("插入了" + count+"条记录");
        sqlSession.commit();
        sqlSession.close();
    }

7、 sql标签与include标签

sql标签用来声明sql片段

include标签用来将声明的sql片段包含到某个sql语句当中

作用:代码复用。易维护

xml 复制代码
<sql id="carCols">id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carType</sql>

<select id="selectAllRetMap" resultType="map">
  select <include refid="carCols"/> from t_car
</select>

<select id="selectAllRetListMap" resultType="map">
  select <include refid="carCols"/> carType from t_car
</select>

<select id="selectByIdRetMap" resultType="map">
  select <include refid="carCols"/> from t_car where id = #{id}
</select>
相关推荐
唐宋元明清218821 分钟前
.NET 阻止系统睡眠/息屏
windows·电源
yylの博客2 小时前
Windows通过git-bash安装zsh
windows·git·bash·zsh
进击的code3 小时前
windows 下使用WLS2 编译aosp Android14并刷机到pixle 5a
windows
cmdch20173 小时前
Mybatis加密解密查询操作(sql前),where要传入加密后的字段时遇到的问题
数据库·sql·mybatis
程序猿小柒3 小时前
【Spark】Spark SQL执行计划-精简版
大数据·sql·spark
听见~5 小时前
SQL优化
数据库·sql
秋恬意6 小时前
什么是MyBatis
mybatis
CodeChampion6 小时前
60.基于SSM的个人网站的设计与实现(项目 + 论文)
java·vue.js·mysql·spring·elementui·node.js·mybatis
染指11106 小时前
50.第二阶段x86游戏实战2-lua获取本地寻路,跨地图寻路和获取当前地图id
c++·windows·lua·游戏安全·反游戏外挂·游戏逆向·luastudio
dntktop7 小时前
Converseen:全能免费批量图像处理专家
windows