MyBatis教程之五动态SQL的使用

来源:互联网 发布:全国淘宝店铺有多少个 编辑:程序博客网 时间:2024/06/09 15:21

MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦。拼接的时候要确保不能忘了必要的空格,还要注意省掉列名列表最后的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦
常用的动态SQL的标签:
1、if
条件验证,单条件或者多个条件之间没有关系
常用属性:
test:boolean类型

和>=可以直接使用
<需要使用lt
<=需要使用lte

2、choose
多条件选择其一
常常结合内部标签:when和otherwise
3、when
验证条件是否满足
常用属性:
test:boolean类型

4、otherwise
相当于else或default

5、where
根据内部的内容选择是否需要拼接where
还可以移除条件前面的多余的条件连接符(and或or)

6、set
根据内部的实际来决定是否添加set关键字
还可以移除后面的,

7、trim
添加或去除前后缀的字符
常用属性:
prefix:添加前缀
prefixOverrides:删除指定的前缀字符
suffix:添加后缀
suffixOverrides:删除后缀

8、foreach
主要就是遍历集合或数组中的元素
一般用在查询的in关键字
常用属性:
collection:对应的集合或数组的名称
item:元素名称
index:索引
open:开始的符号
close:结束的符号
separator:多个元素之间的分隔符

如果要使用foreach,那么对应的数据库可以有以下三种提供:
1、list集合
2、数组
3、map的key的值为list或array

9、bind
为模糊查询的内容准备模板
常用属性:
1、name:当前bind的唯一标记
2、value:对应的模糊查询的内容:格式:’%’+参数名称+’%’

代码实现:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- 命名空间:需要实现的接口的全称 --><mapper namespace="cn.code404.mapper.StudentMapper">    <insert id="save" parameterType="Student" keyProperty="id"        useGeneratedKeys="true">        insert into student(name,age,sex,address,height,weight)        values(#{name},#{age},#{sex},#{address},#{height},#{weight})    </insert>    <select id="select" resultType="Student">        select * from student    </select>    <!-- 演示动态SQL -->    <!--多条件满足其一 -->    <update id="update1" parameterType="Student">        update student set name=#{name}        <!--1、if就是条件验证 -->        <!-- <if test="id>0"> where id=#{id} </if> -->        <!--2、choose:多条件选其一 -->        <choose>            <!--3、条件是否满足 -->            <when test="id>0">                where id=#{id}            </when>            <when test="height>0">                where height=#{height}            </when>            <!--4、上述条件都不满足 -->            <otherwise>                where weight=#{weight}            </otherwise>        </choose>    </update>    <!--多个条件可能同时存在 -->    <update id="update2" parameterType="Student">        update student set name=#{name}        <!--5、where:是否拼接where -->        <where>            <!-- <trim prefixOverrides="and | or "></trim> -->            <if test="id>0">id=#{id} </if>            <if test="height>0">or height=#{height}</if>            <if test="weight>0">and weight=#{weight}</if>        </where>    </update>    <!--set的时候根据实际的值进行修改,有值就修改 -->    <update id="update4" parameterType="Student">        update student        <!--set:根据实际进行set的拼接,还会去除后面多余的, -->        <set>            <trim prefixOverrides=",">                <if test="name!=null and name.length>0">                    name=#{name}                </if>                <if test="address!=null and address.length>0">                    ,address=#{address}                </if>            </trim>        </set>        <if test="id lte 10">            where id=#{id}        </if>    </update>    <sql id="cx">        select        <include refid="cxzd" />        from student    </sql>    <sql id="cxzd">        id,name,age,weight,height,sex,address    </sql>    <!--foreach的使用 -->    <!--通过map集合 -->    <select id="selectByIds" parameterType="map" resultType="Student">        <include refid="cx" />        where id in        <!--foreach遍历集合或数组 -->        <foreach collection="ids" item="item" index="i" open="("            close=")">            #{item}            <if test="i lt ids.size()-1">,</if>            <!-- 取索引为奇数的主键值 -->            <!-- <if test="i%2==1"> #{item} </if> -->        </foreach>        <if test="height>0">            and height>#{height}        </if>    </select>    <!--通过List集合 -->    <select id="selectByIds1" parameterType="list" resultType="Student">        select * from student        <where>            id in            <foreach collection="list" item="i" open="(" close=")"                separator=",">                #{i}            </foreach>        </where>    </select>    <!--通过数组 -->    <select id="selectByIds2"  resultType="Student">        select * from student        <where>            id in            <foreach collection="array" item="i" open="(" close=")"                separator=",">                #{i}            </foreach>        </where>    </select>    <select id="selectLikeName" parameterType="String" resultType="Student">        <!--绑定为模糊查询制定模板 -->        <bind name="ln" value="'%'+name+'%'"></bind>        select * from student where name like #{ln}    </select></mapper>
原创粉丝点击