Mybatis——動態SQL

來源:互聯網
上載者:User

標籤:字串   for   動態sql   別名   item   bat   foreach   實現   cte   

採用代理的方式實現

一、返回每條資料存在Emp中:

方法:

1 //<select id="selectEmpList" parameterType="com.neuedu.model.Emp" resultType="com.neuedu.model.Emp">2 public List<Emp> selectEmpList(Emp emp);

mapper.xml:

 1 <!-- 返回每條資料存在Emp中 --> 2     <select id="selectEmpList" parameterType="com.neuedu.model.Emp" resultType="com.neuedu.model.Emp"> 3         select * from emp 4         <where> 5             <if test="empno > 0"> 6                 and empno = #{empno}  7             </if> 8             <if test="ename != null and ename != ‘‘ "> 9                 and ename like ‘%${ename}%‘  <!-- 注意此處,拼接成字串要使用貨幣符號  -->10             </if>11             <if test="job != null and job !=‘‘">12                 and job like ‘%${job}%‘13             </if>14         </where>15     </select>

二、單參數List : List<Integer> empnos:

方法:

1 //<select id="selectEmpByList" resultType="com.neuedu.model.Emp">2     public List<Emp> selectEmpByList(List<Integer> empnos);

mapper.xml:

1 <!-- 單參數List : List<Integer> empnos-->2     <select id="selectEmpByList" resultType="com.neuedu.model.Emp">3         select * from emp where empno in4         <foreach collection="list" item="empno" open="(" separator="," close=")">5             #{empno}6         </foreach>7     </select>

注意:①collection代表遍曆的集合類型

   ②item代表遍曆出來的變數別名

   ③open代表拼接上去時的開始符號

   ④close代表拼接上去時的結束符號

   ⑤separator代表拼接上去時分隔的符號

三、單參數Array:Integer[] empnos

方法:

 

1 //<select id="selectEmpByArray" resultType="com.neuedu.model.Emp">2     public List<Emp> selectEmpByArray(Integer[] empnos);

 

mapper.xml:

1 <!-- 單參數Array:Integer[] empnos -->2     <select id="selectEmpByArray" resultType="com.neuedu.model.Emp">3         select * from emp where empno in4         <foreach collection="array" item="empno" open="(" separator="," close=")">5             #{empno}6         </foreach>7     </select>

四、多參數 Map:【注意:指的是方法的傳入參數有多個的情況】

方法:

1 //<select id="selectEmpByMap" resultType="com.neuedu.model.Emp">2     public List<Emp> selectEmpByMap(Map<String,Object> map);

mapper.xml:

1 <!-- 多參數 Map-->2     <select id="selectEmpByMap" resultType="com.neuedu.model.Emp">3         select * from emp where ename like ‘%${ename}%‘ and empno in4         <!-- collection:指向參數名稱 -->    <!--此處特別重要,名稱不是屬性名稱,而是map中的key名-->5         <foreach collection="empnos" item="empno" open="(" separator="," close=")">6             #{empno}7         </foreach>8     </select>

 五、引入sql語句

定義:

 1 <!-- 定義sql片段:可重複使用 --> 2     <sql id="select_emp_where_sql"> 3         <if test="empno > 0"> 4             and empno = #{empno}  5         </if> 6         <if test="ename != null and ename != ‘‘ "> 7             and ename like ‘%${ename}%‘  8         </if> 9         <if test="job != null and job !=‘‘">10             and job like ‘%${job}%‘11         </if>12     </sql>

引用:

1 <!-- 引入sql片段 -->2             <include refid="select_emp_where_sql"></include>

六、動態更新

 1 <!-- 動態更新 --> 2     <update id="updateEmpByEmpno" parameterType="com.neuedu.model.Emp"> 3         update emp 4          5         <set> 6             <if test="ename != null and ename !=‘‘ "> 7                 ename=#{ename}, 8             </if> 9             <if test="job != null and job !=‘‘ ">10                 job=#{job},11             </if>12         </set>13         14         where empno=#{empno}15     </update>

注意:set中if中的語句後要加逗號!

Mybatis——動態SQL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.