@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Select {
public String value();
}

  

public interface UserDao {

    @Select("select * from t_user where tid = #{i}")
public List<Map> query(int i);
}

  

public class UserDaoInvocationHandle implements InvocationHandler {

    public SqlSession sqlSession;

    public UserDaoInvocationHandle(SqlSession sqlSession){
this.sqlSession = sqlSession; }
//使用管理者返回方法返回返回值
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(args);
}
}

  

public class Test {

    public static void main(String args[]){

        //模拟
try {
init();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} //创建sqlSession
SqlSession sqlSession = GetSqlSession.getSqlSession(); //创建Handle
UserDaoInvocationHandle h = new UserDaoInvocationHandle(sqlSession); //使用jdk代理 1ClassLoader 2Class[] 3Handle
UserDao ud =(UserDao) Proxy.newProxyInstance(UserDao.class.getClassLoader(),new Class[]{UserDao.class},h); //此时的ud就是由JDK代理得到的代理class
System.out.println(mapperStatement);
//当使用ud.query()方法的时候,就是将ud的class路径+名字+方法的名字当做参数去mapperStatement里面获取到sql
//再由Executor去执行sql语句并返回结果,其中mybatis还有缓存机制,会将sql语句当做缓存的id保存结果。
} public static Map<String,String> mapperStatement = new HashMap<String, String>();
/**
* 模拟初始化将Mapper.xml中命名空间和方法名字当作kay,sql语句当作value保存到MapperStatement中(请回顾mybatis-4中总结的初始化)
* 初始化信息(数据库连接信息,扫描mapper包中的class用于创建bean对象,spring中的类applicationFactory用于创建变对象、mapper中的xml的id与sql放到MapperStatement对象中)
*       其中对于扫描mapper包中的class路径+参数basePackages转成文件夹,然后循环找到所有的类名,使用.......(请看MapperScannerRegistrar引出的doScan方法)
* */
public static void init() throws ClassNotFoundException {
Class clazz = Class.forName("com.dao.UserDao");
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
//判断UserDao中是否存在Select类的注解
if(method.isAnnotationPresent(Select.class)){
//将获取这个注解类
Select select = method.getAnnotation(Select.class);
//这里获取的是java.dao.UserDao.query
String key = method.getDeclaringClass().getName()+"."+method.getName();
//这里最终实现的就是启动的时候将命名空间和方法名当作key使用,SQL语句当作Value
mapperStatement.put(key,select.value());
}
}
}
}

  

最新文章

  1. Spark踩坑记——Spark Streaming+Kafka
  2. Qt——组件位置随窗口变化
  3. JS获取Url中传入的参数
  4. oracle10g在rh6上安装缺少libXtst*的包导致不能出OUI
  5. MySQL-(Master-Slave)配置
  6. MongoDB固定集合
  7. Mistral 工作流组件之一 概述
  8. bnuoj 4207 台风(模拟题)
  9. [Redux] Colocating Selectors with Reducers
  10. VMware Workstation 11序列号一枚
  11. JS数组常用方法
  12. HTML5画布(矩形)
  13. JavaScript :memory leak [转]
  14. Allegro pcb -等长设计
  15. 对sppnet网络的理解
  16. div里面的图片垂直居中
  17. 爬虫IP代理中的http与https
  18. @Value(&quot;${xxxx}&quot;)注解的配置及使用
  19. 【Thinking in Java】读书笔记
  20. Android 上传大文件

热门文章

  1. 库&amp;框架-----CDN网络引用总结
  2. 任务38:JWT 设计解析及定制
  3. C++笔试题(七)
  4. C++笔试题(三)
  5. 使用PDO操作数据库的好处
  6. 鸟哥私房菜基础篇:安裝 CentOS7.x习题
  7. _bzoj1911 [Apio2010]特别行动队【斜率优化dp】
  8. 洛谷 P3768 简单的数学题
  9. 因磁盘空间不足导致HDFS的NameNode进入安全模式问题记录
  10. 设计模式(3)-- 原型模式 (clone分析)