Mybatis动态SQL

一、if标签

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

CarMapper接口

import java.util.List;

public interface CarMapper {
/**
* 根据多条件查询Car
* @param brand
* @param guidePrice
* @param carType
* @return
*/
List<Car> selectByMultiCondition(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);
}

CarMapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.powernode.mybatis.mapper.CarMapper">

    <select id="selectByMultiCondition" resultType="car">
        select * from t_car where 0 = 0 
        <if test="brand != null and brand != ''">
            andbrand 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>

mapper>

测试程序

import org.junit.Test;
import java.util.List;

public class CarMapperTest {
    @Test
    public void testSelectByMultiCondition(){
        CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
        List<Car> cars = mapper.selectByMultiCondition("丰田", 20.0, "燃油车");
        System.out.println(cars);
    }
}

执行结果:

Mybatis动态SQL_第1张图片

二、where标签

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

  • 所有条件都为空时,where标签保证不会生成where子句。
  • 自动去除某些条件前面多余的and或or

继续使用if标签中的需求

CarMapper接口

/**
* 根据多条件查询Car,使用where标签
* @param brand
* @param guidePrice
* @param carType
* @return
*/
List<Car> selectByMultiConditionWithWhere(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);

CarMapper.xml

<select id="selectByMultiConditionWithWhere" resultType="car">
  select * from t_car
  <where>
    <if test="brand != null and brand != ''">
      and 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>

测试程序

@Test
public void testSelectByMultiConditionWithWhere(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    List<Car> cars = mapper.selectByMultiConditionWithWhere("丰田", 20.0, "燃油车");
    System.out.println(cars);
}

运行结果:

在这里插入图片描述

如果所有条件都是空呢?

在这里插入图片描述

它可以自动去掉前面多余的and,那可以自动去掉后面多余的and吗?

<select id="selectByMultiConditionWithWhere" 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>

测试程序

// 让最后一个条件为空
List<Car> cars = mapper.selectByMultiConditionWithWhere("丰田", 20.0, "");

运行结果:
在这里插入图片描述

很显然,后面多余的and是不会被去除的

三、trim标签

trim标签的属性:

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

CarMapper接口

/**
* 根据多条件查询Car,使用trim标签
* @param brand
* @param guidePrice
* @param carType
* @return
*/
List<Car> selectByMultiConditionWithTrim(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);

CarMapper.xml

<select id="selectByMultiConditionWithTrim" resultType="car">
  select * from t_car
  <trim prefix="where" suffixOverrides="and|or">
    <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>

测试程序

@Test
public void testSelectByMultiConditionWithTrim(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    List<Car> cars = mapper.selectByMultiConditionWithTrim("丰田", 20.0, "");
    System.out.println(cars);
}

在这里插入图片描述

如果所有条件为空,where会被加上吗?答案是不会的。

在这里插入图片描述

四、set标签

主要使用在update语句当中,用来生成set关键字,同时去掉最后多余的“,
比如我们只更新提交的不为空的字段,如果提交的数据是空或者"",那么这个字段我们将不更新

CarMapper接口

/**
* 更新信息,使用set标签
* @param car
* @return
*/
int updateWithSet(Car car);

CarMapper.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>

测试程序

@Test
public void testUpdateWithSet(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    Car car = new Car(38L,"1001","丰田霸道2",10.0,"",null);
    int count = mapper.updateWithSet(car);
    System.out.println(count);
    SqlSessionUtil.openSession().commit();
}

执行结果:

在这里插入图片描述

五、choose when otherwise

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

<choose>
  <when></when> //else if
  <when></when>
  <when></when>
  <otherwise></otherwise> //else
</choose>

//等同于:
if(){
    
}else if(){
    
}else if(){
    
}else if(){
    
}else{

}

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

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

CarMapper接口

/**
* 使用choose when otherwise标签查询
* @param brand
* @param guidePrice
* @param produceTime
* @return
*/
List<Car> selectWithChoose(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("produceTime") String produceTime);

CarMapper.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>

测试程序

@Test
public void testSelectWithChoose(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    //List cars = mapper.selectWithChoose("丰田霸道", 20.0, "2000-10-10");
    //List cars = mapper.selectWithChoose("", 20.0, "2000-10-10");
    //List cars = mapper.selectWithChoose("", null, "2000-10-10");
    List<Car> cars = mapper.selectWithChoose("", null, "");
    System.out.println(cars);
}

Mybatis动态SQL_第2张图片

六、foreach标签

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

-- 批量删除
delete from t_car where id in(1,2,3);
delete from t_car where id = 1 or id = 2 or id = 3;

-- 批量添加
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','新能源')
  • 批量删除(in)

CarMapper接口

/**
* 通过foreach完成批量删除
* @param ids
* @return
*/
int deleteBatchByForeach(@Param("ids") Long[] ids);

CarMapper.xml


<delete id="deleteBatchByForeach">
  delete from t_car where id in
  <foreach collection="ids" item="id" separator="," open="(" close=")">
    #{id}
  foreach>
delete>

测试程序

@Test
public void testDeleteBatchByForeach(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    int count = mapper.deleteBatchByForeach(new Long[]{40L, 41L, 42L});
    System.out.println("删除了几条记录:" + count);
    SqlSessionUtil.openSession().commit();
}

执行结果:

在这里插入图片描述

  • 批量删除(or)

CarMapper接口

/**
* 通过foreach完成批量删除
* @param ids
* @return
*/
int deleteBatchByForeach2(@Param("ids") Long[] ids);

CarMapper.xml

<delete id="deleteBatchByForeach2">
  delete from t_car where
  <foreach collection="ids" item="id" separator="or">
    id = #{id}
  foreach>
delete>

测试程序

@Test
public void testDeleteBatchByForeach2(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    int count = mapper.deleteBatchByForeach2(new Long[]{40L, 41L, 42L});
    System.out.println("删除了几条记录:" + count);
    SqlSessionUtil.openSession().commit();
}

执行结果:

在这里插入图片描述

  • 批量添加

CarMapper接口

/**
* 批量添加,使用foreach标签
* @param cars
* @return
*/
int insertBatchByForeach(@Param("cars") List<Car> cars);

CarMapper.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>

测试程序

@Test
public void testInsertBatchByForeach(){
    CarMapper mapper = SqlSessionUtil.openSession().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);
    SqlSessionUtil.openSession().commit();
}

执行结果:

在这里插入图片描述

七、sql标签与include标签

  • sql标签用来声明sql片段
  • include标签用来将声明的sql片段包含到某个sql语句当中
  • 作用:代码复用;易维护
<sql id="carCols">id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carTypesql>

<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>

你可能感兴趣的:(Mybatis,mybatis,sql,java)