mybatis中的动态SQL(IF Chooes When Where Set ForEach SQL片段)
mapper:
public interface BlogMapper {
List<Blog> getBlogByIF(Map map);
}
IF
<select id="getBlogByIF" resultType="blog" parameterType="map">
select * from mybatis.blog
<where>
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</select>
if标签会根据你传入的值去匹配符合条件的if语句
where标签会根据你的传入的值动态的帮助你去掉and
比如:
你传入了title="xxx",那么它的SQL语句会是 select * from blog where title = (你传入的值)
SQL片段
SQL片段是放置重复的语句,提高重用性
例如上述的代码中的
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
此时,我们可以用<sql></sql>标签 id是唯一标识
<sql id="select">
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
则我们的查询语句可以简化成下面的样子
<select id="getBlogByIF" resultType="blog" parameterType="map">
select * from mybatis.blog
<include refid="select"></include>
</select>
refid就是引用上述sql标签中的唯一标识
choose when otherwise
mapper:
public interface BlogMapper {
List<Blog> getBlogByChoose(Map map);
}
<select id="getBlogByChoose" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<choose>
<when test="title!=null">
title = #{title}
</when>
<when test="author!=null">
and author = #{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>
其实就是相当于java中的switch语句
传入了 “title” 就按 “title” 查找,传入了 “author” 就按 “author” 查找的情形。若两者都没有传入,就返回标记为按“views”查找的 BLOG
Set
mapper:
public interface BlogMapper {
int updateBlog(Map map);
}
<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>
<set>标签会动态的帮你去掉逗号
ForEach
mapper:
public interface BlogMapper {
List<Blog> getBlogForEach(Map map);
}
//原始的sql为
//select * from mybatis.blog where (id=1 or id=2 or id=3);
<select id="getBlogForEach" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<foreach collection="list" item="id" open="(" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
测试类:
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Map map = new HashMap();
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
map.put("list",list);
List<Blog> blogs = mapper.getBlogForEach(map);
for (Blog blog : blogs) {
System.out.println(blog);
} sqlSession.close();
}
foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。
提示
你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach。
当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。
mybatis中的动态SQL(IF Chooes When Where Set ForEach SQL片段)的更多相关文章
- 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)
动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些 ...
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- mybatis中的动态SQL
在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...
- mybatis 05: mybatis中的动态代理
mybatis中动态代理存在的意义 图示 图示分析 分层说明:界面层为第一层,业务逻辑层(接口层 + 实现层)为第二层,数据访问层(接口层 + 实现层)为第三层 业务逻辑层和数据访问层:分别分两层来开 ...
- mybatis中的动态SQL语句
有时候,静态的SQL语句并不能满足应用程序的需求.我们可以根据一些条件,来动态地构建 SQL语句. 例如,在Web应用程序中,有可能有一些搜索界面,需要输入一个或多个选项,然后根据这些已选择的条件去执 ...
- mybatis中实现动态SQL
动态SQL语句,也就意味着SQL语句不在是一成不变的而是具有多样性. if if的用法还是跟平常差不多的(不过没有else if也没有else) <update id="modify& ...
- 阶段3 1.Mybatis_08.动态SQL_01.mybatis中的动态sql语句-if标签
创建新的工程 复制到新建的项目里面 pom.xml依赖部分复制过来 dao中整理代码 只保留四个查询 映射文件也只保留四个查询方法 增加一个根据条件查询的方法. 由于用了别名,所以parpameter ...
- mybatis中的动态代理应用(mapper对象)
-----------------UserMapper的配置信息--------------------- <?xml version="1.0" encoding=&quo ...
- mybatis中#{}与${}的差别(如何防止sql注入)
默认情况下,使用#{}语法,MyBatis会产生PreparedStatement语句中,并且安全的设置PreparedStatement参数,这个过程中MyBatis会进行必要的安全检查和转义. # ...
随机推荐
- HTML之前端组成、标签
详情见:https://www.cnblogs.com/liwenzhou/p/7988087.html https://www.cnblogs.com/zhangguosheng1121/p/109 ...
- chrome Provisional headers are shown错误提示(转载)
今天开发时遇到了一个问题,由于要做一个支付等待页,大概的意思就是点击支付之后,跳出来一个页面,告知用户正在跳转到支付页面.这个时候问题来了,指鹤要做的这个静态支付等待页中有图片,而为了要让这个静态页面 ...
- 全网最全最细的appium自动化测试环境搭建教程以及appium工作原理
一.前言 对于appium自动化测试环境的搭建我相信90%的自学者都是在痛苦中挣扎,在挣扎中放弃,在放弃后又重新开始,只有10%的人,人品比较好,能够很快并顺利的搭建成功.appium 自动化测试 ...
- 算法笔记刷题1(codeup 1934)
准备6月份的拼题甲级中(本来现在这两天就考试了,但是因为疫情的原因延期了) 刚刚开始按算法笔记刷题,今天是探索codeup的第一天. 一开始并没有把多点测试当回事,直到一错再错,心态爆炸... 附上我 ...
- thinkphp5.0.x
payload5.0.24 http://-----/index.php?s=index/think\app/invokefunction&function=call_user_func_ar ...
- json格式的相互转化
直接上代码: header("Content-type: text/html; charset=utf-8"); $arr = array(); $arr = [ ', ', ' ...
- qt tableview 选择模式
QAbstractItemView::SingleSelection QAbstractItemView::ContiguousSelection QAbstractItemView::Extende ...
- Zookeeper之Error contacting service. It is probably not running.
安装ZooKeeper时,无论是修改zoo.cfg:还是myid,都检查了几遍都没有错误.但是开启Zookeeper服务时出现: Error contacting service. It is pro ...
- if __name=='__main__"的作用
1.__main__的作用 我们可以经常在不同的程序和脚本中看到有这样的代码: if __name__=='__main__':#如果在windows上启动线程池,必须要使用. func() 很多情况 ...
- 使用BottomNavigationView+ViewPager+Fragment的底部导航栏
2019独角兽企业重金招聘Python工程师标准>>> 使用BottomNavigationView做底部工具栏,使用ViewPager做页面切换,使用Fragment完成每个页面的 ...