程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

【MyBatis 基础知识总结 3】动态sql

发布于2021-06-12 14:17     阅读(451)     评论(0)     点赞(21)     收藏(1)


动态sql,顾名思义,就是动态的在xml中拼接sql语句。

1、where

  1. <select id="getUserById" resultType="com.guor.bean.User">
  2. SELECT * FROM user
  3. <where>
  4. <if test="position != null">
  5. AND position like #{position}
  6. </if>
  7. <if test="school != null">
  8. AND school like #{school}
  9. </if>
  10. </where>
  11. </select>

注:where元素只有至少一个条件存在时,才会有where,而且,如果开头是and或者or时,where元素会自动将其舍弃。

2、trim

其实where也可以用trim来改写:

  1. <select id="getUserById" resultType="com.guor.bean.User">
  2. SELECT * FROM user
  3. <trim prefix="WHERE" prefixOverrides="AND |OR ">
  4. <if test="position != null">
  5. AND position like #{position}
  6. </if>
  7. <if test="school != null">
  8. AND school like #{school}
  9. </if>
  10. </trim>
  11. </select>

3、set

set元素会动态前置set关键字,同时也会删除无关的逗号。

  1. <update id="updateUserById">
  2. update user
  3. <set>
  4. <if test="username != null">username=#{username},</if>
  5. <if test="password != null">password=#{password},</if>
  6. <if test="email != null">email=#{email},</if>
  7. <if test="address != null">ddress=#{ddress}</if>
  8. </set>
  9. where id=#{id}
  10. </update>

set也可以用trim进行改写:

  1. <update id="updateUserById">
  2. update user
  3. <trim prefix="SET" suffixOverrides=",">
  4. <if test="username != null">username=#{username},</if>
  5. <if test="password != null">password=#{password},</if>
  6. <if test="email != null">email=#{email},</if>
  7. <if test="address != null">ddress=#{ddress}</if>
  8. </trim>
  9. where id=#{id}
  10. </update>

4、if

  1. <select id="getUserById" resultType="com.guor.bean.User">
  2. SELECT * FROM user WHERE 1 = 1
  3. <if test="position != null">
  4. AND position like #{position}
  5. </if>
  6. </select>

5、choose、when、otherwish

  1. <select id="getUsersById" resultType="com.guor.bean.User">
  2. SELECT * FROM user WHERE 1 = 1
  3. <choose>
  4. <when test="position != null">
  5. AND position like #{position}
  6. </when>
  7. <when test="department != null and department.name != null">
  8. AND department_name like #{department.name}
  9. </when>
  10. <otherwise>
  11. AND department_name = '公司'
  12. </otherwise>
  13. </choose>
  14. </select>

6、foreach

  1. <select id="selectUserById" resultType="com.guor.bean.User">
  2. SELECT id,name,age,sex,address,school FROM user u
  3. WHERE id in
  4. <foreach item="item" index="index" collection="list"
  5. open="(" separator="," close=")">
  6. #{item}
  7. </foreach>
  8. </select>

当使用可迭代对象或者数组时,index 是当前迭代的次数,item 的值是本次迭代获取的元素。

当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。

7、bind

  1. <select id="selectUserByDept" resultType="com.guor.bean.User">
  2. <bind name="dept" value="'%' + _department.getId() + '%'" />
  3. SELECT * FROM user
  4. WHERE dept_id = #{dept}
  5. </select>

8、连接多数据库

  1. <insert id="getCurrentTime">
  2. <if test="_databaseId == 'oracle'">
  3. select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual
  4. </if>
  5. <if test="_databaseId == 'mysql'">
  6. select now() as Systemtime
  7. </if>
  8. </insert>

 

往期精彩内容:

Java知识体系总结

Spring框架总结

超详细的springBoot学习笔记

常见数据结构与算法整理总结

Java设计模式:23种设计模式全面解析

Java面试题总结(附答案)

Linux知识体系总结

Redis知识体系总结

 

 

 

原文链接:https://blog.csdn.net/guorui_java/article/details/117792065



所属网站分类: 技术文章 > 博客

作者:天花灯

链接:http://www.javaheidong.com/blog/article/222118/34a6e0c976bc7b951a7a/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

21 0
收藏该文
已收藏

评论内容:(最多支持255个字符)