测试代码

sqlSessionFactory.openSession(false).getMapper(StudentMapper.class).findStudentById(id)
核心方法:org.apache.ibatis.session.Configuration#getMapper

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {

//从Mybatis初始化过程中绑定的名称空间和接口类,获取接口工厂
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
//生成接口代理类
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
动态代理工厂

public class MapperProxyFactory<T> {

private final Class<T> mapperInterface;
private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();

public MapperProxyFactory(Class<T> mapperInterface) {
this.mapperInterface = mapperInterface;
}

public Class<T> getMapperInterface() {
return mapperInterface;
}

public Map<Method, MapperMethod> getMethodCache() {
return methodCache;
}

@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
//JDK动态代理
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}

public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
}
动态代理对象:MapperProxy

[@Override](https://my.oschina.net/u/1162528)
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} else if (isDefaultMethod(method)) {
return invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable www.ylouyi3.com) {
throw ExceptionUtil.unwrapThrowable(t);
}
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}
sql操作

public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
switch (command.getType()) {
case INSERT: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
case UPDATE: {
Object param =www.qwert888.com method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}
case DELETE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
break;
}
case SELECT:
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {
result = executeForMany(sqlSession, args);
} else if (method.returnsMap()) {
result = executeForMap(sqlSession, args);
} else if (method.returnsCursor()) {
result = executeForCursor(sqlSession, args);
} else {
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
}
break;
case FLUSH:
result = sqlSession.flushStatements();
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;
}
org.apache.ibatis.binding.MapperMethod#executeForMany

private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
List<E> result;
Object param = method.convertArgsToSqlCommandParam(args);
if (method.hasRowBounds()) {
RowBounds rowBounds = method.extractRowBounds(args);
result = sqlSession.<E>selectList(command.getName(), param, rowBounds);
} else {
result = sqlSession.<www.xinhuiyule1.cn>selectList(command.getName(), param);
}
// issue #510 Collections & arrays support
if (!method.getReturnType().isAssignableFrom(result.getClass())) {
if (method.getReturnType(www.gouyiflb.cn/).isArray()) {
return convertToArray(result);
} else {
return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
}
}
return result;
}

//具体的操作通过Executor操作,本质底层也是JDBC操作,只是通过configuration配置按照业务进行数据转换
[@Override](https://my.oschina.net/u/1162528)
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
try {
MappedStatement ms = configuration.getMappedStatement(statement);
return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
} catch (Exception e) {
throw ExceptionFactory.wrapException(www.yongxinzaixian.cn"Error querying database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}

[root@promote ~]#
[root@promote ~]# bash testreadv1.0.sh
please input name: www
name is : www
#第二次等待5秒未输入内容
[root@promote ~]# bash testreadv1.0.sh
please input name:www.mhylpt.com www
input error...
#思考问题如何清除错误输出内容到下一行,本次操作实际输入www未按回车键
[root@promote ~]# www
[root@promote ~]#
#-s 不前台显示输入内容
[root@promote ~]# cat testreadv1.1.sh
#!/bin/bash
read -s -p "please input password:" passwd
echo -e "\npassword is :$passwd"
exit 0
[root@promote ~]#

CentOS 7 Shell脚本编程第九讲 read命令简单介绍的更多相关文章

  1. shell脚本编程-使用结构化命令(if/else)(转)

    11.1 使用if-then语句 格式如下 if语句会执行if行定义的那个命令,如果该命令的退出状态码是0,则then部分的语句就会执行,其他值,则不会   1 2 3 4 if command th ...

  2. Linux Shell脚本编程--curl命令详解

    用途说明 curl命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类 ...

  3. 解决方案:centos运行shell脚本时报“$'\r': 未找到命令”

    =============================================== 2018/9/12_第1次修改                       ccb_warlock == ...

  4. Linux Shell脚本编程--cut命令

    cut cut命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields <==用于有特定分隔字符 [root@www ...

  5. Linux Shell脚本编程--nc命令使用详解

    linux nc命令使用详解     功能说明:功能强大的网络工具 语 法:nc [-hlnruz][-g<网关...>][-G<指向器数目>][-i<延迟秒数>] ...

  6. Linux Shell脚本编程--sed命令详解

    简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...

  7. (转)Shell脚本编程--Uniq命令

    uniq 原文:http://blog.csdn.net/xifeijian/article/details/9209627 uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用 ...

  8. Shell脚本编程学习入门 02

    Shell脚本编程学习入门是本文要介绍的内容,我们可以使用任意一种文字编辑器,比如gedit.kedit.emacs.vi等来编写shell脚本,它必须以如下行开始(必须放在文件的第一行):   #! ...

  9. centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课

    centos  shell脚本编程2 if 判断  case判断   shell脚本中的循环  for   while   shell中的函数  break  continue  test 命令   ...

随机推荐

  1. Accordion CodeForces - 1101B (实现)

    An accordion is a string (yes, in the real world accordions are musical instruments, but let's forge ...

  2. iOS--LaunchImage启动页设置及问题解决

    在Assets.xcassets中使用LaunchImage来设置启动图:   一.根据不同屏幕尺寸的需求设置不同的图片,可以参照下图: 1.点击Image.xcassets 进入图片管理,然后右击, ...

  3. node学习: package.json

    package.json 定义了这个项目所需要的各种模块,以及项目的配置信息(比如名称.版本.许可证等元数据) 1.创建 package.json npm init npm init –yes 2.p ...

  4. KubeCon CloudNativeCon China 2019

    KubeCon CloudNativeCon China 2019 - LF Asia, LLChttps://events.linuxfoundation.cn/events/kubecon-clo ...

  5. 迁移 VMware 虚拟机到 KVM

    虚拟机转换| VMware vCenter Converterhttps://www.vmware.com/cn/products/converter.html 迁移 VMware 虚拟机到 KVMh ...

  6. Centos 7 关闭selinux and firewall

    关闭selinx,重启生效 修改文件 /etc/selinux/config 修改 SELINUX=disabled getenforce #查selinux状态 setenforce #关闭seli ...

  7. MyBatis映射文件4(参数获取#{}和${}/select标签详解[返回类型为list])

    参数获取 之前我们都是采用#{}的方式进行参数传递,其实MyBatis还有另外的参数传递方式${} 使用方法相同,但是还是有很大区别的 这里做一个测试: <select id="get ...

  8. C程序运行时的内存分布

    该篇博客是自己学习的总结,如果有哪里理解的不对的地方,希望大家可以指点. 一.C内存空间分布图 二.各内存区域详解 1.代码区(.text): 该区域主要存放二进制可执行文件. 2.数据区(.data ...

  9. Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念(转)

    PO(persistant object) 持久对象 在 o/r 映射的时候出现的概念,如果没有 o/r 映射,没有这个概念存在了.通常对应数据模型 ( 数据库 ), 本身还有部分业务逻辑的处理.可以 ...

  10. linux硬盘的分区、格式化、挂载以及LVM

    linux硬盘的分区.格式化.挂载以及LVM   多块硬盘的组合: 硬盘分两种:ide和scsi. ide硬盘: /dev/hda 第一块IDE硬盘 /dev/hdb 第二块IDE硬盘 ... /de ...