MyBatis底层实现原理
如有侵权,请联系~
如有错误,也欢迎批评指正~
1、MyBatis实现基础
MyBatis的实现主要是动态代理和责任链模式。这两种技术不是本次介绍的重点,一笔带过。
1.1、动态代理
动态代理是在 Java 中一种强大的机制,允许在运行时创建代理实例,并根据应用需求动态定义代理的行为。在 Java 中,动态代理主要通过 java.lang.reflect.Proxy 类和 InvocationHandler 接口实现。
1.2、责任链
责任链设计模式其实开源代码、框架中都在使用。例如:Tomacat的valve、netty中的pipeline都是使用的责任链模式,具有易扩展、更加灵活的优点。
1.3、动态代理和责任链结合使用实例
以下是动态代理和责任链的基本使用步骤和示例。在看实例之前先看下整体的流程图,帮助理解:
1.3.1 定义接口
首先,您需要定义一个接口,代理将对其进行实现。
业务接口:
public interface UserService {
void addUser(String username);
void removeUser(String username);
}
下面不属于动态代理,定义一个类,封装对象反射执行方法
public class Invocation {
/**
* 目标对象
*/
private Object target;
/**
* 执行的方法
*/
private Method method;
/**
* 方法的参数
*/
private Object[] args;
public Invocation(Object target, Method method, Object[] args) {
this.target = target;
this.method = method;
this.args = args;
}
/**
* 执行目标对象的方法
*/
public Object invoke() throws Exception{
return method.invoke(target,args);
}
}
定义拦截器接口:
public interface Interceptor {
/**
* 具体拦截处理,目标对象封装到Invocation
*/
Object intercept(Invocation invocation) throws Exception;
/**
* 插入目标类,创建代理对象
*/
Object plugin(Object target);
}
1.3.2. 实现接口
业务逻辑实现:
public class UserServiceImpl implements UserService {
@Override
public void addUser(String username) {
System.out.println("User " + username + " added.");
}
@Override
public void removeUser(String username) {
System.out.println("User " + username + " removed.");
}
}
拦截器实现,在业务代码执行前后进行日志打印:
public class LogInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Exception{
System.out.println("------业务代码执行之前-------------");
Object result = invocation.process();
System.out.println("------业务代码执行之后-------------");
return result;
}
@Override
public Object plugin(Object target){
return UserServiceProxyHandler.wrap(target, this);
}
}
拦截器实现,在业务代码执行前后进行监控上报:
public class CatInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Exception{
System.out.println("------监控开启-------------");
Object result = invocation.process();
System.out.println("------监控结束-------------");
return result;
}
@Override
public Object plugin(Object target){
return UserServiceProxyHandler.wrap(target, this);
}
}
1.3.3 创建 InvocationHandler
实现 InvocationHandler 接口,以定义代理实例在调用方法时的行为。
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class UserServiceProxyHandler implements InvocationHandler {
private final UserService userService;
private final Interceptor interceptor;
public UserServiceProxyHandler(UserService userService, Interceptor interceptor) {
this.userService = userService;
this.interceptor = interceptor;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Invocation invocation = new Invocation(userService, method, args);
Object result = interceptor.intercept(invocation);
return result;
}
public static Object wrap(Object target,Interceptor interceptor) {
UserServiceProxyHandler targetProxy = new UserServiceProxyHandler(target, interceptor);
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),targetProxy);
}
}
1.3.4 使用动态代理和责任链
import java.lang.reflect.Proxy;
public class Main {
public static void main(String[] args) {
UserService realUserService = new UserServiceImpl();
LogInterceptor logInterceptor = new LogInterceptor();
realUserService = (UserService)logInterceptor.plugin(realUserService);
CatInterceptor catInterceptor = new CatInterceptor();
realUserService = (UserService)catInterceptor.plugin(realUserService);
// 使用代理实例
realUserService.addUser("张三");
realUserService.removeUser("张三");
}
}
2、MyBatis底层原理
其实MyBatis的源码整体流程和上述的流程图是一样的。
2.1 拦截器接口Interceptor
拦截器接口主要对目标对象的目标方法进行拦截,然后在拦截器方法中通过反射调用目标方法。
package org.apache.ibatis.plugin;
import java.util.Properties;
/**
* @author Clinton Begin
*/
public interface Interceptor {
// 拦截器的拦截方法,如果这个拦截器能过拦截到这个目标方法就会执行该方法
Object intercept(Invocation invocation) throws Throwable;
// 创建动态代理对象【封装原来对象】,当然也可以直接返回目标对象本身【这样就不会执行拦截方法了intercept】
Object plugin(Object target);
// 获取配置文件中的数据
void setProperties(Properties properties);
}
public class Invocation {
private final Object target;
private final Method method;
private final Object[] args;
public Invocation(Object target, Method method, Object[] args) {
this.target = target;
this.method = method;
this.args = args;
}
public Object getTarget() {
return target;
}
public Method getMethod() {
return method;
}
public Object[] getArgs() {
return args;
}
public Object proceed() throws InvocationTargetException, IllegalAccessException {
return method.invoke(target, args);
}
}
拦截器责任链:
public class InterceptorChain {
private final List<Interceptor> interceptors = new ArrayList<Interceptor>();
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
public void addInterceptor(Interceptor interceptor) {
interceptors.add(interceptor);
}
public List<Interceptor> getInterceptors() {
return Collections.unmodifiableList(interceptors);
}
}
2.2 插件Plugin
插件实现动态代理接口InvocationHandler,可以生成动态代理对象。当目标对象调用目标方法的时候,会执行到Plugin.invoke()方法,该方法会判断当前代理中的拦截器是否满足拦截目标对象的目标方法。
package org.apache.ibatis.plugin;
import org.apache.ibatis.reflection.ExceptionUtil;
/**
* @author Clinton Begin
*/
public class Plugin implements InvocationHandler {
// 目标对象或者目标对象的代理
private final Object target;
// 该动态代理的拦截器
private final Interceptor interceptor;
// 拦截器支持的相应类以及对应的方法
private final Map<Class<?>, Set<Method>> signatureMap;
private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
this.target = target;
this.interceptor = interceptor;
this.signatureMap = signatureMap;
}
// 将目标对象和拦截器封装成动态代理
public static Object wrap(Object target, Interceptor interceptor) {
// 通过拦截器上的注解Intercepts 获取这个拦截器支持的类及方法
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
Class<?> type = target.getClass();
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
if (interfaces.length > 0) {
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}
// 动态代理都会执行这个方法
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
// 判断当前拦截器是否能够拦截当前对象的方法
Set<Method> methods = signatureMap.get(method.getDeclaringClass());
if (methods != null && methods.contains(method)) {
return interceptor.intercept(new Invocation(target, method, args));
}
// 不满足拦截条件,直接执行放行
return method.invoke(target, args);
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
}
private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
// issue #251
if (interceptsAnnotation == null) {
throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
}
Signature[] sigs = interceptsAnnotation.value();
Map<Class<?>, Set<Method>> signatureMap = new HashMap<Class<?>, Set<Method>>();
for (Signature sig : sigs) {
Set<Method> methods = signatureMap.get(sig.type());
if (methods == null) {
methods = new HashSet<Method>();
signatureMap.put(sig.type(), methods);
}
try {
Method method = sig.type().getMethod(sig.method(), sig.args());
methods.add(method);
} catch (NoSuchMethodException e) {
throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
}
}
return signatureMap;
}
}
2.3 执行引擎Executor
上面讲了执行责任链和动态代理,但是最终的执行目标对象和目标方法是谁呢?
没错,就是Executor内部接口的方法,执行sql语句。Executor在执行sql语句的时候利用Configuration将注册的拦截器封装到StatementHandler,通过StatementHandler【子类如:PreparedStatement、SimpleStatementHandler等】获取连接【封装JDBC】执行sql。
public interface Executor {
ResultHandler NO_RESULT_HANDLER = null;
// 更新操作
int update(MappedStatement ms, Object parameter) throws SQLException;
// 查询操作
<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;
<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException;
<E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException;
List<BatchResult> flushStatements() throws SQLException;
void commit(boolean required) throws SQLException;
void rollback(boolean required) throws SQLException;
CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql);
boolean isCached(MappedStatement ms, CacheKey key);
void clearLocalCache();
void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType);
Transaction getTransaction();
void close(boolean forceRollback);
boolean isClosed();
void setExecutorWrapper(Executor executor);
}
2.4、整体执行流程
通过上述源码的展示,最终得到的执行流程图。