转载请注明出处。。。

一、前言

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

1、javabean类型参数

2、非javabean类型参数

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

1、参数的存储

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

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

二、参数的存储

先看下面一段代码

     @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构造函数中

 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类来进行操作,先看下该类的构造函数

 /**
* 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结构有了对应,也就最终可以得到一个 参数名称  --->  参数值 的一个对应关系。

 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这代码带来的一个结果是怎么样的

 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) {//
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的原因。下面回到前面

 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查询语句的背后之封装数据

---------------------------------------------------------------------------------------------------------------------------------------分界线--------------------------------------------------------------------------------------------------------

若有不足或错误之处,还望指正,谢谢!

mybatis查询语句的背后之参数解析的更多相关文章

  1. mybatis查询语句的背后之封装数据

    转载请注明出处... 一.前言 继上一篇mybatis查询语句的背后,这一篇主要围绕着mybatis查询的后期操作,即跟数据库交互的时候.由于本人也是一边学习源码一边记录,内容难免有错误或不足之处,还 ...

  2. mybatis查询语句的背后

    转载请注明出处... 一.前言 在先了解mybatis查询之前,先大致了解下以下代码的为查询做了哪些铺垫,在这里我们要事先了解,myabtis会默认使用DefaultSqlSessionFactory ...

  3. Mybatis,模糊查询语句,以及传参数的正确写法

    不多说直接上代码! 接口: public interface CommodityMapper { int deleteByPrimaryKey(Integer productId); int inse ...

  4. mybatis 查询语句(按条件查询)

    <select id="getAllDitch" parameterType="xxx.xx.entity.CheckDitch" resultType= ...

  5. mybatis查询语句获取自增主键

    第一种方式: 主键回填useGeneratedKeys 代表采用JDBC的Statment对象的getGeneratedKeys方法返回主键keyProperty 代表将用哪个POJO的属性去匹配这个 ...

  6. mybatis中的查询语句in用法的相关问题

    在开发的时候,mybatisl中使用in的时候会遇到一些问题,如果我们传的参数是String类型,以“,”来进行隔开的,例如:参数是0,1,2字符串,mybatis中的语句如下 <select ...

  7. Mybatis SQL语句查询

    MyBatis中使用in查询时的注意事项 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach一共有三种类型,分别为List,[](array),Map三种. ...

  8. gin的url查询参数解析

    gin作为go语言最知名的网络库,在这里我简要介绍一下url的查询参数解析.主要是这里面存在一些需要注意的地方.这里,直接给出代码,和运行结果,在必要的地方进行分析. 代码1: type Struct ...

  9. sql查询语句如何解析成分页查询?

    我们公司主要mysql存储数据,因此也封装了比较好用mysql通用方法,然后,我们做大量接口,在处理分页查询接口,没有很好分查询方法.sql查询 语句如何解析成“分页查询”和“总统计”两条语句.可能, ...

随机推荐

  1. Confluence 6 连接到 Jira 用户管理的建议

    建议 如果下面所有的选项都为是的话: JIRA 应用程序不在高负载下运行. 你仅仅希望在一些不多的应用中跨平台管理你的用户和用户组,例如一个 JIRA 软件服务器和 Confluence 服务器,或者 ...

  2. LeetCode(76): 最小覆盖子串

    Hard! 题目描述: 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = &q ...

  3. mabatis的批量新增sql 初级的 初级的 初级的

    简单描述:做开发的时候,会遇到一次插入好多条记录,怎么做好呢? 解决思路:循环insert啊!  哪凉快那呆着去←!←  这样会增加数据库开销的,当然不能这么干了,要在sql上下功夫.看代码,一下就明 ...

  4. Jmeter 获取CSV行数

    import java.io.BufferedReader; import java.io.FileInputStream; String str = "E:\\Desktop\\WOS接口 ...

  5. 解决Django + DRF:403 FORBIDDEN:CSRF令牌丢失或不正确,{"detail":"CSRF Failed: CSRF cookie not set."}

    我有一个Android客户端应用程序尝试使用Django + DRF后端进行身份验证.但是,当我尝试登录时,我收到以下响应: 403: CSRF Failed: CSRF token missing ...

  6. C#线性表

    线性表是线性结构的抽象 线性结构的特点是结构中的数据元素之间存在一对一的线性关系 一对一的关系指的是数据元素之间的位置关系 (1)除第一个位置的数据元素外,其它数据元素位置的前面都只有一个数据元素 ( ...

  7. 【C++ Primer | 07】常用算法

    第一部分 常用泛型算法: find(beg, end, val); equal(beg1, end1, beg2); fill(beg, end, val); fill_n(beg, cnt, val ...

  8. 跳跃表Skip List的原理

    1.二分查找和AVL树查找 二分查找要求元素可以随机访问,所以决定了需要把元素存储在连续内存.这样查找确实很快,但是插入和删除元素的时候,为了保证元素的有序性,就需要大量的移动元素了.如果需要的是一个 ...

  9. nginx 源码安装的重启命令

    源码安装nginx就面临这样的麻烦,不能使用service nginx restart 来重启nginx,没办法只能重新加载下nginx. #/usr/local/nginx/sbin/nginx - ...

  10. Apache Tomcat RCE(CVE-2017-12615 )漏洞案例分析

    首先搭建tomcat环境: 下载当前项目的版本的tomcat