Explanation
MyBatis Version: 3.5.1
Relevant historical articles ( Before reading this article, you may need to look at before the next series )
Spring Boot MyBatis most complete tutorial: you deserve
MyBatis can do outside of the Spring a diagram of an overview of works MyBatis from source to see MyBatis, is much easier MyBatis Mapper is what the `` garbage
Handwritten MyBatis, hand-built open-source framework (first post: revival)
Handwritten MyBatis, hand-built open-source framework (Second: Harborside)
Handwritten MyBatis, hand-built open-source framework (Title III: strategizing)
Foreword
Devised strategies, winning thousands of miles away, which is to be settled, and you guys can pull out the bayonet.
Previous SqlSession have been able to return results using a query. This one we are joined Swiss Army Knife Mapper.
First, analysis
In getMapper SqlSession provides a method, in DefaultSqlSession use of a Proxy instance MapperProxy agent, the agent obtains the MapperProxy SqlSession, where Sql operation, and then the result.
Second, coding
2.1 MapperProxy
MapperProxy is the core mapper final execution of:
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