【串线篇】Mybatis之动态sql
2024-09-06 00:07:21
一、if标签
<select id="getTeacherByCondition" resultMap="teacherMap"> select * from t_teacher where <if test="id!=null"> id > #{id} and </if> <if test="name!=null && !name.equals("")"> teacherName like #{name} and </if> <if test="birth!=null"> birth_date < #{birth} and </if> <!--"" “”的转义字符-- > <!--&& &&的转义字符,或者直接写and妥了 -- > <!-- <小于号的转义字符 --> </select>
【注】
test属性指定JavaBean里的属性名
teacherName like #{name} and:teacherName是数据库表里的字段名
二、while标签
where可以帮我们去除掉前面的and;
id > #{id},and teacherName like #{name},and birth_date < #{birth}
意思是万一有比如第一句不满足,来到了第二句但第二局前面多了个and这时候where标签就会自动去除
三、trim标签
<!-- trim:截取字符串 prefix="":前缀;为我们下面的sql整体添加一个前缀 prefixOverrides="": 去除出整体字符串前面可能多余的字符 suffix="":为整体添加一个后缀 suffixOverrides="":后面可能哪个多了可以去掉; --> <!-- 我们的查询条件就放在where标签中;每个and写在前面, where帮我们自动取出前面多余的and --> <trim prefix="where" prefixOverrides="and" suffixOverrides="and"> <if test="id!=null"> id > #{id} and </if> <if test="name!=null && !name.equals("")"> teacherName like #{name} and </if> <if test="birth!=null"> birth_date < #{birth} and </if> </trim> </select>
四、foreach标签
<!-- public List<Teacher> getTeacherByIdIn(@Param("ids")List<Integer> ids); <select id="getTeacherByIdIn" resultMap="teacherMap"> SELECT * FROM t_teacher WHERE id IN <!-- 帮我们遍历集合的; collection="":指定要遍历的集合的key close="":以什么结束 index="i":索引; 如果遍历的是一个list; index:指定的变量保存了当前索引 item:保存当前遍历的元素的值 如果遍历的是一个map: index:指定的变量就是保存了当前遍历的元素的key item:就是保存当前遍历的元素的值 item="变量名":每次遍历出的元素起一个变量名方便引用 open="":以什么开始 separator="":每次遍历的元素的分隔符 (#{id_item},#{id_item},#{id_item} --> <if test="ids.size >0"> <foreach collection="ids" item="id_item" separator="," open="(" close=")"> #{id_item} </foreach> </if> </select>
五、choose-when标签
<!--public List<Teacher> getTeacherByConditionChoose(Teacher teacher); --> <select id="getTeacherByConditionChoose" resultMap="teacherMap"> select * from t_teacher <where> <choose> <when test="id!=null"> id=#{id} </when> <when test="name!=null and !name.equals("")"> teacherName=#{name} </when> <when test="birth!=null"> birth_date = #{birth} </when> <otherwise> 1=1 </otherwise> </choose> </where> </select>
【注】
1)、与if不同的是,当满足一个条件的时候when只会进一个,而if都会判断
2)、<otherwise>1=1</otherwise>执行所有查询,有几条返回几条
六、set标签完成mybatis动态更新
!-- public int updateTeacher(Teacher teacher); --> <update id="updateTeacher"> UPDATE t_teacher <set> <if test="name!=null and !name.equals("")"> teacherName=#{name}, </if> <if test="course!=null and !course.equals("")"> class_name=#{course}, </if> <if test="address!=null and !address.equals("")"> address=#{address}, </if> <if test="birth!=null"> birth_date=#{birth} </if> </set> <where> id=#{id} </where> </update>
【注】set就是sql语句中的set,他会帮我们自动去除可能多余的逗号
七、sql标签与include
1)、抽取sql到外面
<sql id=”selectSql”>select * from t_teacher</sql>
2)、内调用
<select id="getTeacherById" resultMap="teacherMap">
<include refid=”selectSql”></include>
where id=#{id}
</select>
最新文章
- java 开发业务逻辑的思考(1)- 通知短信发送
- C# 一个页面,多个Updatepannel,多个Timer
- 【集合框架】JDK1.8源码分析之ArrayList(六)
- 在 MVC4 中使用 Uploadify 3.2 - 1
- Abot爬虫和visjs
- 【LeetCode】5. Longest Palindromic Substring 最大回文子串
- Android读取Manifest文件下Application等节点下的metadata自定义数据
- KISSY对vm的抽取
- 服务器 Disk full
- 目标文件obj的各段 2
- 开窗函数over
- sql server 锁与事务拨云见日(上)
- nginx配置文件注释说明
- golang xorm框架的使用
- 【Atcoder ARC060F】最良表現 / Best Representation
- npm install xxx --save-dev 与npm install xxx --save 的区别
- LeetCode108.将有序数组转换为二叉搜索树
- String和StringBuffer和StringBuilder
- hibernate中3个重要的类 Configuration SessionFactory Session
- SharePoint Development - Custom List using Visual Studio 2010 based SharePoint 2010