Light-commons 加入orm模块

light-commons 计划创建一个ORM通用接口层,并提供一个jdbc实现和一个hibernate实现。今天创建了接口以及Hibernate的实现。

1. DaoTemplate

HibernateDaoTemplate daoTemplate = new HibernateDaoTemplate(sessionFactory);
//与spring的 hibernateTemplate 类似
//MyPojo mypojo = (MyPojo) hibernateTemplate.get(MyPojo.class,1L);
//与spring的HibernateTemplate不同的是,这里不需要类型转换
MyPojo mypojo = daoTemplate.get(MyPojo.class,1L);


2. Dao

public class MyPojoDao extends HibernateDao<MyPojo,Long>{
//必要的参数在构造函数里传入,而不是用set方法注入,保证thread-safe
public MyPojoDao(HibernateDaoTemplate daoTemplate){
super(daoTemplate);
}
//that's all,当然,你也可以在这里加入自己的方法。
}

...
MyPojoDao myPojoDao = new MyPojoDao(hibernateDaoTemplate);
//不需要类型转换
MyPojo myPojo = myPojoDao.get(1L);



3. DaoFactory

HibernateDaoFactory hibernateDaoFactory=new HibernateDaoFactory(sessionFactory);
//指定Entity Class, 指定 ID Class
Dao<MyPojo, Long> myPojoDao= hibernateDaoFactory.getDaoOf(MyPojo.class);
//以get方法为例
//参数仅允许 ID Class ,上面指定的是Long 型
//返回值不再需要类型转换
MyPojo mypojo = myPojoDao.get(1L);



Project Home: [url]http://light-commons.googlecode.com[/url]

maven dependency:

<repositories>
<repository>
<id>light-commons</id>
<url>http://light-commons.googlecode.com/svn/repository</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.lightcommons</groupId>
<artifactId>lightcommons</artifactId>
<version>0.1.0-beta</version>
</dependency>
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值