mongo(一)

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 {
  Connection conn = ds.getConnection();
 
User user = createRandomUser(); int id = dao.insertUser(user); assertThat(dao.getIntegerId(id), equalTo(id)); }

  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值