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 语句的痛苦.例如拼接时要确保不能忘记添加必要的空 ...
随机推荐
- [转]Supporting OData Query Options in ASP.NET Web API 2
本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/suppor ...
- 【读书笔记】读《编写可维护的JavaScript》 - 编程风格(第一部分)
之前大致翻了一遍这本书,整体感觉很不错,还是不可追求快速,需要细细理解. 这篇随笔主要对本书的第一部分中对自己触动比较大的部分及与平常组织代码最为息息相关的部分做一个记录,加深印象. 主要讲述五点内容 ...
- css样式美化 下拉框 select 样式
<span class="setleft wid80"><span class="fyhbx">*</span>入库类型 : ...
- golang学习之闭包
匿名函数不能够独立存在,但可以被赋值于某个变量,即保存函数的地址到变量中:fplus := func(x, y int) int { return x + y },然后通过变量名对函数进行调用:fpl ...
- Java生成验证码(二)
前一篇博客已经介绍了如何用Java servlet产生验证码,本篇继续介绍如何使用一些开源组件生成验证码 ———————————————————————————————————————————— ...
- linux修改用户名和密码
linux修改用户名和密码 修改root密码:sudo passwd root 修改用户密码(如hadoop) sudo passwd hadoop 修改主机名:sudo vi /etc/hostna ...
- 对Mybatis的初步认识
1.认识Mybatis MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索. MyBat ...
- JBPM学习第4篇:10分钟熟悉Eclipse
http://download.jboss.org/jbpm/videos/7.0.0.Final_eclipse_getting_started.swf 打开视频练习 1.导入 evaluation ...
- Spring 框架(一)
1 spring框架概述 1.1 什么是spring l Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert ...
- Tesseract-OCR -01-Tesseract 介绍
Tesseract-OCR -01-Tesseract 介绍 OCR(Optical Character Recognition): 光学字符识别,是指对图片文件中的文字进行分析识别,获取的过程 Te ...