一 if标签

1
2
3
4
5
6
<select id=" getStudentListLikeName " parameterType="StudentEntity" resultMap="studentResultMap">
 SELECT * from STUDENT_TBL ST
 <if test="studentName!=null and studentName!='' ">
  WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
 </if>
</select>

二 where标签

1
2
3
4
5
6
7
8
9
10
11
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">
 SELECT * from STUDENT_TBL ST
 <where>
  <if test="studentName!=null and studentName!='' ">
   ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
  </if>
  <if test="studentSex!= null and studentSex!= '' ">
   AND ST.STUDENT_SEX = #{studentSex}
  </if>
 </where>
</select>

如果它包含的标签中有返回值的话就插入一个where。此外如果标签返回的内容是以AND或OR开头的,则它会剔除掉。

三 set 标签

使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<update id="updateStudent" parameterType="StudentEntity">
 UPDATE STUDENT_TBL
 <set>
  <if test="studentName!=null and studentName!='' ">
   STUDENT_TBL.STUDENT_NAME = #{studentName},
  </if>
  <if test="studentSex!=null and studentSex!='' ">
   STUDENT_TBL.STUDENT_SEX = #{studentSex},
  </if>
  <if test="studentBirthday!=null ">
   STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
  </if>
  <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">
   STUDENT_TBL.CLASS_ID = #{classEntity.classID}
  </if>
 </set>
 WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
</update>

四 trim标签

trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果,where例子的等效trim语句

1
2
3
4
5
6
7
8
9
10
11
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">
 SELECT * from STUDENT_TBL ST
 <trim prefix="WHERE" prefixOverrides="AND|OR">
  <if test="studentName!=null and studentName!='' ">
   ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
  </if>
  <if test="studentSex!= null and studentSex!= '' ">
   AND ST.STUDENT_SEX = #{studentSex}
  </if>
 </trim>
</select>

set例子的等效trim语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<update id="updateStudent" parameterType="StudentEntity">
 UPDATE STUDENT_TBL
 <trim prefix="SET" suffixOverrides=",">
  <if test="studentName!=null and studentName!='' ">
   STUDENT_TBL.STUDENT_NAME = #{studentName},
  </if>
  <if test="studentSex!=null and studentSex!='' ">
   STUDENT_TBL.STUDENT_SEX = #{studentSex},
  </if>
  <if test="studentBirthday!=null ">
   STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
  </if>
  <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">
   STUDENT_TBL.CLASS_ID = #{classEntity.classID}
  </if>
 </trim>
 WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
</update>

五 choose (when, otherwise)

有时候并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch语句,choose为switch,when为case,otherwise则为default。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">
 SELECT * from STUDENT_TBL ST
 <where>
  <choose>
   <when test="studentName!=null and studentName!='' ">
    ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
   </when>
   <when test="studentSex!= null and studentSex!= '' ">
    AND ST.STUDENT_SEX = #{studentSex}
   </when>
   <when test="studentBirthday!=null">
    AND ST.STUDENT_BIRTHDAY = #{studentBirthday}
   </when>
   <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">
    AND ST.CLASS_ID = #{classEntity.classID}
   </when>
   <otherwise></otherwise>
  </choose>
 </where>
</select>

六 foreach

对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。List实例将使用“list”做为键,数组实例以“array”做为键。

1 参数为list实例的写法

SqlMapper.xml

1
2
3
4
5
6
7
<select id="getStudentListByClassIDs" resultMap="studentResultMap">
 SELECT * FROM STUDENT_TBL ST
  WHERE ST.CLASS_ID IN 
  <foreach collection="list" item="classList"  open="(" separator="," close=")">
   #{classList}
  </foreach> 
</select>

Java

1
2
3
4
5
6
7
List<String> classList = new ArrayList<String>();
classList.add("20000002");
classList.add("20000003");
List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(classList);
for(StudentEntity entityTemp : studentList){
 System.out.println(entityTemp.toString());
}

2 参数为Array实例的写法

SqlMapper.xml

1
2
3
4
5
6
7
<select id="getStudentListByClassIDs" resultMap="studentResultMap">
 SELECT * FROM STUDENT_TBL ST
  WHERE ST.CLASS_ID IN 
  <foreach collection="array" item="ids"  open="(" separator="," close=")">
   #{ids}
  </foreach>
</select>

Java

1
2
3
4
5
6
7
String[] ids = new String[2];
ids[0] = "20000002";
ids[1] = "20000003";
List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(ids);
for(StudentEntity entityTemp : studentList){
 System.out.println(entityTemp.toString());
}

mybatis动态SQL语句的更多相关文章

  1. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...

  2. Mybatis 动态Sql语句《常用》

    MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...

  3. mybatis 动态sql语句(3)

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件判断) 2. ...

  4. mybatis 动态sql语句(1)

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件判断) 2. ...

  5. 【转】mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    转自:除非申明,文章均为一号门原创,转载请注明本文地址,谢谢! 转载地址:http://blog.csdn.net/kutejava/article/details/9164353#t5 1. if ...

  6. 7. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    转自:http://www.kyjszj.com/htzq/79.html 1. if 语句 (简单的条件判断) 2. choose (when,otherwize) ,相当于java 语言中的 sw ...

  7. MyBatis学习总结(11)——MyBatis动态Sql语句

    MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...

  8. mybatis动态sql语句学习(一)

    动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格 ...

  9. mybatis 动态sql语句(2)

    什么是动态SQL MyBatis的一个强大特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相似框架的经验,你就明白条件串联SQL字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的 ...

随机推荐

  1. iOS10 UI教程禁用视图与用户的交互

    iOS10 UI教程禁用视图与用户的交互 在上文中我们提到了使用isHidden属性和alpha属性可以使视图与用户的交互被禁用,除此之外此功能还可以使用UIView的isUserInteractio ...

  2. 创建com服务器

    Delphi Com深入编程 第二章:

  3. linux 添加新硬盘的方法

    在服务器上把硬盘接好,启动linux,以root登陆. 比如我新加一块SCSI硬盘,需要将其分成三个区: #fdisk /dev/sdb 进入fdisk模式: Command (m for help) ...

  4. oracle sql别名

    为sql的字段起一个别名,常以为是可有可无的,但是有时候是必要的. 例如在ibatis中 <!-- 获取已发或待发送的彩信记录列表 --> <resultMap id="m ...

  5. Shell 编程基础之 For 练习

    一.语法 for var in con1 con2 con3 ... do # 执行内容 done for var in {min..max} do # 执行内容 done for (( 初始值; 限 ...

  6. ACM 水池数目

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  7. BZOJ4530: [Bjoi2014]大融合

    Description 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够 联通的树上路过它 ...

  8. mysql命令行参数(转)

    MySQL命令行参数 Usage: mysql [OPTIONS] [database] //命令方式  -?, --help //显示帮助信息并退出  -I, --help //显示帮助信息并退出  ...

  9. Oracle 客户端免安装数据库连接

    PLSQL Developer 连接数据库需要安装Oracle客户端,在这里介绍一种Oracle客户端解压式安装,而不是exe安装. 安装步骤: 1:确定客户端安装所在主机的系统位数 2:选择正确的客 ...

  10. java poi ppt操作示例

    poi3.9版本,官网 http://poi.apache.org/slideshow/how-to-shapes.html import java.awt.Color; import java.io ...