服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - mybatis查询语句揭秘之参数解析

mybatis查询语句揭秘之参数解析

2021-07-29 11:31不懂是非 Java教程

这篇文章主要给大家介绍了关于mybatis查询语句之参数解析的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用mybatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

一、前言

通过前面我们也知道,通过getmapper方式来进行查询,最后会通过mappermehod类,对接口中传来的参数也会在这个类里面进行一个解析,随后就传到对应位置,与sql里面的参数进行一个匹配,最后获取结果。对于mybatis通常传参(这里忽略掉rowbounds和resulthandler两种类型)有几种方式。

1、javabean类型参数

2、非javabean类型参数

注意,本文是基于mybatis3.5.0版本进行分析。

1、参数的存储

2、对sql语句中参数的赋值

下面将围绕这这两方面进行

二、参数的存储

先看下面一段代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@test
 public void testselectordinaryparam() throws exception{
 sqlsession sqlsession = mybatisutil.getsessionfactory().opensession();
 usermapper mapper = sqlsession.getmapper(usermapper.class);
 list<user> userlist = mapper.selectbyordinaryparam("张三1号");
 system.out.println(userlist);
 sqlsession.close();
 }
 list<user> selectbyordinaryparam(string username); // mapper接口
 <select id="selectbyordinaryparam" resultmap="baseresultmap">
 select
 <include refid="base_column_list"/>
 from user
 where username = #{username,jdbctype=varchar}
 </select>

或许有的人会奇怪,这个mapper接口没有带@param注解,怎么能在mapper配置文件中直接带上参数名呢,不是会报错吗,

在mybatis里面,对单个参数而言,直接使用参数名是没问题的,如果是多个参数就不能这样了,下面我们来了解下,mybatis的解析过程,请看下面代码,位于mappermehod类的内部类methodsignature构造函数中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public methodsignature(configuration configuration, class<?> mapperinterface, method method) {
 type resolvedreturntype = typeparameterresolver.resolvereturntype(method, mapperinterface);
 if (resolvedreturntype instanceof class<?>) {
 this.returntype = (class<?>) resolvedreturntype;
 } else if (resolvedreturntype instanceof parameterizedtype) {
 this.returntype = (class<?>) ((parameterizedtype) resolvedreturntype).getrawtype();
 } else {
 this.returntype = method.getreturntype();
 }
 this.returnsvoid = void.class.equals(this.returntype);
 this.returnsmany = configuration.getobjectfactory().iscollection(this.returntype) || this.returntype.isarray();
 this.returnscursor = cursor.class.equals(this.returntype);
 this.returnsoptional = optional.class.equals(this.returntype);
 this.mapkey = getmapkey(method);
 this.returnsmap = this.mapkey != null;
 this.rowboundsindex = getuniqueparamindex(method, rowbounds.class);
 this.resulthandlerindex = getuniqueparamindex(method, resulthandler.class);
 // 参数解析类
 this.paramnameresolver = new paramnameresolver(configuration, method);
 }

参数的存储解析皆由paramnameresolver类来进行操作,先看下该类的构造函数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * config 全局的配置文件中心
 * method 实际执行的方法,也就是mapper接口中的抽象方法
 *
 */
public paramnameresolver(configuration config, method method) {
 // 获取method中的所有参数类型
 final class<?>[] paramtypes = method.getparametertypes();
 // 获取参数中含有的注解,主要是为了@param注解做准备
 final annotation[][] paramannotations = method.getparameterannotations();
 final sortedmap<integer, string> map = new treemap<>();
 // 这里实际上获取的值就是参数的个数。也就是二维数组的行长度
 int paramcount = paramannotations.length;
 // get names from @param annotations
 for (int paramindex = 0; paramindex < paramcount; paramindex++) {
 // 排除rowbounds和resulthandler两种类型的参数
 if (isspecialparameter(paramtypes[paramindex])) {
 // skip special parameters
 continue;
 }
 string name = null;
 // 如果参数中含有@param注解,则只用@param注解的值作为参数名
 for (annotation annotation : paramannotations[paramindex]) {
 if (annotation instanceof param) {
  hasparamannotation = true;
  name = ((param) annotation).value();
  break;
 }
 }
 // 即参数没有@param注解
 if (name == null) {
 // 参数实际名称,其实这个值默认就是true,具体可以查看configuration类中的该属性值,当然也可以在配置文件进行配置关闭
 // 如果jdk处于1.8版本,且编译时带上了-parameters 参数,那么获取的就是实际的参数名,如methoda(string username)
 // 获取的就是username,否则获取的就是args0 后面的数字就是参数所在位置
 if (config.isuseactualparamname()) {
  name = getactualparamname(method, paramindex);
 }
 // 如果以上条件都不满足,则将参数名配置为 0,1,2../
 if (name == null) {
  // use the parameter index as the name ("0", "1", ...)
  // gcode issue #71
  name = string.valueof(map.size());
 }
 }
 map.put(paramindex, name);
 }
 names = collections.unmodifiablesortedmap(map);
 }

