Mybatis映射器接口代理对象的方式 运行过程
查询一张表的所有数据。
环境:
使用工具IntelliJ IDEA 2018.2版本。
创建Maven工程不用骨架
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jxjdemo</groupId>
<artifactId>day33_mybatis1_quicksta</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<!--MySql数据库-->
<dependency><!-- 依赖 -->
<groupId>mysql</groupId><!--公司名/组织名groupId:域名倒写 -->
<artifactId>mysql-connector-java</artifactId><!--项目包/包名 -->
<version>5.1.47</version><!--版本号version:1.0-SNAPSHOT 开发版 1.0-RELEASE稳定发行版 -->
</dependency>
<!--MyBatis的jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--日志包-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope> <!--加test单元测试只能写在test内-->
</dependency>
</dependencies> </project>
2.表-类
package com.jxjdemo.domain;
import java.util.Date;
public class User {
private Integer id;
private String username;
private Date birthday; //导包,框架自动帮我们转可以这样写。
private String sex;
private String address;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
//省略Get与Set方法
3.映射器
package com.jxjdemo.dao;
import com.jxjdemo.domain.User;
import java.util.List;
/**
* 映射器:dao层的接口
*/
public interface UserDao {
List<User> queryAll();
}
4.映射器配置文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
上面为引入的约束
_____________________________________________________________
下面的mapper:映射器配置
namespace:映射器的全限定类名
-->
<mapper namespace="com.jxjdemo.dao.UserDao">
<!--
statement:映射器里面每个方法的配置信息,叫做statement
select标签:用于查询
insert标签:用于插入
update标签:用于修改
delete标签:用于删除 以上四个标签都有的属性:
id:映射器里面写 方法的名称
resultType 结果集封装的类型
-->
<select id="queryAll" resultType="com.jxjdemo.domain.User">
select * from user
</select>
</mapper>
5.数据库核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis的核心配置文件,主要配置数据库连接信息-->
<configuration><!--根标签-->
<!--enxironments 可以配置多个数据库环境-->
<environments default="mysql"><!--default 默认使用的数据库-->
<environment id="mysql"><!--environment每一个数据库连接(配置)信息-->
<transactionManager type="JDBC"/><!--事物管理方式-->
<dataSource type="POOLED"><!--数据源。不使用UN连接池POOLED,POOLED使用连接池,JNDI查找数据源配置文件-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:端口号/库名"/>
<property name="username" value="账号"/>
<property name="password" value="密码"/>
</dataSource>
</environment>
</environments>
<mappers><!--映射器-->
<mapper resource="com/jxjdemo/dao/UserDao.xml" />
</mappers>
</configuration>
6.测试类
package com.jxjtest.test; import com.jxjdemo.dao.UserDao;
import com.jxjdemo.domain.User; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; public class QuickstartTest { //Quickstart快速开始
@Test
public void testQuickStart() throws IOException {
//1.读取配置文件
InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml"); //抛出异常 //2.读取到Sqlsession对象.要从SqlSessionFactory里面生产SqlSession对象
SqlSessionFactory facyory = new SqlSessionFactoryBuilder().build(is);
//new一个工厂,给他传流,返回一个工厂对象
SqlSession session = facyory.openSession();//有了工厂对象,就用openSession得到session //3.操作数据库
UserDao userDao = session.getMapper(UserDao.class);//使用getMapper返回UserDao对象,动态代理有接口就行。创建代理对象
List<User> userList = userDao.queryAll();
for (User user : userList) {
System.out.println(user);
}
//4.释放资源
session.close();
is.close();
}
}
7.项目结构

8..在测试类打断点,开始deBug跟踪,从这里开始。首先得到UserDao,dao对象是一个代理对象。
UserDao userDao = session.getMapper(UserDao.class);//使用getMapper返回UserDao对象
2 List<User> userList = userDao.queryAll();
9.到MapperProxy.java里的invole方法
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {//1.
return method.invoke(this, args);
} else if (method.isDefault()) {//2.
return invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
final MapperMethod mapperMethod = cachedMapperMethod(method);//3.
return mapperMethod.execute(sqlSession, args);//4.开始执行映射器的方法进入方法
}
10.到了MapperMethod.java中
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
switch (command.getType()) {//1.判断登陆类型
case INSERT: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
case UPDATE: {
Object param = 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);//2.执行查询,就是select
result = null;
} else if (method.returnsMany()) {//3.根据方法返回值判断,returnsMany()返回多个
result = executeForMany(sqlSession, args);//4.调了executeForMany进入
} 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);
if (method.returnsOptional()
&& (result == null || !method.getReturnType().equals(result.getClass()))) {
result = Optional.ofNullable(result);
}
}
break;
11.到了executeForMany
private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
List<E> result;
Object param = method.convertArgsToSqlCommandParam(args);//1.准备方法的参数
if (method.hasRowBounds()) {//2.因为查询全部,没有参数,所以参数是null
RowBounds rowBounds = method.extractRowBounds(args);
result = sqlSession.selectList(command.getName(), param, rowBounds);
} else {
result = sqlSession.selectList(command.getName(), param);//到了这里setlectList与
}
12.剩余请看步骤截图。与https://www.cnblogs.com/jxearlier/p/11625253.html从11步开始。
13.完整流程如下。

