首页 技术 正文
技术 2022年11月20日
0 收藏 437 点赞 2,722 浏览 1913 个字

动态 SQL

目的:为了摆脱在不同条件拼接 SQL 语句的痛苦

在不同条件在生成不同的SQL语句

本质上仍然是SQL语句,不过是多了逻辑代码去拼接SQL,只要保证SQL的正确性按照格式去排列组合

可以先写好SQL语句

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

if,          where(可以自动去除多余的and)

    <select id="queryBlog_if"  parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<if test="title !=null">
and title=#{title}
</if>
<if test="author !=null">
and author=#{author}
</if>
</where>
</select>

choose(when, otherwise)

从多个条件中选择一个使用,有点像 Java 中的 switch 语句

    <select id="queryBlog_choose" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<choose>
<when test="title !=null ">
title=#{title}
</when>
<when test="author!=null ">
author=#{author}
</when>
<!-- <otherwise>-->
<!-- and views=#{views}-->
<!-- </otherwise>-->
<!-- 没有otherwise的时候可以不用传值,有的话得至少传一个值--> </choose>
</where>
</select>

set(可以去除多余的逗号,  )    [update]

    <update id="updateBlog" parameterType="map" >
update mybatis.blog
<set>
<if test="title != null">
title=#{title},
</if>
<if test="author != null">
author=#{author},
</if></set>
where id=#{id}
</update>

SQL片段(提取共有的SQL语句,达到便捷复用的作用)  [尽量只要if判断的就行]

    <sql id="oo_sb">
<if test="title != null">
title=#{title},
</if>
<if test="author != null">
author=#{author},
</if>
</sql><!-- 然后就用下面方式,写到原有的位置--> <include refid="oo_sb"></include>

   foreach

    <select id="queryBlogget1_3" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id_"
open="id in (" separator="," close=")">
#{id_}
</foreach>
</where>
</select>SQL:select * from mybatis.blog where id in(1,2,3)ids=通过万能map传入的集合名称
item=遍历的每个元素的值
open 前缀
separator 分隔
close 后缀
#{iid} 前缀和后缀里面的东西 等于遍历的元素
collection取值不用加#{}方法二: <select id="queryBlogget1_3" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id_"
open=" (" separator="or" close=")">
id=#{id_}
</foreach>
</where>
</select>SQL:select * from mybatis.blog where(id=? or id=?)

//测试代码片段        HashMap map = new HashMap();
ArrayList<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(3);
map.put("ids",ids);
List<Blog> blogs= mapper.queryBlogget1_3(map);
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902