1. 程式人生 > >mybatis四(動態sql)

mybatis四(動態sql)

test 數據庫連接 executor for 類型 轉換 res emp ognl

<1>
<select id="selectUserByConditions" parameterType="user" resultType="user"> SELECT * FROM USER WHERE 1=1 <if test="id != null and id!=‘‘"> and id =#{id} </if> <if test="id != null and id!=‘‘"> id =#{id}
</if> <if test="email != null and email !=‘‘"> and email =#{email} </if> <if test="lastName != null and lastName !=‘‘"> and last_name =#{lastName} </if> <!-- ognl會進行數字和字符串之間的轉換判斷"0"==0 --> <if test="
gender == 1 or gender == 0"> and gender =#{gender} </if> </select>

<2>

<select id="selectUserByConditions" parameterType="user" resultType="user">
        SELECT * FROM USER
        <!-- where只會去掉第一個and ,如果and在後面的話就會出問題如下

    
<if test="id != null and id!=‘‘
"> id =#{id} and </if> <if test="email != null and email !=‘‘"> email =#{email} and </if> <if test="lastName != null and lastName !=‘‘"> last_name =#{lastName} and </if> <!-- ognl會進行數字和字符串之間的轉換判斷"0"==0 --> <if test="gender == 1 or gender == 0"> gender =#{gender} </if>


    --> <where> <if test="id != null and id!=‘‘"> id =#{id} </if> <if test="email != null and email !=‘‘"> and email =#{email} </if> <if test="lastName != null and lastName !=‘‘"> and last_name =#{lastName} </if> <!-- ognl會進行數字和字符串之間的轉換判斷"0"==0 --> <if test="gender == 1 or gender == 0"> and gender =#{gender} </if> </where> </select>

<3>前後綴解決

後綴解決
<select id="selectUserByConditions" parameterType="user" resultType="user"> SELECT * FROM USER <!-- 後面多出的and或者or where 不能解決 prefix 前綴 prefixOverrides:前綴覆蓋,去掉字符串前面多余的字符 suffix:後綴 給拼裝的字符串添加一個後綴 suffixOverrides:後綴覆蓋 --> <trim prefix="where" suffixOverrides="and"> <if test="id != null and id!=‘‘"> id =#{id} and </if> <if test="email != null and email !=‘‘"> email =#{email} and </if> <if test="lastName != null and lastName !=‘‘"> last_name =#{lastName} and </if> <!-- ognl會進行數字和字符串之間的轉換判斷"0"==0 --> <if test="gender == 1 or gender == 0"> gender =#{gender} </if> </trim> </select>

  前綴解決
<select id="selectUserByCons" parameterType="user" resultType="user"> SELECT * FROM USER <!-- where只會去掉第一個and --> <trim prefix="where" prefixOverrides="and"> <if test="id != null and id!=‘‘"> and id =#{id} </if> <if test="email != null and email !=‘‘"> and email =#{email} </if> <if test="lastName != null and lastName !=‘‘"> and last_name =#{lastName} </if> <!-- ognl會進行數字和字符串之間的轉換判斷"0"==0 --> <if test="gender == 1 or gender == 0"> and gender =#{gender} </if> </trim> </select>

<4>

<select id="selectUserByCons" parameterType="user"
        resultType="user">
        SELECT * FROM USER
        <where>
            <!-- 只選擇其中的一個條件查 -->
            <choose>
            <when test="id != null and id!=‘‘">
                id =#{id}
            </when>
            <when test="email != null and email !=‘‘">
                and email =#{email}
            </when>
            <when test="lastName != null and lastName !=‘‘">
                and last_name =#{lastName}
            </when>
                <otherwise>
                    1=1
                </otherwise>
            </choose>
        </where>
    </select>

<5>更新

<update id="updateUserById" parameterType="model.User">
        UPDATE user 
        <set>
            <if test="email != null and email !=‘‘">
                 email =#{email},
            </if>
            <if test="lastName != null and lastName !=‘‘">
                 last_name =#{lastName},
            </if> 
            <!-- ognl會進行數字和字符串之間的轉換判斷"0"==0 -->
            <if test="gender == 1 or gender == 0">
                 gender =#{gender}
            </if> 
        </set>
        WHERE id=#{id}
    </update>