Mybatis映射器接口代理对象的方式 运行过程的更多相关文章
- Mybatis 映射器接口实现类的方式 运行过程debug分析
查询一张表的所有数据. 环境: 使用工具IntelliJ IDEA 2018.2版本. 创建Maven工程不用骨架 <?xml version="1.0" encoding= ...
- mybatis 映射器(mappers) 配置说明 加载映射文件方式
映射器(mappers) 既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要定义 SQL 映射语句了.但是首先我们需要告诉 MyBatis 到哪里去找到这些语句. Java 在自动查找这 ...
- MyBatis映射器(转载)
什么是MyBatis映射器? MyBatis框架包括两种类型的XML文件,一类是配置文件,即mybatis-config.xml,另外一类是映射文件,例如XXXMapper.xml等.在MyBatis ...
- MyBatis映射器(一)--多参数传递方式
在mybatis映射器的接口中,一般在查询时需要传递一些参数作为查询条件,有时候是一个,有时候是多个.当只有一个参数时,我们只要在sql中使用接口中的参数名称即可,但是如果是多个呢,就不能直接用参数名 ...
- mybatis 映射器
1 映射器 Mapper 是由java接口和 XML 文件共同组成.它的作用如下 1)定义参数类型 2)描述缓存 3)描述 SQL 语句 4)定义查询结果和POJO的映射关系 2 SqlSession ...
- mybatis映射器配置细则
前面三篇博客我们已经多次涉及到映射器的使用了,增删查基本上都用过一遍了,但是之前我们只是介绍了基本用法,实际上mybatis中映射器可以配置的地方还是非常多,今天我们就先来看看映射器还有哪些需要配置的 ...
- 【长文】Spring学习笔记(七):Mybatis映射器+动态SQL
1 概述 本文主要讲述了如何使用MyBatis中的映射器以及动态SQL的配置. 2 MyBatis配置文件概览 MyBatis配置文件主要属性如下: <settings>:相关设置,键值对 ...
- MyBatis映射器元素
映射器是MyBatis最强大的工具,也是我们使用MyBatis时用的最多的工具,映射器中主要有增删改查四大元素,来满足不同场景的需要: 下面是主要元素的介绍: select:查询语句 ...
- 阶段3 1.Mybatis_03.自定义Mybatis框架_2.自定义Mybatis的分析-创建代理对象的分析
如何创建代理对象,以及使用设计模式带来的优势 调用的组合关系 不关注的,执行JDBC那一套.第二个是解析XML,解析的技术有很多.
随机推荐
- ASS/SSA字幕格式
[时间:2019-04] [状态:Open] [关键词:字幕,ASS,SSA,文件格式,v4 Styles,字幕特效] 0 引言 SubStation Alpha(Sub Station Alpha) ...
- Acesrc and Travel(2019年杭电多校第八场06+HDU6662+换根dp)
题目链接 传送门 题意 两个绝顶聪明的人在树上玩博弈,规则是轮流选择下一个要到达的点,每达到一个点时,先手和后手分别获得\(a_i,b_i\)(到达这个点时两个人都会获得)的权值,已经经过的点无法再次 ...
- 资源管理(Resource Management),知识点
资料 网址 资源管理(Resource Management)服务 包含一系列支持企业IT治理的资源管理产品集合,主要包括资源组和资源目录.通过资源管理服务,您可以按照业务需要搭建合适的资源组织关系, ...
- JSPDF支持中文(思源黑体)采坑之旅,JSPDF中文字体乱码解决方案
我拍个砖,通常标称自己文章完美解决何种问题的,往往就是解决不了任何问题! 众所周知,JSPDF是一个开源的,易用的,但是对中文支持非常差的PDF库. 下面,我教大家,如何在pdf中使用思源黑体.思源黑 ...
- 元素的alt和title有什么异同?
①alt作为图片的替代文字出现,title作为图片的解释文字出现. ②alt属性应用较少,如img.area.input中,title应用较多,如a.form.input.还有div.p这些块级元素都 ...
- 第03组 Beta冲刺(1/4)
队名:不等式方程组 组长博客 作业博客 团队项目进度 组员一:张逸杰(组长) 过去两天完成的任务: 文字/口头描述: 制定了初步的项目计划,并开始学习一些推荐.搜索类算法 GitHub签入纪录: 暂无 ...
- 关于m3u8格式的视频文件ts转mp4下载和key加密问题
一,利用网站浏览器F12键,利用谷歌浏览器插件找到视频的.m3u8文件,并打开. 二,打开m3u8文件后,里面有很多.ts的链接,和key的链接. 三,保存为html文件,下载ts文件,代码如下:可加 ...
- Sc config http start= disabled
我不小心使用这个命令 Sc config http start= disabled, 现在http服务 无法启动 管理员运行 sc config http start= demand & ne ...
- ESRally压测ElasticSearch性能 CentOS 7.5 安装 Python3.7
1,CentOS 7.5 安装 Python3.7 1.安装开发者工具 yum -y groupinstall "Development Tools"2.安装Python编译依赖包 ...
- 【插件】【idea】Maven Helper查找maven依赖图
File->Settings->Plugins 打开pom文件,地下有一个Dependency Analyzer选项