MyBatis动态SQL

简介: MyBatis动态SQL


什么是动态SQL?

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


那假如现在我只想根据id来查询数据或者年龄来查询数据难道还要去专门再写一个接口和方法吗?



那么现在开始动态SQL就 来帮我们解决这个难题了

if标签

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

    <select id="getEmpByConditionOne" resultType="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="sex !=null and sex != ''">
            and sex = #{sex}
        </if>
        <if test="email !=null and email != ''">
            and email = #{email}
        </if>
    </select>

if标签中的test属性是我们的判断属性当满足判断条件就会粘贴它里面的sql语句,但有一个弊端,就是当你如是这样子



就会报这样的错误


这也就是上面我们写成这样的原因



where标签

where:当where标签中有内容时,会自动生成where关键字,并且将内容前多余的or和and去掉

* 当where标签中没有内容时,此时where标签没有任何效果

* 注意:where标签中不能将其中内容后面多余的and或or去掉
示例代码

    <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 != ''">
             and sex = #{sex} and
        </if>
        <if test="email !=null and email != ''">
             and email = #{email}
        </if>
        </where>
    </select>

trim标签

trim:

* prefix|suffix:将trim标签中内容前面或后面添加指定内容

* prefixOverides|suffixOverides:将trim标签中内容前面或后面去掉指定内容

* 若标签中没有内容时,trim标签也没有任何效果
示例代码:

    <!--List<Emp> getEmpByCondition(Emp emp);-->
    <select id="getEmpByCondition" resultType="emp">
        select <include refid="empColums"></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} and
            </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标签

choose,when,otherwise,相当于if…else if…else

* when至少有一个,otherwise最多只能有一个

示例代码:

    <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>
                    eid = 1
                </otherwise>
            </choose>
        </where>
    </select>

foreach标签

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

不知道是否你们还记得我们之前学的批量删除,当时我们传给的参数是一个字符串,那现在如果我们传的参数是一个数组该怎么实现批量删除呢,还有,怎么样来实现批量增加呢?
批量删除:

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

批量增加:

    <!--int insertMoreBylist(@Param("emps") List<Emp> emps);-->
    <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>

sql标签

* 设置SQL片段:<sql id="empColums">eid,emp_name,age,sex,email</sql>
* 引用SQL片段:<include refid="empColums"></include>*/

代码示例:

 <sql id="empColums">eid,emp_name,age,sex,email</sql>
    <!--List<Emp> getEmpByCondition(Emp emp);-->
    <select id="getEmpByCondition" resultType="emp">
        select <include refid="empColums"></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} and
            </if>
            <if test="sex !=null and sex != ''">
                sex = #{sex} and
            </if>
            <if test="email !=null and email != ''">
                email = #{email}
            </if>
            </trim>
    </select>
目录
相关文章
|
3天前
|
SQL Java 测试技术
3、Mybatis-Plus 自定义sql语句
这篇文章介绍了如何在Mybatis-Plus框架中使用自定义SQL语句进行数据库操作。内容包括文档结构、编写mapper文件、mapper.xml文件的解释说明、在mapper接口中定义方法、在mapper.xml文件中实现接口方法的SQL语句,以及如何在单元测试中测试自定义的SQL语句,并展示了测试结果。
3、Mybatis-Plus 自定义sql语句
|
23天前
|
SQL Java 数据库连接
idea中配置mybatis 映射文件模版及 mybatis plus 自定义sql
idea中配置mybatis 映射文件模版及 mybatis plus 自定义sql
37 3
|
1月前
|
SQL Java 数据库连接
mybatis动态SQL常用语法总结
MyBatis 使用 OGNL 表达式语言处理动态SQL,如 `if` 标签进行条件判断,`choose`、`when`、`otherwise` 实现多条件选择,`where`、`set` 管理SQL关键字,`trim` 提供通用修剪功能,`foreach` 遍历集合数据。`sql` 和 `include` 用于代码重用,`selectKey` 处理插入后的返回值。参数传递支持匿名、具名、列表、Map、Java Bean和JSON方式。注意SQL转义及使用合适的jdbcType映射Java类型。
50 7
|
2月前
|
SQL Java 数据库连接
深入探索MyBatis Dynamic SQL:发展、原理与应用
深入探索MyBatis Dynamic SQL:发展、原理与应用
|
2月前
|
SQL 缓存 Java
Java框架之MyBatis 07-动态SQL-缓存机制-逆向工程-分页插件
Java框架之MyBatis 07-动态SQL-缓存机制-逆向工程-分页插件
|
2月前
|
SQL Java 数据库连接
MyBatis动态SQL
MyBatis动态SQL
35 0
|
2月前
|
SQL Java 数据库连接
Mybatis日志SQL解析
Mybatis日志SQL解析
30 0
|
9月前
|
SQL 安全 Java
MyBatis映射文件深入--动态sql
MyBatis映射文件深入--动态sql
75 0
|
9月前
|
SQL Java 数据库连接
MyBatis 动态 SQL
MyBatis 动态 SQL
|
2月前
|
SQL XML Java
MyBatis第四课动态SQL
MyBatis第四课动态SQL