Mybatis-动态SQL

416 阅读2分钟

1.动态SQL概述

我们知道,当我们使用jdbc或者其他框架使用不同的条件去拼接sql语句时,总是十分复杂与繁琐的。而Mybatis开发了一个功能去帮助我们解决SQL语句字符串拼接的痛点问题,那就是动态SQL技术。

2.Mybatis动态sql有哪些标签:

1.if if是为了判断传入的值是否符合某种规则,比如是否不为空;

2.where

where标签可以用来做动态拼接查询条件,当和if标签配合的时候,不用显示的声明类似where 1=1这种无用的条件;

3、choose when otherwise

这是一组组合标签,他们的作用类似于 Java 中的 switch、case、default。只有一个条件生效,也就是只执行满足的条件 when,没有满足的条件就执行 otherwise,表示默认条件;

4.trim

是一个格式化标签,主要有4个参数:

prefix(前缀);

prefixOverrides(去掉第一个标记);

suffix(后缀);

suffixOverrides(去掉最后一个标记)

5.foreach

foreach标签可以把传入的集合对象进行遍历,然后把每一项的内容作为参数传到sql语句中,里面涉及到 item(具体的每一个对象), index(序号), open(开始符), close(结束符), separator(分隔符);

6.sql片段

include可以把大量重复的代码整理起来,当使用的时候直接include即可,减少重复代码的编写;

3.代码演示

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

<select id="getEmpByyCondition" resultType="com.atguigu.mybatis.pojo.Emp">
        select * from t_emp where 1=1
        <if test="empName!=null and empName!=''">
            and emp_name=#{empName}
        </if>
        <if test="age!=null and age!=''">
            and age=#{age}
        </if>
        <if test="gender!=null and gender!=''">
            and gender=#{gender}
        </if>
    </select>

2.where

<select id="getEmpByyCondition" resultType="com.atguigu.mybatis.pojo.Emp">
        select * from t_emp
        <where>
            <if test="empName!=null and empName!=''">
                and emp_name=#{empName}
            </if>
            <if test="age!=null and age!=''">
                and age=#{age}
            </if>
            <if test="gender!=null and gender!=''">
                and gender=#{gender}
            </if>
        </where>


    </select>
    
whereif一般结合使用:
若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and去掉
注意:where标签不能去掉条件最后多余的and

3.choose、when、otherwise

<select id="getEmpByChoose" resultType="com.atguigu.mybatis.pojo.Emp">
        select * from t_emp
        <where>
            <choose>
                <when test="empName!=null and empName!=''">emp_name=#{empName} </when>
                <when test="age!=null and age!=''">ge=#{age} </when>
                <when test="gender!=null and gender!=''"> gender=#{gender}</when>

            </choose>

        </where>
    </select>
    choose、when、 otherwise相当于if...else if..else

4.trim

<select id="getEmpByyCondition" resultType="com.atguigu.mybatis.pojo.Emp">
        select * from t_emp
        <trim prefix="where" suffixOverrides="and">

            <if test="empName!=null and empName!=''">
                emp_name=#{empName} and
            </if>
            <if test="age!=null and age!=''">
                 age=#{age}and
            </if>
            <if test="gender!=null and gender!=''">
                gender=#{gender}
            </if>

        </trim>
    </select>
    trim用于去掉或添加标签中的内容

常用属性:
    prefix(前缀);

    prefixOverrides(去掉第一个标记);

    suffix(后缀);

    suffixOverrides(去掉最后一个标记)

5.foreach

1.批量添加数据
void insertEmps(@Param("emps") List<Emp> emps);
---------------------------------------------
<insert id="insertEmps">
        insert into t_emp value
        <foreach collection="emps" item="emp" separator=",">
          (null,#{emp.empName},#{emp.age},#{emp.gender},null)
        </foreach>
    </insert>
----------------------------------------------
public void test8(){
        SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
        SqlSession sqlSession = sqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp1 = new Emp(null,"数据1",21,"女");
        Emp emp2 = new Emp(null,"数据2",24,"男");
        Emp emp3 = new Emp(null,"数据3",31,"女");
        List<Emp> emps = Arrays.asList(emp1, emp2, emp3);
        mapper.insertEmps(emps);
    }
    item(具体的每一个对象), index(序号), open(开始符), close(结束符), separator(分隔符);

6.SQL片段

List<Emp> selectAll();
--------------------------------------------------------------
<sql id="empColumns"> emp_id,emp_name,age,gender</sql>
    <select id="selectAll" resultType="com.atguigu.mybatis.pojo.Emp">
        select <include refid="empColumns"></include> from t_emp
    </select>

----------------------------------------------------------------
public void test10(){
        SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
        SqlSession sqlSession = sqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emps = mapper.selectAll();
        System.out.println(emps);
    }
    sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引