Mybatis(五):动态SQL

Mybatis(五):动态SQL

  • 前言
  • 一、if
  • 二、where
  • 三、trim
  • 四、choose、when、otherwise
  • 五、foreach
    • 1. 示例一
    • 2. 示例二
  • 六、SQL片段



前言

本博主将用CSDN记录软件开发求学之路上亲身所得与所学的心得与知识,有兴趣的小伙伴可以关注博主!也许一个人独行,可以走的很快,但是一群人结伴而行,才能走的更远!

Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。

动态SQL:

if:根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接到SQL中

where

● 当where标签中有内容时,会自动生成where关键字,并且将内容前多余的andor去掉
● 当where标签中没有内容时,此时where标签没有任何效果
注意:where标签不能将其中内容后面多余的andor去掉

trim
● 若标签中有内容时:

  1. prefix|suffix:将trim标签中内容前面或后面添加指定内容
  2. suffixOverrides|prefixOverrides:将trim标签中内容前面或后面去掉指定内容

● 若标签中没有内容时,trim标签也没有任何效果

choosewhenotherwise
● 相当于if...else if...else
when至少要有一个,otherwise最多只能有一个

foreach

collection:设置需要循环的数组或集合
item:表示数组或集合中的每一个数据
separator:循环体之间的分割符
open:foreach标签所循环的所有内容的开始符
close:foreach标签所循环的所有内容的结束符

sql 标签
● 设置SQL片段:eid,emp_name,age,sex,email
● 引用SQL片段:

一、if

if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之标签中的内容不会执行

DynamicSQLMapper

/**
     * @description:多条件查询
     * @author: Hey
     * @date: 2022/7/4 13:13
     * @param: [emp]
     * @return: java.util.List
     **/
    List<Emp> getEmpByCondition(Emp emp);

DynamicSQLMapper.xml

<select id="getEmpByConditionOne" resultType="Emp">
        
        select * from t_emp where 1=1
        <if test="empName != null and empName != ''">
            emp_name = #{empName}
        if>
        <if test="age != null and age != ''">
            and age = #{age}
        if>
        <if test="sex != null and sex != ''">
            and sex = #{sex}
        if>
        <if test="email != null and email != ''">
            and email = #{email}
        if>
    select>

DynamicSQLMapperTest

/**
     * @description:测试if标签
     * @author: Hey 
     * @date: 2022/7/4 13:25
     * @param: []
     * @return: void
     **/
    @Test
    public void testGetEmpByCondition(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        List<Emp> list = mapper.getEmpByCondition(new Emp(null, "", null, "", null));
        System.out.println(list);
    }

二、where

DynamicSQLMapper.xml

<select id="getEmpByConditionTwo" resultType="Emp">
        select * from t_emp
        <where>
            <if test="empName != null and empName != ''">
                emp_name = #{empName}
            if>
            <if test="age != null and age != ''">
                and age = #{age}
            if>
            <if test="sex != null and sex != ''">
                or sex = #{sex}
            if>
            <if test="email != null and email != ''">
                and email = #{email}
            if>
        where>
    select>

DynamicSQLMapperTest

/**
     * @description:测试where标签
     * @author: Hey 
     * @date: 2022/7/4 13:25
     * @param: []
     * @return: void
     **/
    @Test
    public void testGetEmpByCondition(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        List<Emp> list = mapper.getEmpByCondition(new Emp(null, "", null, "", null));
        System.out.println(list);
    }

三、trim

DynamicSQLMapper.xml

 <select id="getEmpByCondition" resultType="Emp">
        select <include refid="empColumns">include> from t_emp
        <trim prefix="where" suffixOverrides="and|or">
            <if test="empName != null and empName != ''">
                emp_name = #{empName} and
            if>
            <if test="age != null and age != ''">
                age = #{age} or
            if>
            <if test="sex != null and sex != ''">
                sex = #{sex} and
            if>
            <if test="email != null and email != ''">
                email = #{email}
            if>
        trim>
    select>

四、choose、when、otherwise

DeptMapper

/**
         * @description:测试choose、when、otherwise
         * @author: Hey 
         * @date: 2022/7/4 16:50
         * @param: [emp]
         * @return: java.util.List
         **/
        List<Emp> getEmpByChoose(Emp emp);

DynamicSQLMapper.xml:

  
    <select id="getEmpByChoose" resultType="Emp">
        select * from t_emp
        <where>
            <choose>
                <when test="empName != null and empName != ''">
                    emp_name = #{empName}
                when>
                <when test="age != null and age != ''">
                    age = #{age}
                when>
                <when test="sex != null and sex != ''">
                    sex = #{sex}
                when>
                <when test="email != null and email != ''">
                    email = #{email}
                when>
                <otherwise>
                    did = 1
                otherwise>
            choose>
        where>
    select>

    <sql id="empColumns">eid,emp_name,age,sex,emailsql>

    

五、foreach

1. 示例一

DynamicSQLMapper

   /**
         * @description:通过数组实现批量删除
         * @author: Hey
         * @date: 2022/7/4 17:03
         * @param: [eids]
         * @return: int
         **/
        int deleteMoreByArray(@Param("eids") Integer[] eids);

DynamicSQLMapper.xml


    <delete id="deleteMoreByArray">
        delete from t_emp where
        <foreach collection="eids" item="eid" separator="or">
            eid = #{eid}
        foreach>
        
    delete>

DynamicSQLMapperTest

 /**
     * @description:通过数组实现批量删除
     * @author: Hey
     * @date: 2022/7/4 17:11
     * @param: []
     * @return: void
     **/
    @Test
    public void testDeleteMoreByArray(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        int result = mapper.deleteMoreByArray(new Integer[]{6, 7, 8});
        System.out.println(result);
    }

2. 示例二

DynamicSQLMapper

/**
         * @description:通过list集合实现批量添加
         * @author: Hey
         * @date: 2022/7/4 17:23
         * @param: [emps]
         * @return: int
         **/
        int insertMoreByList(@Param("emps") List<Emp> emps);

DynamicSQLMapper.xml

 
    <insert id="insertMoreByList">
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
        foreach>
    insert>

DynamicSQLMapperTest

 /**
     * @description:通过list集合实现批量添加
     * @author: Hey
     * @date: 2022/7/4 17:24
     * @param: []
     * @return: void
     **/
    @Test
    public void testInsertMoreByList(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        Emp emp1 = new Emp(null,"a1",23,"男","[email protected]");
        Emp emp2 = new Emp(null,"a2",23,"男","[email protected]");
        Emp emp3 = new Emp(null,"a3",23,"男","[email protected]");
        List<Emp> emps = Arrays.asList(emp1, emp2, emp3);
        System.out.println(mapper.insertMoreByList(emps));
    }

属性:
collection:设置要循环的数组或集合
item:表示集合或数组中的每一个数据
separator:设置循环体之间的分隔符
open:设置foreach标签中的内容的开始符
close:设置foreach标签中的内容的结束符

六、SQL片段

<sql id="empColumns">
 eid,ename,age,sex,did
sql>
select <include refid="empColumns">include> from t_emp

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