论坛首页 Java企业应用论坛

模仿Warp Dynamic Finder的Hibernate Dynamic Dao

浏览 13230 次
该帖已经被评为良好帖
作者 正文
   发表时间:2008-03-16  
看了Robbin前两天发的那帖,Warp framework - 一个相当有前途的Java轻量级Web开发框架(http://www.iteye.com/topic/168780),让人眼前一亮,特别是基于annotation的warp-dynamic-finder部分给人印象非常深刻,利用它,80%情况下Dao的实现不用去写了,只要定义个interface,加几个annotation轻松搞定,自己就着手实现了一个,基于spring的HibernateDaoSupport,其用法也很简单,如下:

public interface StudentDao {
	@Save
	Long save(Student student);

	@Delete
	void delete(Student student);

	@Get
	Student get(Long id);

	@Query("from Student as s where s.name = ? and s.age = ?")
	Student getByNameAndAge(String name, Integer age);//根据参数出现的次序绑定

	@Query("from Student as s where s.age > :age")
	List<Student> getStudentsAgeMoreThan(
			@Parameter("age") Integer age);//name绑定

	@Query("from Student as s where s.name like ?")
	Student getStudentNameLike(
			@Like(matchMode = MatchMode.START) String name);//支持like

	@Query(value = "delete Student where name = ?", executeUpdate = true)
	int deleteStudentByName(String name);//批量更新

	@Query("from Student as s order by s.id desc")
	@Conditions({ 
		@Condition("s.name = ?"), //动态条件添加,如果第一个参数不为null,该条件会被插入query string
		@Condition("s.age = ?") 
	})
	Student query(String name, Integer age);

}



这样一个Dao就定义OK了,非常容易,有两种使用方式,一种是利用AutoInjectDynamicDaoBeanPostProcessor,在spring中的bean初始化好之后,找到有InjectDao标注的方法就,利用动态代理生成StudentDao的代理实现并注入:

<bean class="com.norther.dynamic.dao.AutoInjectDynamicDaoBeanPostProcessor">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="studentService " class="com.norther.dynamic.dao.test.StudentService" />


public class StudentService {

	private StudentDao studentDao;

	@InjectDao//会被注入StudentDao动态代理实现
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}


还有另一种方式是基于FactoryBean的DynamicDaoProxy,并自己指定要实现的Dao接口,如下:
<bean id="studentDao " class="com.norther.dynamic.dao.DynamicDaoProxy">
	<property name="dao" value="com.norther.dynamic.dao.test.StudentDao" />
</bean>
<bean id="teacherDao " class="com.norther.dynamic.dao.DynamicDaoProxy">
	<property name="dao" value="com.norther.dynamic.dao.test.TeacherDao" />
</bean>



非常的简单,也很初级,有什么不足,请大家多多指教,呵呵。
  • dynamicdao.zip (43.4 KB)
  • 描述: 依赖Spring,Hibernate,测试部分依赖EasyMock,SpringMock,HSQLDB,JUnit
  • 下载次数: 336
   发表时间:2008-03-16  
count(case when aaa='555' then aaa end) from .....

这样的什么方法处理
0 请登录后投票
   发表时间:2008-03-16  
剑 事 写道
count(case when aaa='555' then aaa end) from .....

这样的什么方法处理


dynamic dao只是对hibernate进行封装,这个当然可以做到,例如

public interface StudentDao {
     
     @Query(
         value = "count(case when aaa=:value then aaa end) from .....",
         sqlQuery = true
         )
     public Integer count(
           @Parameter(name="value") String aaa);


}
0 请登录后投票
   发表时间:2008-03-16  
看过源代码了,annotation的确用的很不错
8 请登录后投票
   发表时间:2008-03-17  
现在的技术发展真快,多亏有这么多懒人。编码越来越省事了。
0 请登录后投票
   发表时间:2008-03-17  
DAO可以认为是一种模式
模式一般是大家都认可的一种编程方式,习惯,经验的总结
模式的应用往往是运行效率的降低,但是可维护性提高了不少
大家都理解同一种模式的时候,你跟别人交流,也就只需要说一下模式的名字,而不需要特地解释。
当你使用DAO的时候就是希望,你只告诉别人说“我这里使用了DAO”就不再需要告诉别人怎么实现,因为对方理解DAO,

我现在就说这些,另外我想说,等东部老大来消灭你,看你22:0得意的样
0 请登录后投票
   发表时间:2008-03-17  
能不能提供demo的相关jar文件,谢谢!
0 请登录后投票
   发表时间:2008-03-17  
qilei 写道
能不能提供demo的相关jar文件,谢谢!

hibernate,hibernate-annotation,ejb3-persistent这几个在hibernate官方网站都能找到,

hsqldb在http://hsqldb.org/可以下载,是一个轻量的数据库

剩下的google随便一搜索就有了 : )
0 请登录后投票
   发表时间:2008-03-18  
不知道为什么.本人非常不喜欢使用过多的xml配置.
0 请登录后投票
   发表时间:2008-03-19  
seasar下面的一个子项目s2dao也是基于annotation的dao层实现,设计思想采用了ror约定大于配置的概念。初看起来很简单,不过在实际应用中,到了后期还是遇到了很多问题。我觉得一个企业开发中,最卡时间的不是这些类似与hello world的实现。而是对于复杂问题的可扩展性。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics