Mybatis映射文件(3)

àì夳堔傛蜴生んèń 2022-05-24 04:16 296阅读 0赞

动态SQL

1、if/where
  1. <!--
  2. test:判断表达式(OGNL自己去查怎么用)
  3. test="id != null":从参数中取值进行判断
  4. 拼装的时候有的条件没带可能导致sql拼装会有问题
  5. 1、给where后面加上1=1,以后的条件都and
  6. 2、mybatis使用<where>标签来将所有查询条件包括,
  7. mybatis就会将where标签中拼装的sql多出来的and或者or去掉
  8. where只会去掉第一个多出来的and或者or
  9. -->
  10. <select id="getEmpsByConditionIfAndWhere" resultType="bean.Employee">
  11. SELECT * FROM t_employee
  12. <where>
  13. <if test="id != null">
  14. id=#{id}
  15. </if>
  16. <if test="lastName != null and lastName != ''">
  17. AND last_name=#{lastName}
  18. </if>
  19. <if test="email != null and email.trim() != ''">
  20. AND email=#{email}
  21. </if>
  22. <if test="gender==0 or gender==1">
  23. AND gender=#{gender}
  24. </if>
  25. </where>
  26. </select>
2、trim自定义字符串截取规则
  1. <!--
  2. 若将and放在每个条件之后,<where>标签不能解决拼接的问题
  3. trim:自定义字符串截取规则
  4. prefix="":前缀:trim标签中是整个字符串拼串后的结果
  5. prefix给拼串后的整个字符串加一个前缀
  6. prefixOverrides="":前缀覆盖,去掉整个字符串前面多余的字符
  7. suffix="":后缀:
  8. suffix给拼串后的字符串加一个后缀
  9. suffixOverrides="":后缀覆盖,去掉整个字符串后面多余的
  10. -->
  11. <select id="getEmpsByConditionTrim" resultType="bean.Employee">
  12. SELECT * FROM t_employee
  13. <trim prefix="where" suffixOverrides="and">
  14. <if test="id != null">
  15. id=#{id} AND
  16. </if>
  17. <if test="lastName != null and lastName != ''">
  18. last_name=#{lastName} AND
  19. </if>
  20. <if test="email != null and email.trim() != ''">
  21. email=#{email} AND
  22. </if>
  23. <if test="gender==0 or gender==1">
  24. gender=#{gender}
  25. </if>
  26. </trim>
  27. </select>
3、choose (when, otherwise):分支选择
  1. <!--
  2. 如果带了id就用id查,如果带了lastName就用lastName查,只会进入其中一个
  3. -->
  4. <select id="getEmpsByConditionChoose" resultType="bean.Employee">
  5. SELECT * FROM t_employee
  6. <where>
  7. <choose>
  8. <when test="id!=null">
  9. id=#{id}
  10. </when>
  11. <when test="lastName!=null and lastName!=''">
  12. last_name LIKE #{lastName}
  13. </when>
  14. <when test="email!=null and email!=''">
  15. email=#{email}
  16. </when>
  17. <otherwise>
  18. 1=1
  19. </otherwise>
  20. </choose>
  21. </where>
  22. </select>
4、where-封装查询条件, set-封装修改的条件
  1. <update id="updateEmp">
  2. UPDATE t_employee
  3. <set>
  4. <if test="lastName!=null and lastName!=''">
  5. last_name=#{lastName},
  6. </if>
  7. <if test="email!=null and email!=''">
  8. email=#{email},
  9. </if>
  10. <if test="gender!=null">
  11. gender=#{gender}
  12. </if>
  13. </set>
  14. <where>
  15. id=#{id}
  16. </where>
  17. <!-- UPDATE t_employee
  18. <trim prefix="set" suffixOverrides=",">
  19. <if test="lastName!=null and lastName!=''">
  20. last_name=#{lastName},
  21. </if>
  22. <if test="email!=null and email!=''">
  23. email=#{email},
  24. </if>
  25. <if test="gender!=null">
  26. gender=#{gender}
  27. </if>
  28. </trim>
  29. <where>
  30. id=#{id}
  31. </where>-->
  32. </update>
