Mybatis——动态SQL

采用代理的方式实现

一、返回每条数据存在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 in
4         <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 in
4         <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 in
4         <!-- 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中的语句后要加逗号!

原文地址:https://www.cnblogs.com/ccw95/p/6182214.html