本次全部学习内容:MyBatisLearning

 
什么是动态sql:
    mybatis的核心,对sql进行灵活的操作,通过对表达式的判断,对sql灵活的拼接

在之前小案例的基础上我们先进行简单的实现一下:

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

测试类:
//此时值传入username这一个值
     //动态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=河南郑州]
在if中判断此时,之传入了一个username进行查询。

SQL片段:

把实现动态sql判断的代码块抽取出来,组成一个sql片段,在需要的时候可以直接引用,重用性高
 
定义一个sql片段,基于上面的代码进行测试和实践:
在UserMapper.xml文件中:
定义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

select from user where id = 1 or id = 10 or id = 12
在UserMapper.xml文件中,对之前的代码进行加工修改:
此时,在测试的时候,我只是测试foreach里面的内容,所以,对代码进行了修改
     <!-- 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=安徽]
对于
SELECT * FROM USER WHERE id IN(1,2,30)来说
只需要修改下面的测试代码,其余的测试代码均不变
<foreach collection="ids" close=")"  item="userId" open="1=1 and id in(" separator=",">
id=#{userId}
</foreach>
sql只接收一个数组参数,这时sql解析参数的名称mybatis固定为array,如果数组是通过一个pojo传递到sql则参数的名称为pojo中的属性名。
index:为数组的下标。
item:为数组每个元素的名称,名称随意定义
open:循环开始
close:循环结束
separator:中间分隔输出
 
 
 
 
 
 
 
 
 
 
 
 
 
 

MyBatis框架(6)动态sql的更多相关文章

  1. mybatis框架(5)---动态sql

    那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误.Mybatis的动态S ...

  2. mybatis框架中动态SQL的编写

    1.动态SQL:在SQL语句中加入流程控制.比如加入if,foreach等. 重点掌握if语句: 案例1: <update id="updateItem" parameter ...

  3. 【mybatis深度历险系列】mybatis中的动态sql

    最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...

  4. JavaWeb_(Mybatis框架)Mapper动态代理开发_三

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  5. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  6. mybatis 详解------动态SQL

    mybatis 详解------动态SQL   目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...

  7. mybatis中的动态SQL

    在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...

  8. Mybatis映射文件动态SQL语句-01

    因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...

  9. MyBatis进阶使用——动态SQL

    MyBatis的强大特性之一就是它的动态SQL.如果你有使用JDBC或者其他类似框架的经验,你一定会体会到根据不同条件拼接SQL语句的痛苦.然而利用动态SQL这一特性可以彻底摆脱这一痛苦 MyBati ...

  10. mybatis教程4(动态SQL)

    动态SQL语句 MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空 ...

随机推荐

  1. CommandLineRunner预加载数据

    在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–CommandLineRunner. CommandLineRunner是一个接 ...

  2. 百度富文本Ueditor将图片存在项目外路径并回显

    我的毕设中需要一个类似新闻发布的功能,使用到百度富文本编辑器,不过百度富文本编辑器有点坑(只是我太菜了),粘贴图片和回显这个坑坑了我两天时间.效果是这样的: 就是可以在文本中粘贴图片并显示出来,直接说 ...

  3. phpmyadmin数据表结构没有显示注释列

    新开的一个项目,用phpmyadmin作为图形化操作数据库工具.创建数据表时为其每列添加好注释,浏览数据表内容有显示注释内容,但是查看数据表结构没有显示注释列,不方便直观查看数据表每列的意思. 上网搜 ...

  4. Java xml 操作(Dom4J修改xml   + xPath技术  + SAX解析 + XML约束)

    1 XML基础 1)XML的作用 1.1 作为软件配置文件 1.2 作为小型的"数据库" 2)XML语法(由w3c组织规定的) 标签: 标签名不能以数字开头,中间不能有空格,区分大 ...

  5. Perl学习笔记(1)----入门

    在UNIX/Linux 系统上,打开命令终端,输入 'rpm -q perl' 查看系统是否安装了 perl ---- 在自己的CentOS7 系统上,默认自带了 perl 软件: root@javi ...

  6. Windows核心编程(第5版)----关闭内核对象

    无论怎样创建内核对象,都要向系统指明将通过调用 CloseHandle 来结束对该对象的操作: BOOL CloseHandle(HANDLE hobj); 该函数首先检查调用进程的句柄表,以确保传递 ...

  7. Python爬虫教程-27-Selenium Chrome版本与chromedriver兼容版本对照表

    我们使用Selenium+Chrome时,版本不一样, 会导致 chromedriver 停止运行 chromedriver 所有版本下载链接:http://npm.taobao.org/mirror ...

  8. 数据结构----线性表顺序和链式结构的使用(c)

    PS:在学习数据结构之前,我相信很多博友也都学习过一些语言,比如说java,c语言,c++,web等,我们之前用的一些方法大都是封装好的,就java而言,里面使用了大量的封装好的方法,一些算法也大都写 ...

  9. maven学习(四)maven的生命周期

    官网:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html maven有三套相互独立的生命周期, ...

  10. 深入理解java的形参和实参

    转载声明:本文转载自公众号「码匠笔记」. 前几天在头条上看到一道经典面试题,引发了一些思考.也是写这篇文章的导火索. 背景 请看题: public    classMain{    publicsta ...