5、foreach标签——批量处理
  1. <!--
  2. collection:指定要遍历的集合
  3. list类型的参数会特殊处理封装在map中,map的key就是list
  4. item:将当前遍历出的元素赋值给指定的变量
  5. #{变量名}:就能取出变量的值,也就是当前遍历出的元素
  6. separator:每个元素之间的分隔符
  7. open/close:遍历出所有结果拼接一个开始和结束的字符(整个语句以什么开始和什么结束)
  8. index:索引:遍历list的时候就是索引,item就是当前值
  9. 遍历map的时候就是map的key,item就是map的value
  10. -->
  11. <select id="getEmpsByConditionForeach" resultType="bean.Employee">
  12. SELECT * FROM t_employee WHERE id IN
  13. <foreach collection="list" item="item_id" separator="," open="(" close=")">
  14. #{item_id}
  15. </foreach>
  16. </select>
  17. <!--批量保存-->
  18. <!--MySQL下批量保存,可以foreach遍历values-->
  19. <!--<insert id="addEmps">
  20. INSERT INTO t_employee(id,last_name,gender,email) VALUES
  21. <foreach collection="emps" item="emp" separator=",">
  22. (#{emp.id},#{emp.lastName},#{emp.gender},#{emp.email})
  23. </foreach>
  24. </insert>-->
  25. <!--该种方法会有语法异常,需要开启sql语句间用“;”分隔的权限—allowMultiQueries=true-->
  26. <insert id="addEmps">
  27. <foreach collection="emps" item="emp" separator=";">
  28. INSERT INTO t_employee(id,last_name,gender,email) VALUES
  29. (#{emp.id},#{emp.lastName},#{emp.gender},#{emp.email})
  30. </foreach>
  31. </insert>
6、内置参数和bind
  1. <!--
  2. 两个内置参数:
  3. 不只是方法传递过来的参数可以被用来判断、取值
  4. mybatis默认还有两个内置参数:
  5. _parameter:代表整个参数
  6. 单个参数:_parameter就是这个参数
  7. 多个参数:参数会被封装为一个map,_parameter就是代表这个map
  8. _databaseId:如果配置了DatabaseIdProvider标签
  9. _databaseId就是代表当前数据库的别名
  10. bind:可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值
  11. <bind name="_lastName" value="'%'lastName'%'"></bind>这样写了就可以不用在传入参数的时候写%号
  12. -->
  13. <select id="getEmpsTestInnerParameter" resultType="bean.Employee">
  14. <bind name="_lastName" value="'%'lastName'%'"></bind>
  15. <if test="_databaseId=='mysql'">
  16. SELECT * FROM t_employee
  17. <if test="_parameter!=null">
  18. WHERE last_name LIKE #{_lastName}
  19. </if>
  20. </if>
  21. <if test="_databaseId=='oracle'">
  22. SELECT * FROM employee
  23. <if test="_parameter!=null">
  24. WHERE last_name=#{lastName}
  25. </if>
  26. </if>
  27. </select>
7、可重用SQL——经常将要查询的列名或插入用的列名抽取出来方便引用
  1. <!--
  2. 1、sql标签抽取可重用的sql片段,方便后面引用
  3. 里面还可以做动态判断,经常将查询和插入的列名抽取出来
  4. 2、include标签就是引用从外部定义的sql
  5. 3、include还可以自定义有些property,sql标签内部就能使用自定义的属性,取值的方式${pro},#{}不可以
  6. -->
  7. <sql id="insertColumn">
  8. <if test="_databaseId=='mysql'">
  9. id,last_name,gender,email
  10. </if>
  11. <if test="_databaseId=='oracle'">
  12. --------
  13. </if>
  14. </sql>
  15. <insert id="addEmps">
  16. <foreach collection="emps" item="emp" separator=";">
  17. INSERT INTO t_employee(
  18. <include refid="insertColumn"></include>
  19. ) VALUES
  20. (#{emp.id},#{emp.lastName},#{emp.gender},#{emp.email})
  21. </foreach>
  22. </insert>

发表评论

表情:
评论列表 (有 0 条评论,296人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MyBatis3:SQL映射

    前言 前面学习了config.xml,下面就要进入MyBatis的核心SQL映射了,第一篇文章的时候,student.xml里面是这么写的: ![复制代码][copycod

    相关 Mybatis-映射文件

    Mybatis-映射文件(二) Mybatis的真正强大在于它的映射语句,也是它的魔力所在。由于它的异常强大,映射器XML文件就显得相对简单。如果拿他跟具有相同功能的JD