zoukankan      html  css  js  c++  java
  • Mbatis——动态SQL

    <?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>
  • 相关阅读:
    React 框架 | 深入剖析 Scheduler 原理 广东靓仔
    Spool and Print [转]
    Excel表中的文本格式与数字格式转换方法(转)
    什么叫网关的精解(超经典)(zt)
    如何在公司局域网内部上网使用代理服务器突破网关(zt)
    百鸟之王
    两种修改网卡物理地址的秘笈
    MAC地址作用以及原理(ZT)
    js 通过后台接口返回的URL地址下载文件并保存到本地(已在项目中使用,保存音视频文件)
    DataTable中数据记录的统计
  • 原文地址:https://www.cnblogs.com/realshijing/p/8076296.html
Copyright © 2011-2022 走看看