Mybatis源码解析5—— 接口代理
Mybatis源码解析5—— 接口代理
本篇文章,可乐将为大家介绍通过接口代理的方式去执行SQL操作。话不多说,直接上图:
其实无论哪种方式,我们最终是需要找到对应的 SQL 语句,接口代理的方式就是通过 【包名.方法名】 的方式,去找到 xxxMapper.xml 文件中的 SQL 语句。
很明显,通过动态代理的方式,我们能够实现该功能。下面,可乐将为大家手撸一个 Mybatis 的接口代理。
1、创建接口
package com.itcoke.mapperproxy;
import com.itcoke.bean.Person;
public interface PersonMapper {
Person selectPersonById(Long pid);
}
2、创建代理类
package com.itcoke.mapperproxy;
import org.apache.ibatis.session.SqlSession;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MapperProxyHandler implements InvocationHandler {
private SqlSession sqlSession;
private Class<?> targetInterface;
public MapperProxyHandler(SqlSession sqlSession,Class<?> targetInterface){
this.sqlSession = sqlSession;
this.targetInterface = targetInterface;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String className = targetInterface.getName();
String methodName = method.getName();
String statement = className + "." + methodName;
return sqlSession.selectOne(statement,args[0]);
}
}
3、创建代理工厂类
package com.itcoke.mapperproxy;
import java.lang.reflect.Proxy;
public class MapperProxyFactory {
private Class<?> targetInterface;
public MapperProxyFactory(Class<?> targetInterface){
this.targetInterface = targetInterface;
}
public Object newInstance(MapperProxyHandler handler){
return Proxy.newProxyInstance(targetInterface.getClassLoader(),
new Class[]{targetInterface},
handler);
}
}
4、创建测试类
package com.itcoke.mapperproxy;
import com.itcoke.bean.Person;
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 java.io.IOException;
import java.io.InputStream;
public class MapperProxyTest {
public static void main(String[] args) {
// 1、获取目标接口对象
Class<?> targetInterface = PersonMapper.class;
// 2、获取 SqlSession 对象
SqlSession sqlSession = getSqlSession();
MapperProxyHandler proxyHandler = new MapperProxyHandler(sqlSession,targetInterface);
MapperProxyFactory mapperProxyFactory = new MapperProxyFactory(PersonMapper.class);
PersonMapper personMapper = (PersonMapper)mapperProxyFactory.newInstance(proxyHandler);
Person person = personMapper.selectPersonById(1L);
System.out.println(person);
}
public static SqlSession getSqlSession() {
//定义mybatis全局配置文件
String resource = "mybatis-config.xml";
//加载 mybatis 全局配置文件
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sessionFactory.openSession();
}
}
5、总结
其实 Mybatis 内部实现方式大体上和上面差不多,在加入一些类型处理器,其实就是一个简易版本的 Mybatis