码迷,mamicode.com
首页 > 数据库 > 详细

Mbatis——动态SQL

时间:2017-12-21 23:05:40      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:main   mybatis   des   规则   --   multi   结果   tty   拼接   

<?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="com.nuch.edu.mapper.EmployeeDynamicSql">

    <!--
    • if:判断
    • choose (when, otherwise):分支选择;带了break的swtich-case
    如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个
    • trim 字符串截取(where(封装查询条件), set(封装修改条件))
    • foreach 遍历集合
     -->
    <!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 -->
    <select id="getEmpsByConditionIf" resultType="com.nuch.edu.domain.Employee">
        SELECT * FROM tbl_employee
        <where>
            <!-- test:判断表达式(OGNL)
             OGNL参照PPT或者官方文档。
             -->
            <if test="id != null ">
                id = #{id}
            </if>
            <!-- ognl会进行字符串与数字的转换判断  "0"==0 -->
            <if test="gender==0 or gender ==1">
                and gender = #{gender}
            </if>
            <if test="email != null and email.trim() != ‘‘">
                and email = #{email}
            </if>
            <if test="lastName != null and lastName.trim() != ‘‘">
                and last_name like #{lastName}
            </if>
        </where>
    </select>

    <select id="getEmpsByConditionTrim" resultType="com.nuch.edu.domain.Employee">
        SELECT * FROM tbl_employee
        <!-- 后面多出的and或者or where标签不能解决
         prefix="":前缀:trim标签体中是整个字符串拼串 后的结果。
                 prefix给拼串后的整个字符串加一个前缀
         prefixOverrides="":
                 前缀覆盖: 去掉整个字符串前面多余的字符
         suffix="":后缀
                 suffix给拼串后的整个字符串加一个后缀
         suffixOverrides=""
                 后缀覆盖:去掉整个字符串后面多余的字符

         -->
        <!-- 自定义字符串的截取规则 -->
        <trim prefix="where" prefixOverrides="and" suffix="" suffixOverrides="and">
            <if test="id != null ">
                id = #{id} and
            </if>
            <if test="gender==0 or gender ==1">
                 gender = #{gender} and
            </if>
            <if test="email != null and email.trim() != ‘‘">
                 email = #{email} and
            </if>
            <if test="lastName != null and lastName.trim() != ‘‘">
                 last_name like #{lastName} and
            </if>
        </trim>
    </select>

    <select id="getEmpsByConditionChoose" resultType="com.nuch.edu.domain.Employee">
        SELECT * FROM tbl_employee
        <where>
            <!-- 如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个 -->
            <choose>
                <when test="id != null" >id = #{id}</when>
                <when test="email != null and email.trim() != ‘‘"> email = #{email}</when>
                <when test="lastName != null and lastName.trim() !=‘‘" >last_name = #{lastName}</when>
                <otherwise>
                    gender = 1
                </otherwise>
            </choose>
        </where>
    </select>

    <update id="UpdateEmpsBySet">
        update tbl_employee
        <!-- Set标签的使用 -->
        <set>
            <if test="gender==0 or gender ==1">
                gender = #{gender} ,
            </if>
            <if test="email != null and email.trim() != ‘‘">
                email = #{email} ,
            </if>
            <if test="lastName != null and lastName.trim() != ‘‘">
                last_name = #{lastName} ,
            </if>
        </set>
        <where>
            <if test="id != null ">
                id = #{id}
            </if>
        </where>
    </update>

    <select id="getEmpsByConditionCollection" resultType="com.nuch.edu.domain.Employee">
        SELECT * from tbl_employee
          WHERE  id in
        <!--
             collection:指定要遍历的集合:
                 list类型的参数会特殊处理封装在map中,map的key就叫list
             item:将当前遍历出的元素赋值给指定的变量
             separator:每个元素之间的分隔符
             open:遍历出所有结果拼接一个开始的字符
             close:遍历出所有结果拼接一个结束的字符
             index:索引。遍历list的时候是index就是索引,item就是当前值
                          遍历map的时候index表示的就是map的key,item就是map的值

             #{变量名}就能取出变量的值也就是当前遍历出的元素
           -->
          <foreach collection="ids" item="id" open="(" close=")" separator=",">
              #{id}
          </foreach>
    </select>
    <!-- 批量保存 -->
    <!--MySQL下批量保存:可以foreach遍历   mysql支持values(),(),()语法-->
   <!-- <insert id="addEmps">
        INSERT  INTO tbl_employee (gender,email,last_name,dept_id) VALUES
          <foreach collection="emps" item="emp"  separator=",">
              (#{emp.gender},#{emp.email},#{emp.lastName},#{emp.dept.id})
          </foreach>
    </insert>-->

    <!-- 一次执行多个sql需要数据库连接属性allowMultiQueries=true;
        这种分号分隔多个sql可以用于其他的批量操作(删除,修改) -->
    <!--url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true-->
    <insert id="addEmps">
        <foreach collection="emps" item="emp"  separator=";">
            INSERT  INTO tbl_employee (gender,email,last_name,dept_id) VALUES
            (#{emp.gender},#{emp.email},#{emp.lastName},#{emp.dept.id})
        </foreach>
    </insert>
</mapper>

Mbatis——动态SQL

标签:main   mybatis   des   规则   --   multi   结果   tty   拼接   

原文地址:http://www.cnblogs.com/realshijing/p/8076296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!