Mybatis - 动态sql
learn from:http://www.mybatis.org/mybatis-3/dynamic-sql.html
mybatis支持动态拼接sql语句。主要有:
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
1.if
首先看基本实例:
- <select id="findActiveBlogWithNameLike" resultType="Blog">
- SELECT * FROM blog
- WHERE state = 'active'
- <if test="name != null">
- AND name LIKE #{name}
- </if>
- </select>
- List<Blog> findActiveBlogWithNameLike(String name);
这里遇到一个问题:
- There is no getter for property named 'name' in 'class java.lang.String'###
Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'name' in 'class java.lang.String'
也就是说,mybatis将name当做输入参数的一个属性,并且期望通过getter方法来获取它的值。很容易想到,将输入参数改成Blog就可以了。
然而,这并不符合我们的查询习惯,比如,如果是Blog就必须这样查询:
- @Test
- public void testFindActiveBlogWithNameLike() throws Exception{
- Blog key = new Blog();
- key.setName("test%");
- List<Blog> blogs = mapper.findActiveBlogWithNameLike(key);
- System.out.println(blogs);
- return;
- }
为了一个String字段而创建一个类,看着要多别扭有多别扭。当然,前提是我们仅仅是查询name条件,所以会觉得其他属性冗余。如果是多条件查询,那么Blog必然是最好的选择。那么,仅仅传入String的话应该也是可以的。参考1,参考2。
第一种做法是简单类型都是使用_parameter来代替。
- <select id="findActiveBlogWithNameLikeByString" parameterType="java.lang.String" resultType="Blog">
- SELECT * FROM blog
- WHERE name LIKE '%${_parameter}%'
- </select>
第二种做法比较容易理解,在方法参数前添加@Param(value="xxx")注解来使用xxx作为传入参数。
- <select id="findActiveBlogWithNameLikeByString" parameterType="java.lang.String" resultType="Blog">
- SELECT * FROM blog
- WHERE name LIKE '%${key}%'
- </select>
- List<Blog> findActiveBlogWithNameLikeByString(@Param(value = "key") String key);
两种做法均可,所以,看你喜欢了,是想要省事简洁还是通俗易读。在这里,还是选择第0种方案,即传入Blog对象来作为查询条件。
下面简单介绍if的语法:
- if节点中,属性test是一个boolean值,为true的时候将拼接if里的sql语句。
- 参数值是可以包含一些掩码或通配符的.比如通配符%和占位符_
所以,很简单很容易理解。
场景一: 查询blog的名字name like %Insert and 作者author的username like Ryan%.
首先看期望的结果,blog表中有三条满足name like:
- mysql> select * from blog;
- +----+------------+-----------+--------------+--------+
- | id | name | author_id | co_author_id | state |
- +----+------------+-----------+--------------+--------+
- | 1 | test | 3 | 4 | active |
- | 8 | testInsert | 4 | 5 | active |
- | 9 | testInsert | 5 | 6 | active |
- | 10 | testInsert | 6 | 7 | active |
- | 12 | testA | 50 | NULL | active |
- | 13 | testA | 51 | NULL | active |
- | 14 | testInsert | NULL | NULL | active |
- +----+------------+-----------+--------------+--------+
- 7 rows in set
这三条中,满足author的username like的有两条:
- mysql> select author.id, author.username from author where id in (4,5,6);
- +----+----------+
- | id | username |
- +----+----------+
- | 4 | Ryan |
- | 5 | Ryan0 |
- | 6 | Leslie |
- +----+----------+
- 3 rows in set
也就是我们最终希望结果是blog id为8 和 9。
mybatis的sql语句如下:
- <select id="findActiveBlogLike" resultType="Blog">
- SELECT * FROM blog b, author a
- WHERE state = 'active'
- <if test="name != null">
- AND name LIKE #{name}
- </if>
- AND b.author_id = a.id
- <if test="author != null and author.name != null">
- AND a.username LIKE #{author.username}
- </if>
- </select>
当blog的name不为null的时候查询name匹配,当author的username不为null的时候,查询author的username匹配。
- 第一个if节点的test为name,这个会查找Blog的name字段,如果传入参数Blog没有name字段,那么就会像我们开始那样报错。所以,name必须是blog的一个字段。同理,#{name}这个也要和blog字段字面量的值匹配。
- 第二个if节点的test里看到了and,and就是并且。首先判断author是否为null,就是判断Blog对象的author属性是否为null。接着判断author.name是否为null,这里就有点问题了。因为我的Author类中并没有name字段,对应的字段字面量是username。也就是说这里应该是author.username。但我粗心写成了author.name(所以为每段代码编写unit test是多么的重要)。更奇葩的是,这条test通过了判断为真,这里先不讲,后面测试的时候再分析原因。不过这里一定要改成author.username才是正确的做法。
对应的java接口:
- List<Blog> findActiveBlogLike(Blog blog);
下面开始测试:
- @Test
- public void testFindActiveBlogLike() throws Exception{
- Blog blog = new Blog();
- blog.setName("%Insert");
- Author author = new Author();
- author.setUsername("Ryan%");
- blog.setAuthor(author);
- List<Blog> blogs = mapper.findActiveBlogLike(blog);
- System.out.println(blogs);
- assertTrue(blogs.size()==2);
- return;
- }
先看结果对不对:
- 2016-08-06 16:20:19,264 DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Opening JDBC Connection
- 2016-08-06 16:20:19,740 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] - Created connection 1150284200.
- 2016-08-06 16:20:19,742 DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@448ff1a8]
- 2016-08-06 16:20:19,745 DEBUG [com.test.mapper.dao.BlogMapper.findActiveBlogLike] - ==> Preparing: SELECT * FROM blog b, author a WHERE state = 'active' AND name LIKE ? AND b.author_id = a.id AND a.username LIKE ?
- 2016-08-06 16:20:19,888 DEBUG [com.test.mapper.dao.BlogMapper.findActiveBlogLike] - ==> Parameters: %Insert(String), Ryan%(String)
- 2016-08-06 16:20:19,978 DEBUG [com.test.mapper.dao.BlogMapper.findActiveBlogLike] - <== Total: 2
- [Blog{id=8, name='testInsert', author=null, coAuthor=null, posts=null, state='active'}, Blog{id=9, name='testInsert', author=null, coAuthor=null, posts=null, state='active'}]
test通过了,blog也确实是我们想要的两条。但仔细观察结果就会发现几个问题。第一个问题是author为null,这个我们等下再解决。第二问题是sql查询语句查询了author.username like,也就是说我们第二个if节点的test 为true。难道出了问题?我们的Author类明明没有name字段。所以,这里要跟踪下代码。
好吧,跟踪了半天一直到ognl内部,还是没追踪到为什么name翻译成username了。下面还是搞定第一个问题,author为null。
查询的结果映射到Blog,但blog的author字段并没有初始化。很容易就猜测到结果集的字段和blog的author不匹配。这个就用到resultMap而不是resultType。在上一遍博文中记录了下来。
- <select id="findBlogMap" resultMap="findBlogResultMap">
- SELECT b.id AS id,
- b.name AS name,
- b.state AS state,
- a.id as author_id,
- a.username as author_username,
- a.password as author_password,
- a.email as author_email,
- a.bio as author_bio
- FROM blog b, author a
- WHERE state = 'active'
- <if test="name != null">
- AND name LIKE #{name}
- </if>
- AND b.author_id = a.id
- <if test="author != null and author.username != null">
- AND a.username LIKE #{author.username}
- </if>
- </select>
- <resultMap id="findBlogResultMap" type="Blog">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="state" column="state"/>
- <association property="author" column="author_id" javaType="Author">
- <id property="id" column="author_id"/>
- <result property="username" column="author_username"/>
- <result property="password" column="author_password"/>
- <result property="email" column="author_email"/>
- <result property="bio" column="author_bio"/>
- </association>
- </resultMap>
这样测试结果:
- -- ::, DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Opening JDBC Connection
- -- ::, DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] - Created connection .
- -- ::, DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@]
- -- ::, DEBUG [com.test.mapper.dao.BlogMapper.findBlogMap] - ==> Preparing: SELECT b.id AS id, b.name AS name, b.state AS state, a.id as author_id, a.username as author_username, a.password as author_password, a.email as author_email, a.bio as author_bio FROM blog b, author a WHERE state = 'active' AND name LIKE ? AND b.author_id = a.id AND a.username LIKE ?
- -- ::, DEBUG [com.test.mapper.dao.BlogMapper.findBlogMap] - ==> Parameters: %Insert(String), Ryan%(String)
- -- ::, DEBUG [com.test.mapper.dao.BlogMapper.findBlogMap] - <== Total:
- [Blog{id=8, name='testInsert', author=Author{id=4, username='Ryan', password='123456', email='qweqwe@qq.com', bio='this is a blog'}, coAuthor=null, posts=null, state='active'}, Blog{id=9, name='testInsert', author=Author{id=5, username='Ryan0', password='123456', email='qweqwe@qq.com', bio='this is a blog'}, coAuthor=null, posts=null, state='active'}]
2.choose
Mybatis - 动态sql的更多相关文章
- mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句
mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...
- 9.mybatis动态SQL标签的用法
mybatis动态SQL标签的用法 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...
- 自己动手实现mybatis动态sql
发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客.本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有时间再补上orm完整的实现吧. 用过mybatis的人,估计 ...
- Mybatis动态SQL单一基础类型参数用if标签
Mybatis动态SQL单一基础类型参数用if标签时,test中应该用 _parameter,如: 1 2 3 4 5 6 <select id="selectByName" ...
- 超全MyBatis动态SQL详解!( 看完SQL爽多了)
MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...
- Mybatis动态SQL简单了解 Mybatis简介(四)
动态SQL概况 MyBatis 的强大特性之一便是它的动态 SQL 在Java开发中经常遇到条件判断,比如: if(x>0){ //执行一些逻辑........ } Mybatis应用中,S ...
- mybatis原理分析学习记录,mybatis动态sql学习记录
以下个人学习笔记,仅供参考,欢迎指正. MyBatis 是支持定制化 SQL.存储过程以及高级映射的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转 ...
- mybatis 动态sql和参数
mybatis 动态sql 名词解析 OGNL表达式 OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性, ...
- MyBatis动态SQL之一使用 if 标签和 choose标签
bootstrap react https://segmentfault.com/a/1190000010383464 xml 中 < 转义 to thi tha <if test=&qu ...
- MyBatis动态SQL(认真看看, 以后写SQL就爽多了)
目录 0 一起来学习 mybatis 1 数据准备 2 if 标签 2.1 在 WHERE 条件中使用 if 标签 2.1.1 查询条件 2.1.2 动态 SQL 2.1.3 测试 2.2 在 UPD ...
随机推荐
- 学习笔记——SQLite介绍
简介:Google为android的较大数据的处理提供了SQLlite, 他在数据存储.管理.维护.等各方面都相当出色功能也非常强大. 1.创建数据库 Android 为了让我们能够更加方便地管理数据 ...
- 安卓工具箱:color of Style
<?xml version="1.0" encoding="utf-8"?> <resources> <color name=&q ...
- DotNet Core 介绍
前言 asp.net core rtm 6月底即将发布,自己也想着为社区做点共享,刚好最近不太忙,看到社区的小伙伴们都在为dotnet core的推广而贡献力量,项目中刚好在用rc2版本,就多写些文章 ...
- Unity3D游戏开发初探—3.初步了解U3D物理引擎
一.什么是物理引擎? 四个世纪前,物理学家牛顿发现了万有引力,并延伸出三大牛顿定理,为之后的物理学界的发展奠定了强大的理论基础.牛顿有句话是这么说的:“如果说我看得比较远的话,那是因为我站在巨人的肩膀 ...
- [Voice communications] 声音的滤波
本系列文章主要是介绍 Web Audio API 的相关知识,以及 web语音通信 中会遇到的一些问题,阐述可能存在错误,还请多多斧正! 通过设备获取音频流会不可避免的渗入一些杂音,这些杂音可能来自你 ...
- [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序
[.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序 本节导读: 本节主要说明使用异步进行程序设计的优缺点及如何通过异步编程. 使用 ...
- js中几种实用的跨域方法原理详解
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- 如何在没有域的环境中搭建AlwaysOn(一)
对DBA而言,不需要域就可以搭建SQL Server AlwaysOn是Windows Server 2016中最令人兴奋的功能了,它不仅可以降低搭建的成本,而且还减少了部署和运维的工作量. 该特性可 ...
- [备忘]检索 COM 类工厂中 CLSID 为 {91493441-5A91-11CF-8700-00AA0060263B} 的组件时失败解决方法
检索 COM 类工厂中 CLSID 为 {91493441-5A91-11CF-8700-00AA0060263B} 的组件时失败,原因是出现以下错误: 80070005 在CSDN上总是有网友问这个 ...
- 【Win10 应用开发】自适应Toast通知的XML文档结构
老规矩,在开始之前老周先讲个故事. 话说公元2015年7月20日,VS 2015发布.于是,肯定有人会问老周了,C#6有啥新特性,我学不来啊.学不来的话你应该检讨.老周比较保守地计算一下,学会C# 6 ...