<6>批量查詢

<select id="selectUserByIds" resultType="user">
        SELECT * FROM USER where id 
        in
        <!-- foreach元素的屬性主要有 item,index,collection,open,separator,close。
             item表示集合中每一個元素進行叠代時的別名, 
            index指 定一個名字,用於表示在叠代過程中,每次叠代到的位置, 遍歷list的時候index就是list的索引,item就是當前值
                                                          遍歷map時,index就是key,item當前值
            open表示該語句以什麽開始, separator表示在每次進行叠代之間以什麽符號作為分隔 符, 
            close表示以什麽結束。 
            如果傳入的是單參數且參數類型是一個List的時候,collection屬性值為list . 
            如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值為array 
            如果傳入的參數是多個的時候,我們就需要把它們封裝成一個Map了,當然單參數也可以封裝成map,實際上如果你在傳入參數的時候,
            在MyBatis裏面也是會把它封裝成一個Map的,map的key就是參數名,
            所以這個時候collection屬性值就是傳入的List或array對象在自己封裝的map裏面的key. -->
       public List<User> selectUserByIds(@Param("ids")List ids);對應collection="ids" <foreach collection="list" item="item_id" open="(" separator="," close=")"> #{item_id} </foreach> </select>

<7>批量保存

mysql下

<!-- public void addDepts(@Param("emps") List<Dept> emps); -->

<!-- MySQL下批量保存,可以foreach遍歷 mysql支持values(),(),()語法 --> //推薦使用
     <insert id="addEmpsBatch">
      INSERT INTO emp(ename,gender,email,did)
      VALUES
      <foreach collection="emps" item="emp" separator=",">
      (#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
      </foreach>
     </insert>
<!-- 這種方式需要數據庫連接屬性allowMutiQueries=true的支持 -->  //在jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
    <!--  <insert id="addEmpsBatch">                                                                                                                 後加上allowMultiQueries=true
       <foreach collection="emps" item="emp" separator=";">                                                                        表示可以多次執行insert into語句,中間;不會錯
         INSERT INTO emp(ename,gender,email,did)
         VALUES(#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
      </foreach>
     </insert> -->

批量batch保存mybatis ExecutorType.BATCH

Mybatis內置的ExecutorType有3種,默認為simple,該模式下它為每個語句的執行創建一個新的預處理語句,單條提交sql;而batch模式重復使用已經預處理的語句,並且批量執行所有更新語句,

顯然batch性能將更優; 但batch模式也有自己的問題,比如在Insert操作時,在事務沒有提交之前,是沒有辦法獲取到自增的id,這在某型情形下是不符合業務要求的

Oracle下批量保存

  方法1,將多個insert放在begin和end之間執行 
  begin
  insert into user(userno,username,email) values (seq_user.nextval,
001,aaaaa); insert into user(userno,username,email) values (seq_user.nextval,002,bbbbbb); end;
方法2利用中間表 insert into user(userno,username,email)
select seq_user.nextval,username,email from( select 003 username ,cccccc email from dual union select 004 username,ddddddddddd email from dual union select 005 username,eeeeeee email from dual ) -->

 <!--示例--> <!-- public void addUsers(@Param("users") List<User> users);-->
  <insert id = "addUsers"databaseId="oracle" parameterType="user">
  <!--Oracle:批量保存方法1 -->
  <foreach collection = "users" item="user" open="begin" close="end;"> insert into user(userno,username,email) values (seq_user.nextval,#{user.username},#{user.email}); </foreach>

<!--
  <!-- Oracle:批量保存方法2 --> insert into user(userno,username,email) select seq_user.nextval,username,email from( <foreach collection="users" item="user" separator="union" > select #{user.username} username ,#{user.email} email from dual </foreach> )
  -->
</insert>

mybatis四(動態sql)