Mybatis-动态SQl

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

  标签一:

  if

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

  mapper文件:

<!--动态sql之if标签
        List<Emp> getEmpListByMoreTj(Emp emp);
        if标签中test属性:表示判断条件(条件满足才会执行对应的sql语句)
    -->
    <select id="getEmpListByMoreTj" resultType="emp">
        select * from t_emp where 1=1
        <if test="empName !=null and empName!=''">
            and empName = #{empName}
        </if>
        <if test="age != '' and age!=null">
            and age = #{age}
        </if>
        <if test="sex != '' and sex != null">
            and sex = #{sex}
        </if>
    </select>

    其中where后面跟的1=1是为了跟好的拼接sql语句,如果没有第一个if标签生效 sql会出现错误p

  标签二:

  where

  where和if一般结合使用:a>若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字

              b>若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的 and去掉

(注意:where标签不能去掉条件最后多余的and ,只能取出前方的多余的and)

    <!--动态sql之where标签-->
    <select id="getEmpListByMoreTj" resultType="emp">
        select * from t_emp
        <where>
            <if test="empName !=null and empName!=''">
             empName = #{empName}
            </if>
            <if test="age != '' and age!=null">
            and age = #{age}
            </if>
            <if test="sex != '' and sex != null">
            and sex = #{sex}
            </if>    
        </where>
    </select>

 

  标签三:

  trim

  trim用于去掉或添加标签中的内容

常用属性:(条件成立时)prefix:在trim标签中的内容的前面添加某些内容

            prefixOverrides:在trim标签中的内容的前面去掉某些内容

            suffix:在trim标签中的内容的后面添加某些内容

            suffixOverrides:在trim标签中的内容的后面去掉某些内容

    <!--动态sql之trim标签-->
    <select id="getEmpListByMoreTrim" resultType="emp">
        select * from t_emp
        <trim prefix="where" suffixOverrides="and">
            <if test="empName !=null and empName!=''">
                empName = #{empName} and
            </if>
            <if test="age != '' and age!=null">
                 age = #{age} and
            </if>
            <if test="sex != '' and sex != null">
                 sex = #{sex}
            </if>
        </trim>
    </select>

 

 

  标签四:

  choose、when、otherwise

  choose、when、otherwise相当于if...else if..else(当条件有一个满足时其他条件就不会再去判断)

              when至少有一个,otherwise最多有一个

    <!--动态sql之choose,when.otherwise标签-->
    <select id="getEmpListByMoreTrim" resultType="emp">
        select * from t_emp
        <where>
            <choose>
                <when test="empName !=null and empName!=''">
                empName = #{empName}
                </when>
                <when test="age != '' and age!=null">
                 age = #{age}
                </when>
                <when test="sex != '' and sex != null">
                 sex = #{sex}
                </when>
                <otherwise>
                    did = 1;
                </otherwise>
            </choose>
        </where>
    </select>

 

  标签五:

  foreach:

  属性:

    collection:设置要循环的数组或集合

    item:表示集合或数组中的每一个数据

       separator:设置循环体之间的分隔符(会自动在sql语句中分隔符前后加入空格)

    open:设置foreach标签中的内容的开始符

    close:设置foreach标签中的内容的结束符

通过foreach标签进行批量删除操作:

  定义接口方法:

    /**
     * 通过数组实现批量删除
     */
    int deleteMoreByArray(@Param("eids") int[] eids);

  mapper文件:

<!--int deleteMoreByArray(@Param("eids") int[] eids);-->
    <delete id="deleteMoreByArray">
        delete from t_emp where eid in
        <foreach collection="eids" item="eid" separator="," open="(" close=")">
             #{eid}
        </foreach>
    </delete>

通过foreach标签进行批量添加操作:

  定义接口方法:

/**
     * 通过List集合实现批量添加
     */
    int insertMoreEmp(@Param("emps") List<Emp> emps);

  mapper文件:

 <!--int insertMoreEmp(List<Emp> emps);-->
    <insert id="insertMoreEmp">
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
        </foreach>
    </insert>

  测试类方法:

    @Test
    public void insertMoreEmp(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp1 = new Emp(5,"张三",20,"男","1859@qq.com");
        Emp emp2 = new Emp (6,"里斯",23," 女","2859@qq.com");
        List<Emp> emps = Arrays.asList(emp1,emp2);
        int i = mapper.insertMoreEmp(emps);
        System.out.println(i);
    }

 

 

  标签六:

  SQL片段

  sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入

  设置sql片段:<sql id="empColumns">eid,emp_name,age,sex,email(设置要拼接的内容)</sql>     id属性设置sql片段的名称

  引用sql片段:<include refid="empColumns"></include>   通过id属性引用sql片段

  定义接口方法:

  Emp selectEmpById(@Param("eid")int eid);

  mapper文件:

 <sql id="empColumns">eid,emp_name,age,sex,email</sql>
    <!--Emp selectEmpById(@Param("eid")int eid);-->
    <select id="selectEmpById" resultType="emp">
        select <include refid="empColumns"></include> from t_emp where eid = #{eid}
    </select>

  测试类方法:

    @Test
    public void selectEmpById(){
        SqlSession session = MybatisUtils.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = mapper.selectEmpById(1);
        System.out.println("emp"+emp);
    }

  查看结果:

  

 

   可以看到sql拼接正常,运行没有问题

posted @ 2022-03-02 21:03  Soleili  阅读(33)  评论(0编辑  收藏  举报