好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MyBatis中动态sql的实现方法示例

1. 动态sql

动态sql是mybatis中的一个核心,什么是动态sql?

动态sql即对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

mybatis的强大特性之一便是它的动态 sql。如果你有使用 jdbc 或其他类似框架的经验,你就能体会到根据不同条件拼接 sql 语句有多么痛苦。拼接的时候要确保不能忘了必要的空格,还要注意省掉列名列表最后的逗号。有些时候,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息。使用oracle的序列、mysql的函数生成id。这时我们可以使用动态sql。利用动态 sql 这一特性可以彻底摆脱这种痛苦。通常使用动态 sql 不可能是独立的一部分,mybatis 当然使用一种强大的动态 sql 语言来改进这种情形,这种语言可以被用在任意的 sql 映射语句中。动态 sql 元素和使用 jstl 或其他类似基于 xml 的文本处理器相似。mybatis 采用功能强大的基于 ognl 的表达式来消除其他元素。

mybatis中用于实现动态sql的元素主要有:

1、if和where

2、choose(when,otherwise)

3、trim

4、set

5、foreach

就拿上一篇博文中对用户的综合查询一例来说:

?

1

select * from user where user.sex = #{user.sex} and user.username like '%${user.username}%'

假如这个user是null咋整?或者user.sex或者user.username为null呢?所以更严谨的做法应该是在执行这个语句之前要先进行判断才对,确保都不为空,那么我再去查询。这就涉及到了mybatis中的动态sql了。

在mybatis中,动态sql可以使用标签来表示,这很类似于jstl表达式,我们可以将上面的sql语句改成动态sql,如下:

?

1

2

3

4

5

6

7

8

9

10

11

<select id= "finduserlist" parametertype= "mybatis.po.userqueryvo" resulttype= "mybatis.po.user" >

  select * from user <!-- where可以自动去掉条件中的第一个and -->

  <where>

  < if test= "user!=null" >

  < if test= "user.sex!=null and user.sex!=''" >

  and user.sex = #{user.sex} </ if >

  < if test= "user.username!=null and user.username!=''" >

  and user.username like '%${user.username}%' </ if >

  </ if >

  </where>

</select>

上面的代码很好理解,主要就是加了一些判断,条件不为空,才进行查询条件的拼接,让mybatis动态的去执行。那么在测试代码中,我们可以故意的将user.sex不赋初值,就可以看到查询的结果是不一样的。

2. sql片段

那么现在还有个问题,如果好几个statement都需要这样做,而且动态sql部分都一样,这就会导致一些代码的重复,所以如果遇到这种情况,我们就应该抽取,动态sql也可以抽取,我们可以将动态的这部分sql抽取成sql片段,然后在具体的statement中引用进来即可。如下:

?

1

2

3

4

5

6

7

8

<sql id= "query_user_where" >

  < if test= "user!=null" >

  < if test= "user.sex!=null and user.sex!=''" >

  and user.sex = #{user.sex} </ if >

  < if test= "user.username!=null and user.username!=''" >

  and user.username like '%${user.username}%' </ if >

  </ if >

</sql>

id是给该sql片段起个名字而已,内部就是上面的where动态部分,然后我们将上面原来的动态部分改成对这个sql片段的引用,如下:

?

1

2

3

4

5

6

7

<select id= "finduserlist" parametertype= "mybatis.po.userqueryvo" resulttype= "mybatis.po.user" >

  select * from user <where>

  <!-- 引用sql片段的id,如果refid指定的id不在本mapper文件中,需要在前面加上namespace -->

  <include refid= "query_user_where" ></include>

  <!-- 还可以引用其他sql片段 -->

  </where>

</select>

3. foreach

还有个问题:如果我们要向sql传递数组或list该咋整呢?mybatis使用的是foreach解析。为了模拟这个场景,我们将上面的查询改成多个id查询,有两种查询方式:

?

1

select * from user where id= 1 or id= 12 or id=17select * from user where id in( 1 , 12 , 17 )

首先有一点很明确,既然要使用多个id进行查询,那么多个id肯定要作为参数传进来,所以存储多个id的list需要放到userqueryvo中作为一个属性,这点很好理解,所以我们先在userqueryvo中增加这个属性:

?

1

2

//传入多个id

private list<integer> ids;

然后我们修改usermapper.xml中的sql片段(还是写在sql片段中),如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<sql id= "query_user_where" >

  < if test= "user!=null" >

  < if test= "user.sex!=null and user.sex!=''" >

  and user.sex = #{user.sex} </ if >

  < if test= "user.username!=null and user.username!=''" >

  and user.username like '%${user.username}%' </ if >

  </ if >

  < if test= "ids!=null" >

  <!-- 使用右边的sql拼接:and (id= 1 or id= 12 or id= 17 ) -->

  <foreach collection= "ids" item= "user_id" open= "and (" close= ")" separator= "or" >

  id=#{user_id}

   </foreach>

  </ if >

</sql>

下面简单介绍一下这个foreach中相关属性的作用:

collection:指定输入对象中的集合属性,这里就是这个ids。 item:表示每个遍历生成的对象,自己起个名儿,在foreach体中使用。 open:开始遍历时拼接的sql串。 close:结束遍历时拼接的sql串。 separator:遍历的两个对象中需要拼接的sql串。

我们测试一下,然后看下控制台打印出来的sql就很容易理解了。测试程序:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

@testpublic void testfinduserlist() throws exception {

 

  sqlsession sqlsession = sqlsessionfactory.opensession();

  //创建usermapper对象,mybatis自动生成mapper代理对象

  usermapper usermapper = sqlsession.getmapper(usermapper. class );

 

  //创建包装对象,设置查询条件

  userqueryvo userqueryvo = new userqueryvo();

  user user = new user();

  //由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中

  user.setsex( "男" );

  user.setusername( "倪升武" );

 

  //传入多个id

  list<integer> ids = new arraylist<integer>();

  ids.add( 1 );

  ids.add( 12 );

  ids.add( 17 );

  userqueryvo.setids(ids);

 

  userqueryvo.setuser(user);

  //调用usermapper的方法

  list<user> list = usermapper.finduserlist(userqueryvo);

  system.out.println(list);}

看下控制台打印出的sql:

?

1

select * from user where user.sex = ? and user.username like '%倪升武%' and ( id=? or id=? or id=? )

注意一个细节:在mybatis中,如果输入的是integer或者int类型的0,上面那个if判断标签返回的是false,也就是说,即使非空非'',也不会拼接标签体中的sql。

所以mybatis自动的将多个id拼接到了sql中。那么另外一个sql的实现就不再赘述了,跟上面的一样,唯一不同的就是sql片段部分,如下:

?

1

2

3

<!-- 使用右边的sql拼接:and id in( 1 , 12 , 17 ) -->

<foreach collection= "ids" item= "user_id" open= "and id in(" close= ")" separator= "," >

  #{user_id}</foreach>

总结:

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://yq.aliyun.com/articles/667279

查看更多关于MyBatis中动态sql的实现方法示例的详细内容...

  阅读:17次