说明java
MyBatis版本:3.5.1sql
相关历史文章(阅读本文以前,您可能须要先看下以前的系列)session
Spring Boot MyBatis最全教程:你值得拥有
MyBatis能脱离Spring吗一图纵览MyBatis的工做原理从源码看MyBatis,竟如此简单MyBatis的Mapper是什么`垃圾` mybatis
手写MyBatis,纯手工打造开源框架(第一篇:风云再起) app
手写MyBatis,纯手工打造开源框架(第二篇:君临天下) 框架
手写MyBatis,纯手工打造开源框架(第三篇:指挥若定) 测试
前言ui
指挥若定之中,决胜千里以外,是该作个告终了,把你的家伙掏出来,上刺刀。this
上一篇已经可以使用SqlSession进行查询返回结果了。这一篇咱们就是加入瑞士军刀Mapper。编码
1、分析
在SqlSession会提供getMapper的方法,在DefaultSqlSession会使用Proxy实例化一个MapperProxy代理,而MapperProxy代理会获取SqlSession,在这里进行Sql的操做,而后结果。
2、编码
2.1 MapperProxy
MapperProxy是mapper最终执行的核心:
package com.kfit.mybatis.session.impl; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.util.Collection; import com.kfit.mybatis.session.SqlSession; public class MapperProxy implements InvocationHandler{ private SqlSession sqlSession; public MapperProxy(SqlSession sqlSession) { this.sqlSession = sqlSession; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String statement = method.getDeclaringClass().getName()+"."+method.getName(); //isAssignableFrom方法是判断是否为某个类的父类 if(Collection.class.isAssignableFrom(method.getReturnType())) { //返回值是集合的话,那么是调用selectList return sqlSession.selectList(statement,null==args?null:args[0]); }else { return sqlSession.selectOne(statement,null==args?null:args[0]); } } }
说明:
(1)因为MapperProxy是一个代理类,因此须要实现接口InvocationHandler的Invoke方法。
(2)在Invoke方法中直接使用SqlSession进行执行,那么主要的核心就是要判断具体执行什么方法,这里如今经过返回值是不是集合来判断是不是执行selectOne仍是SelectList。
2.2 SqlSession
在SqlSession中添加getMapper方法:
public interface SqlSession { <T> T selectOne(String statement, Object parameter); <E> List<E> selectList(String statement); <E> List<E> selectList(String statement, Object parameter); <T> T getMapper(Class<T> type); }
在DefaultSqlSession中进行实现getMapper方法:
@SuppressWarnings("unchecked") public <T> T getMapper(Class<T> type) { T newProxyInstance = (T) Proxy.newProxyInstance(type.getClassLoader(),new Class[]{type},new MapperProxy(this)); return newProxyInstance; }
2.3 测试下
好了写段代码测试下吧:
public static void main(String[] args) { String resource = "mybatis-config.xml"; InputStream inputStream = App.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); System.out.println(sqlSessionFactory); System.out.println(sqlSessionFactory.getConfiguration().getJdbcProperties().getUrl()); SqlSession sqlSession = sqlSessionFactory.openSession(); Demo demo = null; List<Demo> demos = null; //使用Mapper DemoMapper demoMapper = sqlSession.getMapper(DemoMapper.class); demo = demoMapper.getById(1); System.out.println(demo); demos = demoMapper.getAll(); System.out.println(demos); }
运行看下结果:
Demo [id=1,name=张三1]
[Demo [id=1, name=张三1], Demo [id=9, name=张三], Demo [id=10, name=张三], Demo [id=11, name=张三], Demo [id=12, name=张三], Demo [id=13, name=张三]]
很明显执行的结果和直接使用SqlSession调用的结果是同样的。
好了有关手写MyBatis的文章就先到此告一段落了,经过手写mybatis,想必你们对于MyBatis的认知又高了一个等级了。
我就是我,是颜色不同的烟火。
我就是我,是不同凡响的小苹果。
à悟空学院:http://t.cn/Rg3fKJD
学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!
SpringBoot视频:http://t.cn/R3QepWG
Spring Cloud视频:http://t.cn/R3QeRZc
SpringBoot Shiro视频:http://t.cn/R3QDMbh
SpringBoot交流平台:http://t.cn/R3QDhU0
SpringData和JPA视频:http://t.cn/R1pSojf
SpringSecurity5.0视频:http://t.cn/EwlLjHh
Sharding-JDBC分库分表实战:http://t.cn/E4lpD6e