`
非飞
  • 浏览: 79351 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Simple ActiveRecord

    博客分类:
  • Java
阅读更多
研究了几天Guice和NetMind,于是针对前段时间javaeye中关于ActiveRecord的讨论实现了一个简单的ActiveRecord模型。

Guice: http://code.google.com/p/google-guice/
NetMind: http://netmind.hu/persistence/

java 代码
 
  1. public class ActiveRecord {  
  2.    // Guice 注入  
  3.     @Inject  
  4.     protected static transient Store store;  
  5.      
  6.     public void save() {  
  7.        store.save(this);  
  8.     }  
  9.   
  10.     public void delete() {  
  11.         store.remove(this);  
  12.     }  
  13.      
  14.    /** 
  15.      * NetMind 自动生成的ID 
  16.      */  
  17.     public Long getPersistenceId() {  
  18.         return this.persistenceId;  
  19.     }  
  20. }  
  21.   
  22. public class User extends ActiveRecord {  
  23.     private String name;  
  24.   
  25.     public User() {}  
  26.     public User(String name) {  
  27.        this.name = name;  
  28.     }  
  29.   
  30.     public String getName() {  
  31.        return this.name;  
  32.     }  
  33.      
  34.     public void setName(String name) {  
  35.        this.name = name;  
  36.     }  
  37.   
  38.     @SuppressWarning("unchecked")  
  39.     public static List findAll() {  
  40.        return (List<User>)store.find("find user");  
  41.     }  
  42.   
  43.     public static User findByName(String name) {  
  44.        return (User)store.findSingle("find user where name=?"new Object[] {name});  
  45.     }  
  46. }  
  47.   
  48. /** 
  49.   * Guice Module,使用struts2的时候可以在struts.xml中设置该module。 
  50.   *  
  51.   * <constant name="guice.module"  value="temp.TempModule"/> 
  52.   * 
  53.   * 目前下载的guice-struts-plugin有一个bug,无法加载用户自定义的module,需要修改一下源代码。 
  54.   * 找到com.google.guice.inject.struts2.GuiceObjectFactory中下面这段代码,并添加一行加载用户自定义 
  55.   * module的代码即可。 
  56.   * 
  57.   *   this.injector = Guice.createInjector(new AbstractModule() { 
  58.   *         protected void configure() { 
  59.   *            install(new ServletModule()); 
  60.   *            // 需要添加的代码 
  61.   *            if(module != null) install(module); 
  62.   *            // Tell the injector about all the action classes, etc., so it 
  63.   *            // can validate them at startup. 
  64.   *            for (Class<?> boundClass : boundClasses) { 
  65.   *              bind(boundClass); 
  66.   *            } 
  67.   *          } 
  68.   *        }); 
  69.   */  
  70. public class ExampleModule extends AbstractModule {  
  71.     protected void configure() {  
  72.         bind(Store.class).toInstance(new Store("org.hsqldb.jdbcDriver""jdbc:hsqldb:file:temp"));  
  73.         binder().requestStaticInjection(ActiveRecord.class);  
  74.     }  
  75. }  

<user><user></user>
分享到:
评论
2 楼 Arden 2007-06-04  
与其自己实现,不如直接用Grails或者JRuby吧!
1 楼 yy77 2007-06-04  
这个和activeRecord好像不太一样啊?
如果但是注入一个store对象,然后通过store来进行数据库persistence的话,那我还不如直接用spring的HibernateSupport类呢,也能够完成上面的功能啊。

相关推荐

Global site tag (gtag.js) - Google Analytics