1.理解mongo加载过程
private final static DataSource ds = DataSourceConfig.getDataSource();
private final static Mango mango = Mango.newInstance(ds);
private final static UserDao dao = mango.create(UserDao.class);
@Test
public void testQueryInteger() throws Exception {
User user = createRandomUser(); int id = dao.insertUser(user); assertThat(dao.getIntegerId(id), equalTo(id)); }Connection conn = ds.getConnection();
private User createRandomUser() { Random r = new Random(); String name = Randoms.randomString(20); int age = r.nextInt(200); boolean gender = r.nextBoolean(); long money = r.nextInt(1000000); Date date = new Date(); User user = new User(name, age, gender, money, date); return user; }
}
public class User {
private int id;
private String name;
private int age;
private boolean gender;
private Long money;
private Date updateTime;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public User(String name, int age, boolean gender, long money, Date updateTime) {
this.name = name;
this.age = age;
this.gender = gender;
this.money = money;
this.updateTime = updateTime != null ? new Date(updateTime.getTime() / 1000 * 1000) : null; // 精确到秒
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public Long getMoney() {
return money;
}
public void setMoney(Long money) {
this.money = money;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
第一步 生成datasource
public static DataSource getDataSource(int i, boolean autoCommit, int maxActive) {
String driverClassName = getDriverClassName(i);
String url = getUrl(i);
String username = getUsername(i);
String password = getPassword(i);
BasicDataSource ds = new BasicDataSource();
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
ds.setInitialSize(1);
ds.setMaxActive(maxActive);
ds.setDriverClassName(driverClassName);
ds.setDefaultAutoCommit(autoCommit);
return ds;
}第二步 主要的,,由mongo.create(UserDao.class) 创建dao操作代理类
/**
* 创建代理DAO类
*/
public <T> T create(Class<T> daoClass) {
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 cacheAnno = daoClass.getAnnotation(Cache.class);
if (cacheAnno != null && cacheHandler == null) {
throw new IllegalStateException("if @Cache annotation on dao interface, " +
"cacheHandler can't be null");
}
if (dataSourceFactoryGroup == null) {
throw new IllegalArgumentException("please set dataSource or dataSourceFactory or dataSourceFactories");
}
MangoInvocationHandler handler = new MangoInvocationHandler(
dataSourceFactoryGroup, cacheHandler, interceptorChain, statCollector, this);
if (!isLazyInit) { // 不使用懒加载,则提前加载
Method[] methods = daoClass.getMethods();
for (Method method : methods) {
try {
handler.getOperator(method);
} catch (Throwable e) {
throw new InitializationException("initialize " + ToStringHelper.toString(method) + " error", e);
}
}
}
return Reflection.newProxy(daoClass, handler);
}
这里使用CacheLoader 来完成对userDao中的 @DB,@SQL 等注解的解析 MangoInvocationHandler 拦截器执行sql