这个构造函数的作用就是对参数名称进行一个封装,得到一个  “参数位置-->参数名称 “ 的一个map结构,这样做的目的是为了替换参数值,我们也清楚,实际传过来的参数就是一个一个object数组结构,我们也可以将它理解为map结构。即 index --> 参数值,此就和之前的 map结构有了对应,也就最终可以得到一个 参数名称  --->  参数值 的一个对应关系。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public object execute(sqlsession sqlsession, object[] args) {
 object result;
 switch (command.gettype()) {
 // 其它情况忽略掉
 case select:
 // 这里参数中含有resulthandler,暂不做讨论
 if (method.returnsvoid() && method.hasresulthandler()) {
  executewithresulthandler(sqlsession, args);
  result = null;
 } else if (method.returnsmany()) {// 1、 返回结果为集合类型或数组类型,这种情况适用于大多数情况
  result = executeformany(sqlsession, args);
 } else if (method.returnsmap()) {// 返回结果为map类型
  result = executeformap(sqlsession, args);
 } else if (method.returnscursor()) {
  result = executeforcursor(sqlsession, args);
 } else {// 2、返回结果javabean类型,或普通的基础类型及其包装类等
  object param = method.convertargstosqlcommandparam(args);
  result = sqlsession.selectone(command.getname(), param);
  // 对java8中的optional进行了支持
  if (method.returnsoptional() &&
  (result == null || !method.getreturntype().equals(result.getclass()))) {
  result = optional.ofnullable(result);
  }
 }
 break;
 default:
 throw new bindingexception("unknown execution method for: " + command.getname());
 }
 if (result == null && method.getreturntype().isprimitive() && !method.returnsvoid()) {
 throw new bindingexception("mapper method '" + command.getname()
  + " attempted to return null from a method with a primitive return type (" + method.getreturntype() + ").");
 }
 return result;
 }

这里主要分析1情况。对于2情况也就是接下来要说的参数赋值情况,不过要先介绍下method.convertargstosqlcommandparam这代码带来的一个结果是怎么样的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public object convertargstosqlcommandparam(object[] args) {
 return paramnameresolver.getnamedparams(args);
 }
 
public object getnamedparams(object[] args) {
 final int paramcount = names.size();
 if (args == null || paramcount == 0) {
 return null;
 } else if (!hasparamannotation && paramcount == 1) {// 1
 return args[names.firstkey()];
 } else {
 final map<string, object> param = new parammap<>();
 int i = 0;
 for (map.entry<integer, string> entry : names.entryset()) {
 param.put(entry.getvalue(), args[entry.getkey()]);
 // add generic param names (param1, param2, ...)
 final string genericparamname = generic_name_prefix + string.valueof(i + 1);
 // ensure not to overwrite parameter named with @param
 if (!names.containsvalue(genericparamname)) {
  param.put(genericparamname, args[entry.getkey()]);
 }
 i++;
 }
 return param;
 }
 }

可以很清楚的知道最后又调用了paramnameresolver类的getnamedpaams方法,这个方法的主要作用就是,将原来的参数位置 -->  参数名称  映射关系转为  参数名称 --->参数值 ,并且新加一个参数名和参数值得一个对应关系。即

param1  ->参数值1

param2 -->参数值2

当然如果只有一个参数,如代码中的1部分,若参数没有@param注解,且只有一个参数,则不会加入上述的一个对象关系,这也就是前面说的,对于单个参数,可以直接在sql中写参数名就ok的原因。下面回到前面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private <e> object executeformany(sqlsession sqlsession, object[] args) {
 list<e> result;
 // 获取对应的一个映射关系,param类型有可能为map或null或参数实际类型
 object param = method.convertargstosqlcommandparam(args);
 if (method.hasrowbounds()) {
 rowbounds rowbounds = method.extractrowbounds(args);
 result = sqlsession.<e>selectlist(command.getname(), param, rowbounds);
 } else {
 result = sqlsession.<e>selectlist(command.getname(), param);
 }
 // 如果返回结果类型和method的返回结果类型不一致,则进行转换数据结构
 // 其实就是result返回结果不是list类型,而是其他集合类型或数组类型
 if (!method.getreturntype().isassignablefrom(result.getclass())) {
 if (method.getreturntype().isarray()) {// 为数组结果
 return converttoarray(result);
 } else {// 其他集合类型
 return converttodeclaredcollection(sqlsession.getconfiguration(), result);
 }
 }
 return result;
 }

代码也不复杂,就是将得到的参数对应关系传入,最终获取结果,根据实际需求进行结果转换。

3、对sql语句中参数的赋值

其实前面一篇博客中也有涉及到。参数赋值的位置在defaultparameterhandler类里面,可以查看前面一篇博客,这里不做过多介绍,传送门   mybatis查询语句的背后之封装数据

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。

原文链接:http://www.cnblogs.com/qm-article/p/10658527.html

延伸 · 阅读

精彩推荐