Mango源码分析2: basic代码分析
2017-06-04 00:00
393 查看
我们先来看basic的代码,
jdb org.jfaster.mango.example.basic.UserMain
stop in org.jfaster.mango.example.basic.UserMain.main
run
看代码实现
然后,
其实执行了
接下来执行 UserDao dao = mango.create(UserDao.class);这个跟mybatis的有点类似
就如mybatis一样,生成了一个接口相关的类后,就可以进行一些操作了
===
===
===
感觉这些mybatis也可以做,就不再深究了。
jdb org.jfaster.mango.example.basic.UserMain
stop in org.jfaster.mango.example.basic.UserMain.main
run
看代码实现
String driverClassName = "com.mysql.jdbc.Driver"; // 常见的一些配置 String url = "jdbc:mysql://localhost:3306/mango_example"; String username = "root"; // 这里请使用您自己的用户名 String password = "root"; // 这里请使用您自己的密码 //构造成1个对象 DataSource ds = new DriverManagerDataSource(driverClassName, url, username, password);
然后,
Mango mango = Mango.newInstance(ds); // 使用数据源初始化mango
其实执行了
Mango mango = new Mango();
public static Mango newInstance(DataSource dataSource) { //创建1个mango实例对象 Mango mango = newInstance(); //设置数据源 mango.setDataSource(dataSource); //返回 return mango; }
接下来执行 UserDao dao = mango.create(UserDao.class);这个跟mybatis的有点类似
/** * 创建代理DAO类 */ public <T> T create(Class<T> daoClass) { // 一个check的过程 if (daoClass == null) { throw new NullPointerException("dao interface can't be null"); } if (!daoClass.isInterface()) { throw new IllegalArgumentException("expected an interface to proxy, but " + daoClass); } // 确保注解存在 DB dbAnno = daoClass.getAnnotation(DB.class); if (dbAnno == null) { throw new IllegalStateException("dao interface expected one @DB " + "annotation but not found"); } // 检查Cache注解 Cache cacheAnno = daoClass.getAnnotation(Cache.class); if (cacheAnno != null && cacheHandler == null) { throw new IllegalStateException("if @Cache annotation on dao interface, " + "cacheHandler can't be null"); } // 确保dataSourceFactoryGroup已经初始化了 if (dataSourceFactoryGroup == null) { throw new IllegalArgumentException("please set dataSource or dataSourceFactory or dataSourceFactories"); } // 生成1个MangoInvocationHandler MangoInvocationHandler handler = new MangoInvocationHandler(daoClass, dataSourceFactoryGroup, cacheHandler, interceptorChain, statCollector, this); // if (!isLazyInit) { // 不使用懒加载,则提前加载 // 获取这个类的所有方法 List<Method> methods = Methods.listMethods(daoClass); // 遍历每1个方法 for (Method method : methods) { try { // 交给handler处理 handler.getOperator(method); } catch (Throwable e) { throw new InitializationException("initialize " + ToStringHelper.toString(method) + " error", e); } } } //生成反射对象 return Reflection.newProxy(daoClass, handler); }
就如mybatis一样,生成了一个接口相关的类后,就可以进行一些操作了
===
String name = "ash"; int age = 28; boolean gender = true; long money = 100; Date updateTime = new Date(); dao.insertUser(name, age, gender, money, updateTime); @SQL("insert into user(name, age, gender, money, update_time) values(:1, :2, :3, :4, :5)") public void insertUser(String name, int age, boolean gender, long money, Date updateTime);
===
User user = new User(); user.setName(name); user.setAge(age); user.setGender(gender); user.setMoney(money); user.setUpdateTime(updateTime); @ReturnGeneratedId @SQL("insert into user(name, age, gender, money, update_time) " + "values(:1.name, :1.age, :1.gender, :1.money, :1.updateTime)") public int insertUser(User user);
===
dao.deleteUser(id); @SQL("delete from user where id=:1") public int deleteUser(int id);
感觉这些mybatis也可以做,就不再深究了。
相关文章推荐
- 应用框架的设计与实现——.NET平台(4.3 SAF代码分析.源码2)
- 开放源代码的全文检索引擎Lucene――介绍、系统结构与源码实现分析
- Struts源码粗略分析三:代码剖析
- ExtJs源码分析与学习—ExtJs核心代码扩展
- ExtJs源码分析与学习—ExtJs核心代码(一)
- 关于Basic程序解释器及编译原理的简单化(1)---Basic器的语法分析及主要代码
- Zebra-VTYSH源码分析和改造(二):深入代码
- 从分析家中得到股票数据!C#源码 (代码很长,建议火狐)
- Uboot 源码分析----代码整体结构分析
- ExtJs源码分析与学习—ExtJs核心代码(二)
- Linux内核2.6.14源码分析-双向循环链表代码分析(巨详细)
- Linux内核源码分析-链表代码分析
- Linux内核2.6.14源码分析-双向循环链表代码分析(巨详细)
- Struts源码粗略分析二:代码初探
- u-boot源码分析 --- 启动第二阶段 ,基于2410 启动代码 分析
- 关于Basic程序解释器及编译原理的简单化(1)---Basic器的语法分析及主要代码
- C#分析数据库结构,使用XSL模板自动生成代码 - 清清月儿 .NET万花筒 Asp.net技术 Asp.net教程 Asp.net源码 Asp.net基础 Asp.net控件 Asp.net入门 - CSDNBlog
- EntLib.com Forum / YAF 开源论坛--源码的目录结构(对分析代码很有帮助)
- GCC Coverage代码分析-从GCC源码中抽取gcov/gcov-dump程序
- Luncher修改wallpaper(壁纸)源码跟踪(代码实现过程分析)