mybatis动态SQL语句

简介:

 

一 if标签

<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标签

<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则不进行更新,而是保持数据库原值

<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语句

<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语句

<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。

 

<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
<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
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
<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
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());
}

 

原帖地址:http://blog.csdn.net/ask_rent/article/details/6320326

 

 

目录
相关文章
|
6月前
|
SQL Java 数据库连接
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
|
5月前
|
SQL Java 数据库连接
MyBatis动态SQL字符串空值判断,这个细节99%的程序员都踩过坑!
本文深入探讨了MyBatis动态SQL中字符串参数判空的常见问题。通过具体案例分析,对比了`name != null and name != &#39;&#39;`与`name != null and name != &#39; &#39;`两种写法的差异,指出后者可能引发逻辑混乱。为避免此类问题,建议在后端对参数进行预处理(如trim去空格),简化MyBatis判断逻辑,提升代码健壮性与可维护性。细节决定成败,严谨处理参数判空是写出高质量代码的关键。
618 0
|
4月前
|
SQL XML Java
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
297 5
|
6月前
|
SQL Java 数据库连接
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
|
6月前
|
SQL 缓存 Java
框架源码私享笔记(02)Mybatis核心框架原理 | 一条SQL透析核心组件功能特性
本文详细解构了MyBatis的工作机制,包括解析配置、创建连接、执行SQL、结果封装和关闭连接等步骤。文章还介绍了MyBatis的五大核心功能特性:支持动态SQL、缓存机制(一级和二级缓存)、插件扩展、延迟加载和SQL注解,帮助读者深入了解其高效灵活的设计理念。
|
7月前
|
SQL XML Java
九、MyBatis动态SQL
九、MyBatis动态SQL
91 2
|
6月前
|
SQL XML Java
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
153 0
|
8月前
|
SQL Java 数据库连接
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
835 6
|
SQL 存储 缓存
一文搞懂MySQL中一条SQL语句是如何执行的
一文搞懂MySQL中一条SQL语句是如何执行的
|
存储 SQL 缓存
【Mysql】执行sql语句后,mysql都做了什么?
【Mysql】执行sql语句后,mysql都做了什么?
【Mysql】执行sql语句后,mysql都做了什么?