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,解析的技术有很多.
随机推荐
- 两数相加[链表加法] LeetCode.2
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- jdbc笔记2
private static String driver; private static String url; private static String username; private sta ...
- openpose开发(1)官方1.5版本源码编译
环境 WIN10系统,联想Y7000配置,8G内存 VS2019 cuda10 cudnn10 opencv4.11没有扩展库 显卡 1050TI 用到的库(提前下载好的模型,依赖库,user_cod ...
- restql 学习三 查询语言简单说明
restql 中with 参数的内容在restql 中实际上就是http request 的请求内容.按照restql 的设计 method 分为 from (get) , to (post) ,in ...
- Burp Suite 入门教程(BURP SUITE TUTORIAL )
参考链接1:https://www.pentestgeek.com/what-is-burpsuite 参考链接2:https://www.pentestgeek.com/web-applicatio ...
- 微信小程序跳转函数总结
微信小程序跳转函数总结 笔者在微信小程序前端的开发过程中,在不同的情况下遇到了需要使用不同的页面跳转逻辑的情况,以下是我对这些函数的使用场景的一个总结介绍. wx.navigateTo 这是最常用 ...
- kibana We couldn't activate monitoring
调节一下监控状态查询的时间范围,有时候,刚启动监控,数据没有生成.把”last 1 hour“改成 具体有数据的时间 用如下语句查看,监控日志在不断生成.重启kibana后正常有监控画面了. GET ...
- winform自定义分页控件
1.控件代码: public partial class PagerControl : UserControl { #region 构造函数 public PagerControl() { Initi ...
- VS2019调试 asp.net core 2.2 出现《ANCM In-Process Handler Load Failure 发布后启动错误处理》处理
从 google 出来的 github 上 AspNetCore issues 和 stackoverflow 搜到的,百度 博客园搜到的,CSDN 搜到的,统统设置了,不管用. 从这些问题 ...
- ASP.NET Core WebApi构建API接口服务实战演练
一.ASP.NET Core WebApi课程介绍 人生苦短,我用.NET Core!提到Api接口,一般会想到以前用到的WebService和WCF服务,这三个技术都是用来创建服务接口,只不过Web ...