mybatis基礎使用(sql片段)


SQL過程中出現特殊字符報錯:

 

1.進行轉義

2.使用cdata <![CDATA[<=]]>

 

實現動態條件SQL

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tulingxueyuan.mapper.EmpMapper">

    <!-- 
    
    如果在編寫SQL過程中出現特殊字符報錯: 1.進行轉義
                                       2.使用cdata <![CDATA[<=]]>
    -->
    <!--
    使用動態SQL:
    1.實現動態條件SQL
    <if>
           test 條件表達式 支持OGNL表達式
      

      問題: and 需要動態拼接的問題(只有一個條件的情況就不需要and,如果多個條件就必須用and/or 來拼接)
           解決:1.加一個永遠都成立的條件(比如:1=1) , 后面條件都加上and/or就行
                 2.<where
                 3.<trim
    <where 一般會加載動態條件中配合使用, 在有條件的情況下它會自動在所有條件的前面加上WHERE關鍵字, 還會去掉所有條件前面的AND/OR

    <trim  它的功能比較靈活、廣泛。 它可以用來實現<where節點的功能
       prefix 在所有包含的SQL前面加上指定的字符串
       prefixOverrides  在所有包含的SQL前面加上去除指定的字符串
       suffix 在所有包含的SQL后面加上指定的字符串
       prefixOverrides  在所有包含的SQL后面加上去除指定的字符串

    -->

    <!--
    sql片段  解決SQL中重復的代碼冗余,可以提取出來放在sql片段中
    1.  <sql 定義sql片段
            id 唯一標識
    2.   <include 在SQL中引用SQL片段片段
            refid 需要引用的SQL片段的id
            <property 聲明變量, 就可以在SQL片段中動態調用,讓不同的SQL調用同一個SQL片段達到不同的功能
                name 變量名
                value 變量值
                一般情況使用${}在sql片段中引用.一單引用了,一定保證每個include都聲明了該變量
    -->
    <sql id="SelectEmp">
        SELECT ${columns} FROM emp
    </sql>

    <select id="QueryEmp" resultType="Emp">
        <!-- SQL 片段 -->
        <include refid="SelectEmp">
            <property name="columns" value="id"/>
        </include>
        <trim prefix="WHERE" prefixOverrides="and|or"  >
            <if test="id!=null and id>0 ">
                and  id=#{id}
            </if>
            <!--OGNL調用對象的方法-->
            <if test="username!=null and username.indexOf('徐')>-1 ">
                and user_name=#{username}
            </if>
            <if test="beginDate!=null">
                and create_date >=#{beginDate}
            </if>
            <if test="endDate!=null">
                and create_date <![CDATA[<=]]> #{endDate}
            </if>
            <!--OGNL: gt 是大於
                調用靜態方法 -->
            <if test="deptId!=null and deptId gt @cn.tulingxueyuan.pojo.Emp@getNum() ">
                and dept_id=#{deptId}
            </if>
        </trim>

        <!--<where>
            <if test="id!=null and id!='' ">
                and  id=#{id}
            </if>
            <if test="username!=null and username!='' ">
              and user_name=#{username}
            </if>
            <if test="beginDate!=null and beginDate!='' ">
              and create_date >=#{beginDate}
            </if>
            <if test="endDate!=null and endDate!='' ">
              and create_date <![CDATA[<=]]> #{endDate}
            </if>
            <if test="deptId!=null and deptId!='' ">
              and dept_id=#{deptId}
            </if>
        </where>-->
    </select>


    <!--
    <choose when otherwise
    多條件取其中一個
    -->
    <select id="QueryEmp2" resultType="Emp">
        <include refid="SelectEmp">
            <property name="columns" value="id,dept_id"/>
        </include>
        <where>
            <choose>
                <when test="deptName=='經理'">
                    dept_id=1
                </when>
                <when test="deptName=='普通員工'">
                    dept_id=2
                </when>
                <otherwise>
                    dept_id=#{id}
                </otherwise>
            </choose>

        </where>
    </select>

    <!--
    <foreach  循環
        實現in 范圍查詢  使用$可以實現但是有sql注入風險
        collection 需要循環的集合的參數名字
        item  每次循環使用的接收變量
        separator 分割符設置(每次循環在結尾添加什么分隔符,會自動去除最后一個結尾的分隔符)
        open 循環開始添加的字符串
        close 循環結束添加的字符串
        index 循環的下標的變量
    -->
    <select id="QueryEmp3" resultType="Emp">
        <include refid="SelectEmp">
            <property name="columns" value="id,user_name,dept_id"/>
        </include>
        <where>
            <foreach collection="usernames" item="username" separator="," open=" user_name in (" close=")" index="i" >
                #{username}
            </foreach>
        </where>
    </select>

    <!--
    <set 用在update語句上面的
        會自動加上set關鍵字
        會自動去除最后一個更新字段的,
    -->
    <update id="update">
        update emp
        <!--<trim prefix="set" suffixOverrides=",">
            <if test="username!=null and username!='' ">
            user_name=#{username},
            </if>
            <if test="createDate!=null and createDate!='' ">
            create_date=#{createDate},
            </if>
            <if test="deptId!=null and deptId!='' ">
            dept_id=#{deptId}
            </if>
        </trim>-->
        <set>
            <if test="username!=null and username!='' ">
                user_name=#{username},
            </if>
            <if test="createDate!=null">
                create_date=#{createDate},
            </if>
            <if test="deptId!=null and deptId!='' ">
                dept_id=#{deptId},
            </if>
        </set>
        where  id=#{id}
    </update>


    <!-- 實現模糊查詢 like '%xx%'
    1、可以使用mysql的字符串拼接   1. 空格拼接  2.CONCAT函數
    2、可以拼接好再傳進來
    3、使用bind在Mapper映射文件上下文聲明一個變量 <bind>
    -->
    <!--
     <bind> 在Mapper映射文件上下文聲明一個變量
        name 變量名稱
        value 值(支持OGNL表達式)
    -->


    <select id="QueryEmp4" resultType="Emp">
        <bind name="_username" value="'%'+username+'%' "/>
        <include refid="SelectEmp">
            <property name="columns" value="id"/>
        </include> 
        where user_name like    #{_username}
    </select>

     <select id="QueryEmp4" resultType="Emp">
        <include refid="SelectEmp">
            <property name="columns" value="id"/>
        </include> 
        where user_name  LIKE concat(concat('%',#{username}),'%')
    </select>
    


    <!--循環逐條插入-->
    <insert id="inserBatch">
        insert into emp (user_name,create_date,dept_id)
        values
        <foreach collection="emps" item="emp" separator=",">
        (#{emp.username},#{emp.createDate},#{emp.deptId})
        </foreach>
    </insert>


    <!--循環逐條插入
        此種方法需要在配置文件中加入:allowMultiQueries=true
            url: jdbc:mysql://127.0.0.1:3306/vone?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false
    -->
    <insert id="inserBatch">
        <foreach collection="emps" item="emp" separator=";">
        insert into emp (user_name,create_date,dept_id)
        values
            (#{emp.username},#{emp.createDate},#{emp.deptId})
        </foreach>
    </insert>

</mapper>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM