一 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
分享到:
相关推荐
JDBC读取数据库元数据,生成JAVA实体类,SQL语句,Mybatis动态sql语句,详细内容见博客地址https://blog.csdn.net/zi971553/article/details/82862039
在实际开发过程中,我们往往需要编写复杂的SQL语句,拼接稍有不注意就会导致错误,Mybatis给开发者提供了动态SQL,大大降低了拼接SQL导致的错误。 动态标签 if标签 if标签通常用那个胡where语句,update语句,insert...
Mybatis执行SQL语句的方式
mybatis动态SQL语句--基于xml配置。
适用于springboot(整合过mybatis的)和常规SSM项目,可以获取完整的mybatis执行的sql语句,用于直观的看到执行sql是否异常
mybatis+spring 框架中配置日志中显示sql语句
把 mybatis 输出的sql日志还原成完整的sql语句。 将日志输出的sql语句中的问号 ? 替换成真正的参数值。 通过 "Tools -> MyBatis Log Plugin" 菜单或快捷键 "Ctrl+Shift+Alt+O" 启用。 点击窗口左边的 "Filter" ...
动态SQL是一种根据不同条件动态生成SQL语句的技术。在Mabits中,动态SQL通常是通过使用一组特殊的标签和代码块来实现的,这些标签和代码块可以根据条件包含或排除不同的部分,从而生成不同的SQL语句。动态SQL可以让...
拦截器监控慢SQL并将完整的可执行的SQL语句打印在日志文件中,复制该SQL语句即可在数据库工具中执行。 使用方法: 找到你springboot项目中的配置文件,增加如下配置即可 application.yml 配置如下: sql: slow...
MyBatis动态SQL是一种强大的特性,它允许开发人员根据条件动态地构建SQL语句,从而极大地提高了SQL语句的复用性和灵活性。在传统的JDBC或其他类似框架中,开发人员通常需要手动拼接SQL语句,这不仅工作量大,而且...
内容概要:本文介绍了MyBatis动态SQL的基本概念、常用标签和使用技巧,帮助读者了解如何构建灵活的查询语句。 使用人群:适用于使用MyBatis框架进行数据库操作的开发者,尤其是需要构建复杂查询逻辑的开发者。 ...
MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据库操作。在JDBC或其他类似框架中,开发人员通常需要手动拼接SQL语句,这既繁琐又易出错。...