Mybatis系列十八:源码解析-getMapper

mybatis全局配置文件mybatis-config.xml

<?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">
<configuration>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>
	<!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->
	<mappers>
		<mapper resource="EmployeeMapper.xml" />
	</mappers>
</configuration>

mapper映射文件EmployeeMapper.xml

<?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="com.atguigu.mybatis.dao.EmployeeMapper">
<!-- 
namespace:名称空间;指定为接口的全类名
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
 -->

	<select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">
		select id,last_name lastName,email,gender from tbl_employee where id = #{id}
	</select>
</mapper>

测试方法:

	/**
	 * 1、获取sqlSessionFactory对象
	 * 		解析文件的每一个信息保存在Configuration中,然后返回包含Configuration的DefaultSqlSession。
	 * 		MappedStatement:代表一个增删改查的详细信息。
	 * 2、获取sqlSession对象
	 * 		 返回一个SqlSession 的实现类DefaultSqlSession对象。
	 * 		 这一步会创建Executor 对象。
	 * 3、获取接口的代理对象(MapperProxy)
	 *  	getMapper,使用MapperProxyFactory创建一个MapperProxy的代理对象。
	 *  	代理对象里面包含了,DefaultSqlSession(Executor )
	 * 4、执行增删改查方法
	 * @throws IOException
	 */
	@Test
	public void test() throws IOException {
		//1、获取sqlSessionFactory对象
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		//2、获取sqlSession对象
		SqlSession openSession = sqlSessionFactory.openSession();
		try {
			//3、获取接口的代理对象
			//会为接口自动创建一个代理对象,代理对象去执行增删改查方法
			EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
			//4、执行增删改查方法
			Employee employee = mapper.getEmpById(1);
			System.out.println(mapper);
			System.out.println(employee);
		} finally {
			openSession.close();
		}
	}

在这里插入图片描述
源码解析:
三、获取到接口的代理对象

	EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);

在这里插入图片描述
1、DefaultSqlSession

	public <T> T getMapper(Class<T> type) {
        return this.configuration.getMapper(type, this);
    }

2、Configuration

	public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
        return this.mapperRegistry.getMapper(type, sqlSession);
    }

3、MapperRegistry

	public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
		//获取Mapper的代理工厂MapperProxyFactory
        MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory)this.knownMappers.get(type);
        if (mapperProxyFactory == null) {
            throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
        } else {
            try {
            	//传入sqlSession创建一个MapperProxy
                return mapperProxyFactory.newInstance(sqlSession);
            } catch (Exception var5) {
                throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
            }
        }
    }

4、MapperProxyFactory

	//根据传入的MapperProxy创建一个MapperProxy的代理对象
 	protected T newInstance(MapperProxy<T> mapperProxy) {
        return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
    }
    
	public T newInstance(SqlSession sqlSession) {
        MapperProxy<T> mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
        return this.newInstance(mapperProxy);
    }

5、MapperProxy

//MapperProxy实现了InvocationHandler。InvocationHandler是Jdk用来创建动态代理的
public class MapperProxy<T> implements InvocationHandler, Serializable {
    private static final long serialVersionUID = -6424540398559729838L;
    private final SqlSession sqlSession;
    private final Class<T> mapperInterface;
    private final Map<Method, MapperMethod> methodCache;

    public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
        this.methodCache = methodCache;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (Object.class.equals(method.getDeclaringClass())) {
            try {
                return method.invoke(this, args);
            } catch (Throwable var5) {
                throw ExceptionUtil.unwrapThrowable(var5);
            }
        } else {
            MapperMethod mapperMethod = this.cachedMapperMethod(method);
            return mapperMethod.execute(this.sqlSession, args);
        }
    }

    private MapperMethod cachedMapperMethod(Method method) {
        MapperMethod mapperMethod = (MapperMethod)this.methodCache.get(method);
        if (mapperMethod == null) {
            mapperMethod = new MapperMethod(this.mapperInterface, method, this.sqlSession.getConfiguration());
            this.methodCache.put(method, mapperMethod);
        }

        return mapperMethod;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在您提供的引用中,出现了一个错误信息"Cannot resolve plugin org.mybatis.generator:mybatis-generator-maven-plugin:1.3.0"。这个错误通常表示Maven无法解析或找到所需的插件。根据您的引用内容,我可以提供以下解决方法: 1. 确保您的Maven配置文件中的依赖项正确配置。检查您的pom.xml文件中是否添加了正确的插件依赖以及其版本号。 2. 检查您的Maven仓库中是否存在所需的插件。您可以尝试删除Maven本地仓库中与该插件相关的文件,然后重新运行Maven命令,以便重新下载并安装插件。 3. 检查您的网络连接是否正常。有时候插件无法下载是由于网络问题导致的。您可以尝试使用其他网络或者使用代理进行连接。 4. 尝试使用更新的插件版本。您提供的引用中使用的是1.3.0版本的插件,尝试使用更高版本的插件,可能会解决该问题。 总结起来,要解决"Cannot resolve plugin org.mybatis.generator:mybatis-generator-maven-plugin:1.3.0"的问题,您可以检查Maven配置文件、Maven仓库、网络连接并尝试更新插件版本。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate (default-](https://blog.csdn.net/qq_52291182/article/details/121033304)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Maven更新失败,Cannot resolve plugin org.apache.maven.plugins:maven-compiler-plugin:3.1](https://download.csdn.net/download/weixin_38687277/14888111)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值