模拟mybatis代理

本文详细解析了如何使用JDK动态代理来模拟MyBatis的代理机制,通过创建代理工厂类、代理类及核心执行方法,实现了接口方法与配置文件中SQL语句的映射。测试类展示了整个流程,从初始化接口与SQL的关系到具体方法调用。

记录:
        理解与整理.
备注:模拟mybatis代理只是记录而已20191227.
理解:
        <1>.使用jdk代理,简化模拟mybatis代理实现
        <2>.mybatis没有为接口去反射实现类
        <3>.mybatis是在代理的执行方法中,使用代理方式设计了特有策略,根据接口的方法名,匹配对应配置文件中的sql.
模拟测试:
1.接口Topic

public interface Topic {
  public void getInfo(String size);
  public void getHeight();
}

2.代理工厂类

public class SimulationMapperProxyFactory<T> {
  private final Class<T> mapperInterface;
  private final Map<Method, String> methodCache = new ConcurrentHashMap<>();
  public SimulationMapperProxyFactory(Class<T> mapperInterface) {
  	this.mapperInterface = mapperInterface;
  }
  @SuppressWarnings("unchecked")
  protected T newInstance(SimulationMapperProxy<T> mapperProxy) {
  	return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{ mapperInterface },mapperProxy);
  }
  public T newInstance(Map sqlSession) {
  	final SimulationMapperProxy<T> mapperProxy = new SimulationMapperProxy<T>(sqlSession, mapperInterface, methodCache);
  	return newInstance(mapperProxy);
  }
}

3.代理类
        jdk动态代理需要实现InvocationHandler接口.

public class SimulationMapperProxy<T> implements InvocationHandler, Serializable {	
  private final Map sqlSession;
  private final Class<T> mapperInterface;
  private final Map<Method, String> methodCache;
  public SimulationMapperProxy(Map sqlSession, Class<T> mapperInterface, Map<Method, String> methodCache) {
  	this.sqlSession = sqlSession;
  	this.mapperInterface = mapperInterface;
  	this.methodCache = methodCache;
  }
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  	System.out.println("SimulationMapperProxy->invoke开始...");
  	if (Object.class.equals(method.getDeclaringClass())) {
  	  try {
  	  	return method.invoke(this, args);
  	  } catch (Throwable t) {
  	  	;
  	  }
  	}
  	final SimulationMapperMethod mapperMethod = cachedMapperMethod(method);
  	Object obj =mapperMethod.execute(sqlSession, args);
  	System.out.println("SimulationMapperProxy->invoke结束...");
  	return obj;
  }
  private SimulationMapperMethod cachedMapperMethod(Method method) {
  	SimulationMapperMethod mapperMethod = new SimulationMapperMethod(method);
  	return mapperMethod;
  }
}

4.代理类需要代理的核心内容

public class SimulationMapperMethod {
  private Method method;
  public SimulationMapperMethod(Method method){
  	this.method = method;
  }
  public Object execute(Map sqlSession, Object[] args) {
  	System.out.println("SimulationMapperMethod->execute开始......");
  	System.out.println("在代理方法中并不实现接口类,而是根据接口调用的方法去匹配对应的配置文件中与之绑定的执行sql.");
  	System.out.println("接口方法:"+ this.method.getName());
  	System.out.println("匹配策略: 根据调用的方法匹配执行的sql.");
  	System.out.println("真正执行的sql:" + sqlSession.get(this.method.getName()));
  	System.out.println("SimulationMapperMethod->execute结束......");
  	return null;
  }
}

5.测试类
        init方法,模拟mybatis启动后,存储了加载的配置文件与接口方法对应关系.
        main方法,测试入口.

public class TestMainDemo {
  public static void main(String [] args){
  	System.out.println("测试开始......");
  	Map sqlSession = init();
  	System.out.println("");
  	SimulationMapperProxyFactory<Topic> zbMapperProxyFactory = new SimulationMapperProxyFactory(Topic.class);
  	Topic topic = zbMapperProxyFactory.newInstance(sqlSession);
  	System.out.println("开始调用topic.getInfo......");
  	topic.getInfo("G");
  	System.out.println("结束调用topic.getInfo......");
  	System.out.println("");
  	System.out.println("开始调用topic.getHeight......");
  	topic.getHeight();
  	System.out.println("结束调用topic.getHeight......");
  	System.out.println("");
  	System.out.println("测试结束......");
  }
/**模拟:初始化接口与mapper中对应sql的关系*/
public static Map init(){
  System.out.println("模拟:初始化接口与mapper中对应sql的关系");
  Map sqlSession = new HashMap<String,String>();
  String sql = "select * from tb_demo";
  String sqlHeight = "select * from tb_height";
  sqlSession.put("getInfo", sql);
  sqlSession.put("getHeight", sqlHeight);
  return sqlSession;
  }
}

以上,感谢.

【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器模拟器的研究展开,重点介绍了基于Matlab的建模与仿真方法。通过对四轴飞行器的动力学特性进行分析,构建了非线性状态空间模型,并实现了姿态与位置的动态模拟。研究涵盖了飞行器运动方程的建立、控制系统设计及数值仿真验证等环节,突出非线性系统的精确建模与仿真优势,有助于深入理解飞行器在复杂工况下的行为特征。此外,文中还提到了多种配套技术如PID控制、状态估计与路径规划等,展示了Matlab在航空航天仿真中的综合应用能力。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的高校学生、科研人员及从事无人机系统开发的工程技术人员,尤其适合研究生及以上层次的研究者。; 使用场景及目标:①用于四轴飞行器控制系统的设计与验证,支持算法快速原型开发;②作为教学工具帮助理解非线性动力学系统建模与仿真过程;③支撑科研项目中对飞行器姿态控制、轨迹跟踪等问题的深入研究; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注动力学建模与控制模块的实现细节,同时可延伸学习文档中提及的PID控制、状态估计等相关技术内容,以全面提升系统仿真与分析能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值