利用泛型减少Dao方法的数量
- package com.oa.dao;
- import java.io.Serializable;
- import java.util.*;
- /**
- * *
- *
- * @param <T>
- * 泛 型,指实体类 type
- * @param <PK>
- * 泛 型,指实体类主键的数据类型,如Integer,Long
- */
- public interface GenericDao<T, PK> {
- /**
- * 保 存指定实体类
- *
- * @param entityobj
- * 实 体类
- */
- public void save(T entity);
- /**
- * 删 除指定实体
- *
- * @param entityobj
- * 实 体类
- */
- public void delete(T entity);
- /** *
- * 删 除实体
- * @param entityClass 实体类名
- * @param id 实 体的ID
- */
- public void deleteById(Class<T> entityClass,PK id);
- /**
- * 更 新或保存指定实体
- *
- * @param entity 实 体类
- */
- public void saveorupdate(T entity);
- /** *
- * 更 新实体
- * 可用于添加、修改、删除操作
- * @param hql 更 新的HQL语句
- * @param params 参数,可有项目或 多项目,代替Hql中的"?"号
- */
- public void update(final String hql,final Object[] params);
- /**
- * 模 糊查询指定条件对象集合 <br>
- * 用法:可以 实例化一个空的T对象,需要查询某个字段,就set该字段的条件然后调用本方法<br>
- * 缺 点:目前测试貌似只能支持String的模糊查询,虽然有办法重写,但没必要,其他用HQL<br>
- *
- * @param entity
- * 条 件实体
- * @return 结合
- */
- public List<T> findByExample(T entity);
- /**
- * 获 取所有实体集合
- *
- * @param entityClass
- * 实 体
- * @return 集合
- */
- public List<T> findAll(Class<T> entityClass);
- public List<T> findAll(Class<T> entityClass,String hql,Object[] params,int start, int limit);
- /**
- * 查 找指定PK实体类对象
- *
- * @param entityClass
- * 实 体Class
- * @param id
- * 实 体PK
- * @return 实体对象
- */
- public T findById(Class<T> entityClass, PK id);
- /** *
- * 按 HQL条件查询列表
- * @param hql 查询语句,支持连接 查询和多条件查询
- * @param params 参数数组,代替 hql中的"?"号
- * @return 结果集List
- */
- public List<T> findByHql(String hql,Object[] params);
- /**
- * 查 找指定属性的实体集合
- *
- * @param entityClass
- * 实 体
- * @param propertyName
- * 属 性名
- * @param value
- * 条 件
- * @return 实体集合
- */
- public List<T> findByProperty(Class<T> entityClass, String propertyName,Object value);
- /**
- * 查 询指定HQL语句的分页数据集合
- *
- * @param hsql
- * HQL 语句
- * @param start
- * 开 始记录号
- * @param limit
- * 最 大记录号
- * @return 分页数据集合
- * @throws Exception
- * 抛 出异常
- */
- public List<T> findByPage(Class<T> entityClass,int start,int limit) ;
- /**
- * 获 得总记录数
- */
- public T getTotalCount(Class<T> entityClass);
- public T getPageCount(String hql,Object[] params);
- }
- package com.oa.dao.impl;
- import java.io.Serializable;
- import java.util.List;
- import org.hibernate.Query;
- import org.springframework.stereotype.Repository;
- import com.oa.dao.GenericDao;
- import com.oa.dao.MyHibernateDaoSupport;
- @SuppressWarnings("unchecked")
- @Repository("genericDao") // 声明此类为数据持久层的类
- public class GenericDaoImpl<T, PK extends Serializable> extends
- MyHibernateDaoSupport implements GenericDao<T, PK> {
- public void delete(T entity) {
- super.getHibernateTemplate().delete(entity);
- }
- public void deleteById(Class entityClass, PK id) {
- super.getHibernateTemplate().delete(findById(entityClass, id));
- }
- public void save(T entity) {
- super.getHibernateTemplate().save(entity);
- }
- public void saveorupdate(T entity) {
- super.getHibernateTemplate().saveOrUpdate(entity);
- }
- public void update(String hql, Object[] params) {
- Query query = super.getSession().createQuery(hql);
- for(int i=0; i<params.length; i++){
- query.setParameter(i, params[i]);
- }
- query.executeUpdate();
- }
- public List<T> findAll(Class entityClass) {
- return super.getHibernateTemplate().loadAll(entityClass);
- }
- public List<T> findAll(Class entityClass, String hql, Object[] params,int start, int limit) {
- Query query = super.getSession().createQuery(hql);
- if(params!=null&¶ms.length>0){
- for(int i = 0;i<params.length;i++){
- query.setParameter(i, params[i]);
- }
- }
- if(start!=0&&limit!=0){
- query.setFirstResult(start).setMaxResults(limit);
- }
- return query.list();
- }
- public List<T> findByExample(T entity) {
- return super.getHibernateTemplate().findByExample(entity);
- }
- public List<T> findByHql(String hql, Object[] params) {
- Query query = super.getSession().createQuery(hql);
- if(null!= params && params.length>0){
- for(int i = 0; i<params.length;i++){
- query.setParameter(i, params[i]);
- }
- }
- return query.list();
- }
- public T findById(Class entityClass, PK id) {
- return (T)super.getHibernateTemplate().get(entityClass, id);
- }
- public List<T> findByProperty(Class entityClass, String propertyName,Object value) {
- String queryString = "from "+entityClass.getName()+ " as model where model." + propertyName + "=?";
- return super.getHibernateTemplate().find(queryString, value);
- }
- // 分页使用
- public List<T> findByPage(Class<T> entityClass,int start,int limit) {
- Query query=super.getSession().createQuery("select o from "+entityClass.getName()+" o");
- query.setFirstResult(start).setMaxResults(limit);
- return query.list();
- }
- public T getTotalCount(Class entityClass) {
- return (T)super.getSession().createQuery("select count(o) from "+entityClass.getName()+" o").uniqueResult();
- }
- public T getPageCount(String hql, Object[] params) {
- Query query = super.getSession().createQuery(hql);
- if(null!= params && params.length>0){
- for(int i = 0; i<params.length;i++){
- query.setParameter(i, params[i]);
- }
- }
- return (T)query.list();
- }
- }
至此 泛型就告一个段落。
接下来日子就好过了。
我们不是有user news 等等一系列的curd管理。
以User为例子;
定义一个user的接口,
UserDao.Java
Java代码 <embed height="50" width="400" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="elmtId=28507_1245983629038¶mStr=++src%3D%22http%3A%2F%2Fwww.javaeye.com%2Fjavascripts%2Fsyntaxhighlighter%2Fclipboard_new.swf%22++flashvars%3D%22clipboard%3Dpackage%2520com.oa.dao%253B%250A%250Aimport%2520com.oa.User%253B%250A%250Apublic%2520interface%2520UserDao%2520extends%2520GenericDao%253CUser%252C%2520Integer%253E%2520%257B%250A%250Apublic%2520%2520%2520int%2520%2520%2520login%25EF%25BC%2588User%2520user%25EF%25BC%2589%253B%250A%252F%252F%25E5%2585%25B6%25E4%25BB%2596%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%25E7%259A%2584%250A%257D%250A%250A%250A%25E7%2584%25B6%25E5%2590%258E%25E5%25B0%25B1%25E6%2598%25AF%25E5%25AE%259E%25E7%258E%25B0%25E5%25AE%2583%2520UserDaoImpl%250A%250Apackage%2520com.oa.dao.impl%253B%250A%250A%250A%250Aimport%2520com.oa.User%253B%250Aimport%2520com.oa.dao.UserDao%253B%250A%250Apublic%2520class%2520UserDaoImpl%2520extends%2520GenericDaoImpl%253CUser%252C%2520Integer%253E%2520implements%2520UserDao%2520%257B%250A%250A%2509public%2520%2520int%2520%2520login%25EF%25BC%2588User%2520%2520user%25EF%25BC%2589%257B%250A%252F%252F%25E7%2599%25BB%25E9%2599%2586%25E5%2588%25A4%25E6%2596%25AD%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%250A%250Areturn%2520%2520%2520XX%253B%250A%257D%253B%250A%250A%250A%252F%252F%25E5%2585%25B6%25E4%25BB%2596%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%25E7%259A%2584%25E5%25AE%259E%25E7%258E%25B0%250A%250A%250A%257D%250A%250A%2520%22+quality%3D%22high%22+type%3D%22application%2Fx-shockwave-flash%22+width%3D%2214%22+height%3D%2215%22" type="application/x-shockwave-flash" allowscriptaccess="sameDomain" quality="high" src="http://ecjtu05.blog.sohu.com/flash/embedloader.swf" id="28507_1245983629038">
- package com.oa.dao;
- import com.oa.User;
- public interface UserDao extends GenericDao<User, Integer> {
- public int login(User user);
- // 其他的方法的
- }
- 然 后就是实现它 UserDaoImpl
- package com.oa.dao.impl;
- import com.oa.User;
- import com.oa.dao.UserDao;
- public class UserDaoImpl extends GenericDaoImpl<User, Integer> implements UserDao {
- public int login(User user) {
- //登陆判断的方法
- return XX;
- };
- // 其他的方法的实现
- }