您的位置:首页 > 编程语言

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

看代码实现

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也可以做,就不再深究了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Mango