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 语句的痛苦.例如拼接时要确保不能忘记添加必要的空 ...
随机推荐
- 使用C#委托来实现异步编程
最近在我参与的几个.Net项目中都有用到异步编程,作为一名.Net小白,很有必要好好地学习一下异步编程. 什么是异步编程 异步编程指的就是不用阻塞当前线程来等待任务的完成,而是将任务扔到线程池中去执行 ...
- 手机UA识别
整理手机UA识别如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- javaweb带属性的自定义标签
带属性的自定义标签: 1.先在标签处理器中定义setter方法,建议把所有的属性类型都设置为String类型. package com.javaweb.tag; import java.io.IOEx ...
- 使用 WireShark 分析 TCP/IP 三次握手 和 四次挥手
TCP 三次握手 示意图 Wireshark 抓包注意事项 为了演示一个TCP三次握手建立连接的过程,我们通过 Chrome 访问一个网页. 已知 HTTP 协议就是建立在TCP链接上的 比如访问以下 ...
- SZU 7
A - Megacity sqrtf是个坑 #include <iostream> #include <string> #include <cstring> #in ...
- Maven 配置tomcat和findbug插件(在eclipse建立的项目中)
tomcat插件 a) tomcat的maven插件可以在tomcat的官网上寻找,这就是tomcat插件的plugin b) 将tomcat的plugin配置到项目的po ...
- laravel JWT Auth - JSON Web令牌认证API
https://github.com/tymondesigns/jwt-auth/wiki
- mysql 查询近几天的结果 按每天的粒度查询
),DATE_FORMAT(FROM_UNIXTIME(createtime), '%Y-%m-%d') as time from bskuser group by time
- 洛谷P1155 双栈排序(贪心)
题意 题目链接 Sol 首先不难想到一种贪心策略:能弹则弹,优先放A 然后xjb写了写发现只有\(40\),原因是存在需要决策的情况 比如 \(A = {10}\) \(B = {8}\) 现在进来一 ...
- jquery插件一直报错:xx is not a function
当然像js文件未引用或者js插件使用方法不对这样的解决办法想必大家都已经试过了. 那么在放弃前请最后看一下是不是引用了两个jquery文件. 引用了两个jquery文件! 引用了两个jquery文件! ...