MyBatis框架(6)动态sql
本次全部学习内容:MyBatisLearning
在之前小案例的基础上我们先进行简单的实现一下:
if:
在UserMapper.xml文件中找到:
<!-- 动态sql -->
<!-- 综合查询 -->
<select id="findBySelect" parameterType="com.MrChengs.po.UserView" resultType="com.MrChengs.po.UserCustomer" >
select * from user
<where>
<if test="userCustomer!=null">
<if test="userCustomer.sex!=null and userCustomer.sex!='' ">
and user.sex=#{userCustomer.sex}
</if>
<if test="userCustomer.username!=null and userCustomer.username!=''">
and user.username like '%${userCustomer.username}%'
</if>
</if>
</where>
</select>
注意:where标签可以自动去掉条件中的第一个 and
//动态sql
//高级查询
@Test
public void testfindBySelect() throws Exception{
SqlSession sqlSession = getSqlSessionFactory().openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); UserView userView = new UserView(); UserCustomer userCustomer = new UserCustomer();
//userCustomer.setSex(1);
userCustomer.setUsername("小明"); userView.setUserCustomer(userCustomer); List<User> user = mapper.findBySelect(userView);
for(User u : user){
System.out.println(u);
}
sqlSession.close();
}
结果:
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@545997b1]
DEBUG [main] - ==> Preparing: select * from user WHERE user.username like '%小明%'
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 3
User [id=16, username=张小明, birthday=null, sex=1, address=河南郑州]
User [id=22, username=陈小明, birthday=null, sex=1, address=河南郑州]
User [id=25, username=陈小明, birthday=null, sex=1, address=河南郑州]
SQL片段:
<!-- sql片段 -->
<!-- id唯一,是sql片段的唯一标识 -->
<!-- 基于单表定义sql片段,这样的话sql片段的可重用性才高,在sql片段中不要包括where -->
<sql id="selectBySql">
<if test="userCustomer!=null">
<if test="userCustomer.sex!=null and userCustomer.sex!='' ">
and user.sex=#{userCustomer.sex}
</if>
<if test="userCustomer.username!=null and userCustomer.username!=''">
and user.username like '%${userCustomer.username}%'
</if>
</if>
</sql>
引用sql片段:
<!-- 动态sql -->
<!-- 综合查询 -->
<select id="findBySelect" parameterType="com.MrChengs.po.UserView" resultType="com.MrChengs.po.UserCustomer" >
select * from user <where>
<!-- 引用sql片段 -->
<include refid="selectBySql"></include>
</where>
</select>
测试代码同上次测试代码!
foreach标签:
假设我们同时查询多个id
<!-- sql片段 -->
<!-- id唯一,是sql片段的唯一标识 -->
<!-- 基于单表定义sql片段,这样的话sql片段的可重用性才高,在sql片段中不要包括where -->
<sql id="selectBySql">
<if test="userCustomer!=null">
<if test="userCustomer.sex!=null and userCustomer.sex!='' ">
and user.sex=#{userCustomer.sex}
</if>
<if test="userCustomer.username!=null and userCustomer.username!=''">
and user.username like '%${userCustomer.username}%'
</if>
</if> <!-- foreach -->
<!-- 测试 -->
<!-- select from user where id = 1 or id = 10 or id = 12 --> <!-- collection:指定输入对象的集合 -->
<!-- item:每个遍历生成成的对象 -->
<!-- open:开始遍历时 拼接的串 -->
<!-- close:结束遍历时 拼接的串 -->
<!-- separator:遍历时两个对象中需要拼接的串 -->
<foreach collection="ids" close=")" item="userId" open="1=1 and (" separator="or">
id=#{userId}
</foreach>
</sql> <!-- 动态sql -->
<!-- 综合查询 -->
<select id="findBySelect" parameterType="com.MrChengs.po.UserView" resultType="com.MrChengs.po.UserCustomer" >
select * from user <where>
<!-- 引用sql片段 -->
<include refid="selectBySql"></include>
</where> </select>
测试代码:
//foreach
//动态sql
//高级查询
@Test
public void testfindBySelect() throws Exception{
SqlSession sqlSession = getSqlSessionFactory().openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); UserView userView = new UserView();
//foreach
List<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(30); userView.setIds(ids); List<User> users = mapper.findBySelect(userView);
for(User user : users){
System.out.println(user);
}
sqlSession.close();
}
结果:
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@545997b1]
DEBUG [main] - ==> Preparing: select * from user WHERE 1=1 and ( id=? or id=? or id=? )
DEBUG [main] - ==> Parameters: 1(Integer), 2(Integer), 30(Integer)
DEBUG [main] - <== Total: 2
User [id=1, username=王五, birthday=null, sex=2, address=null]
User [id=30, username=Ma, birthday=null, sex=1, address=安徽]
<foreach collection="ids" close=")" item="userId" open="1=1 and id in(" separator=",">
id=#{userId}
</foreach>
MyBatis框架(6)动态sql的更多相关文章
- mybatis框架(5)---动态sql
那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误.Mybatis的动态S ...
- mybatis框架中动态SQL的编写
1.动态SQL:在SQL语句中加入流程控制.比如加入if,foreach等. 重点掌握if语句: 案例1: <update id="updateItem" parameter ...
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- JavaWeb_(Mybatis框架)Mapper动态代理开发_三
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- Mybatis入门之动态sql
Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...
- mybatis 详解------动态SQL
mybatis 详解------动态SQL 目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...
- mybatis中的动态SQL
在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...
- Mybatis映射文件动态SQL语句-01
因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...
- MyBatis进阶使用——动态SQL
MyBatis的强大特性之一就是它的动态SQL.如果你有使用JDBC或者其他类似框架的经验,你一定会体会到根据不同条件拼接SQL语句的痛苦.然而利用动态SQL这一特性可以彻底摆脱这一痛苦 MyBati ...
- mybatis教程4(动态SQL)
动态SQL语句 MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空 ...
随机推荐
- JVM(五) class类文件的结构
概述 class类文件的结构可见下面这样图(出处见参考资料),可以参照下面的例子,对应十六进制码,找出找出相应的信息. 其中u2 , u4 表示的意思是占用两个字节和占用四个字节,下面我们将会各项说明 ...
- svn在commit后报错:is scheduled for addition, but is missing
今天通过svn 的cr(code review)代码审核后,我欲执行svn ci -m"xxxxxxx(提交注释) ISSUE=3380305",但是没有提交成功,SVN报错啦! ...
- 一、快速构建Springboot应用
1.基本概念 Spring的出现对于企业级应用来说是一个福音,它让企业级应用开发更加地简单.但是随着Spring的不断发展,它也慢慢变得越来越重.即使apache出品的maven工具能够使得项目创建. ...
- Java基础(八)异常处理
一.异常 异常是指程序在运行过程产生的不正常情况.语法错误不算异常. 1.异常体系 Throwable:Java语言中所有错误或异常的超类:Throwable 类的子类有两个:1.Error:2.Ex ...
- git基本命令集合
以下内容不适合初学者 括号中表示需要自己填写 v1.0 git add git commit -m git commit -a -m git commit -amend git clone git l ...
- Js的核心:找到DOM
掌握 JavaScript 的核心之一:DOM,能够熟悉DOM相关操作,了解JavaScript事件机制 一.使用getElementById().getElementsByTagName().chi ...
- 归并排序算法Matlab实现
Matlab一段时间不用发现有些生疏了,就用归并排序来练手吧.代码没啥说的,百度有很多.写篇博客,主要是记下matlab语法,以后备查. 测试代码 srcData = [1,3,2,4,6,5,8 ...
- MySQL数据库(1)----入门级操作
1.在服务器主机上以 root 用户登陆,创建位于其他客户端的新用户: mysql> CREATE USER 'newuser'@'192.168.1.109' IDENTIFIED BY 'p ...
- C#开发小技巧
001.判断一个Form是否已关闭并释放,需要从引用和对象两方面来判断,判断引用是否为null:mainfm==null判断引用的对象是否已释放:mainfm.IsDisposedMainFormma ...
- vue2.0路由写法、传参和嵌套
前置知识请戳这里 vue-routerCDN地址:https://unpkg.com/vue-router@3.0.1/dist/vue-router.js vue-router下载地址:https: ...