最近公司有任务,用的是ssh框架,我看了下公司的dao的封装确实牛逼,所有我也参考他的dao进行了封装。废话少说先上代码。
package cn.com.wanhao.dao.impl.base;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;
import cn.com.wanhao.util.Page;
public interface BaseDao
{
/**
* function:增加一个entity对象,返回是否添加成功
* @createDate 2016-11-25 下午02:44:34
* @author ending
* @param T 对象类型
* @param entity 实体类
* @return boolean true/false
* @throws Exception
* */
public boolean add(T entity) throws Exception;
/**
* function:增加一个entity对象,返回String主键
* @createDate 2016-11-25 下午02:45:15
* @author ending
* @param T 对象类型
* @param entity 实体类
* @return String
* @throws Exception
* */
public String addStringKey(T entity) throws Exception;
/**
* function:增加一个entity对象,返回Integer主键
* @createDate 2016-11-25 下午02:45:15
* @author ending
* @param T 对象类型
* @param entity 实体类
* @return Integer
* @throws Exception
* */
public Integer addNumKey(T entity) throws Exception;
/**
* function:传入一个hql语句,返回影响多少条数据
* @createDate 2016-11-25 下午02:45:15
* @author ending
* @param hql
* @return int
* @throws Exception
* */
public int executeData(String hql) throws Exception;
/**
* function:传入一个hql语句,返回list集合
* @createDate 2016-11-25 下午02:53:14
* @author ending
* @param hql
* @return List
* @throws Exception
* */
public List findByHql(String hql) throws Exception;
/**
* function:执行sql语句,实行增,删,改
* @createDate 2016-11-25 下午02:56:03
* @author ending
* @param sql
* @return Integer
* @throws Exception
* */
public int executeBySql(String sql) throws Exception;
/**
* function:执行sql查询语句,获取list集合
* @createDate 2016-11-25 下午02:56:03
* @author ending
* @param sql
* @return List
* @throws Exception
* */
public List findBySql(String sql) throws Exception;
/**
* function:修改实体的数据,返回boolean结果
* @createDate 2016-11-25 下午03:01:08
* @author ending
* @param entity
* @return boolean
* @throws Exception
* */
public boolean editEntity(T entity) throws Exception;
/**
* function:执行hql语句,返回boolean结果
* @createDate 2016-11-25 下午03:01:08
* @author ending
* @param hql
* @return boolean
* @throws Exception
* */
public boolean editHql(String hql) throws Exception;
/**
* function:执行hql语句,返回执行结果影响的行数
* @createDate 2016-11-25 下午03:04:10
* @author ending
* @param hql
* @return Integer
* @throws Exception
* */
public int editNumHql(String hql) throws Exception;
/**
* function:传入要删除的实体,返回boolean结果
* @createDate 2016-11-25 下午03:08:33
* @author ending
* @param entity
* @return boolean
* @throws Exception
* */
public boolean removeEntity(T entity) throws Exception;
/**
* function:传入要实体类的class和int主键,返回具体实体
* @createDate 2016-11-25 下午03:12:07
* @author ending
* @param clazz
* @param id
* @return T
* @throws Exception
* */
public T getById(Class clazz,int id) throws Exception;
/**
* function:传入要实体类的class和String主键,返回具体实体
* @createDate 2016-11-25 下午03:12:07
* @author ending
* @param clazz
* @param id
* @return T
* @throws Exception
* */
public T getById(Class clazz,String id) throws Exception;
/**
* function:传入要实体类的class和Serializable主键,返回具体实体
* @createDate 2016-11-25 下午03:12:07
* @author ending
* @param clazz
* @param id
* @return T
* @throws Exception
* */
public T getById(Class clazz,Serializable id) throws Exception;
/**
* function:传入hql语句,返回实体
* @createDate 2016-11-25 下午03:16:21
* @author ending
* @param hql
* @return T
* @throws Exception
* */
public T getByHql(String hql) throws Exception;
/**
* function:传入hql语句,返回实体集合
* @createDate 2016-11-25 下午03:17:26
* @author ending
* @param hql
* @return List
* @throws Exception
* */
public List getList(String hql) throws Exception;
/**
* function:传入hql语句删除数据,返回执行结果
* @createDate 2016-11-25 下午03:18:44
* @author ending
* @param hql
* @return boolean
* @throws Exception
* */
public boolean remove(String hql) throws Exception;
/**
* function:动态查询
* @createDate 2016-11-25 下午03:18:44
* @author ending
* @param clazz
* @return List
* @throws Exception
* */
public List getList(Class clazz) throws Exception;
/**
* function:传入hql查询语句和object数组的参数,list返回结果
* @createDate 2016-11-25 下午03:18:44
* @author ending
* @param hql
* @param obj
* @return List
* @throws Exception
* */
public List getList(String hql,Object[] obj) throws Exception;
/**
* function:传入查询语句和查询总条数的hql,当前页数,一页显示多少数据,用list集合返回
* @createDate 2016-11-25 下午03:26:39
* @author ending
* @param queryHql
* @param queryCountHql
* @param firstResult
* @param maxResult
* @return List
* @throws Exception
* */
public List> showPage(String queryHql,String queryCountHql,int firstResult,int maxResult) throws Exception;
/**
* function:传入查询语句和查询总条数的hql,page分页对象,用list集合返回
* @createDate 2016-11-25 下午03:26:39
* @author ending
* @param queryHql
* @param queryCountHql
* @param page
* @return List
* @throws Exception
* */
public List showPage(String queryHql,String queryCountHql,Page page) throws Exception;
/**
* function:传入查询语句和查询总条数的hql,DetachedCriteria动态查询条件进行分页,用list集合返回
* @createDate 2016-11-25 下午03:26:39
* @author ending
* @param queryHql
* @param criteria
* @param firstResult
* @param maxResult
* @return List
* @throws Exception
* */
@SuppressWarnings("rawtypes")
public List showPage(String queryHql,DetachedCriteria criteria,int firstResult,int maxResult) throws Exception;
/**
* function:传入查询语句和查询总条数的hql,DetachedCriteria动态查询条件进行分页,分页对象page,用list集合返回
* @createDate 2016-11-25 下午03:26:39
* @author ending
* @param queryHql
* @param criteria
* @param page
* @return List
* @throws Exception
* */
public List showPage(String queryHql,DetachedCriteria criteria,Page page) throws Exception;
/**
* function:传入DetachedCriteria动态查询条件,用list集合返回
* @createDate 2016-11-25 下午03:38:01
* @author ending
* @param criteria
* @return List
* @throws Exception
* */
public List find(DetachedCriteria criteria) throws Exception;
/**
* function:提供session使用
* @createDate 2016-11-25 下午03:39:22
* @author ending
* @return Session
* */
public Session session();
/**
* function:提供HibernateTemplate使用
* @createDate 2016-11-25 下午03:39:22
* @author ending
* @return HibernateTemplate
* */
public HibernateTemplate getTemplate();
}
package cn.com.wanhao.dao.impl.base.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.com.wanhao.dao.impl.base.BaseDao;
import cn.com.wanhao.util.Page;
@SuppressWarnings("unchecked")
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao
{
@Override
public boolean add(T entity) throws Exception
{
boolean bool = false;
try
{
Serializable ser = this.getHibernateTemplate().save(entity);
if (ser != null)
{
bool = true;
}
}
catch (Exception e)
{
bool = false;
throw new RuntimeException(e);
}
return bool;
}
@Override
public String addStringKey(T entity) throws Exception
{
String id = null;
try
{
id = (String) this.getHibernateTemplate().save(entity);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return id;
}
@Override
public Integer addNumKey(T entity) throws Exception
{
Integer id = null;
try
{
id = (Integer) this.getHibernateTemplate().save(entity);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return id;
}
@Override
public int executeData(String hql) throws Exception
{
try
{
return this.getHibernateTemplate().bulkUpdate(hql);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
@Override
public List findByHql(String hql) throws Exception
{
List list = null;
try
{
list = this.getHibernateTemplate().find(hql);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return list;
}
@Override
public int executeBySql(String sql) throws Exception
{
try
{
return this.getSession().createSQLQuery(sql).executeUpdate();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
@Override
public List findBySql(String sql) throws Exception
{
try
{
return this.getSession().createSQLQuery(sql).list();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
@Override
public boolean editEntity(T entity) throws Exception
{
boolean bool = false;
try
{
this.getHibernateTemplate().update(entity);
bool = true;
}
catch (Exception e)
{
bool = false;
throw new RuntimeException(e);
}
return bool;
}
@Override
public boolean editHql(String hql) throws Exception
{
boolean bool = false;
try
{
int num = this.getHibernateTemplate().bulkUpdate(hql);
bool = num > 0 ? true : false;
}
catch (Exception e)
{
bool = false;
throw new RuntimeException(e);
}
return bool;
}
@Override
public int editNumHql(String hql) throws Exception
{
int count = 0;
try
{
count = this.getHibernateTemplate().bulkUpdate(hql);
}
catch (Exception e)
{
count = 0;
throw new RuntimeException(e);
}
return count;
}
@Override
public boolean removeEntity(T entity) throws Exception
{
boolean bool = false;
try
{
this.getHibernateTemplate().delete(entity);
bool = true;
}
catch (Exception e)
{
bool = false;
throw new RuntimeException(e);
}
return bool;
}
@Override
public T getById(Class clazz, int id) throws Exception
{
T t = null;
try
{
t = (T) this.getHibernateTemplate().get(clazz, id);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return t;
}
@Override
public T getById(Class clazz, String id) throws Exception
{
T t = null;
try
{
t = (T) this.getHibernateTemplate().get(clazz, id);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return t;
}
@Override
public T getById(Class clazz, Serializable id) throws Exception
{
T t = null;
try
{
t = (T) this.getHibernateTemplate().get(clazz, id);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return t;
}
@Override
public T getByHql(String hql) throws Exception
{
T t = null;
try
{
t = (T) this.getSession().createQuery(hql).setMaxResults(1).uniqueResult();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return t;
}
@Override
public List getList(String hql) throws Exception
{
List list = null;
try
{
list = this.getHibernateTemplate().find(hql);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return list;
}
@Override
public boolean remove(String hql) throws Exception
{
boolean bool = false;
try
{
bool = this.executeBySql(hql) > 0 ? true : false;
}
catch (Exception e)
{
bool = false;
throw new RuntimeException(e);
}
return bool;
}
@Override
public List getList(Class clazz) throws Exception
{
List list = null;
try
{
list = this.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(clazz));
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return list;
}
@Override
public List getList(String hql, Object[] obj) throws Exception
{
List list = null;
try
{
list = this.getHibernateTemplate().find(hql, obj);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return list;
}
@Override
public List> showPage(String queryHql, String queryCountHql, int firstResult, int maxResult) throws Exception
{
List
以上基本就是公用的dao了,可以给任务dao层调用,这次写了下次就不用写了。下面就是你本身项目dao到用上面的了,想用就用。
package cn.com.wanhao.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cn.com.wanhao.dao.LoginToDao;
import cn.com.wanhao.dao.impl.base.BaseDao;
import cn.com.wanhao.entity.Perssion;
import cn.com.wanhao.entity.RoleTable;
@Component
public class LoginToDaoImpl implements LoginToDao{
@Autowired
private BaseDao baseDao;
public BaseDao getBaseDao()
{
return baseDao;
}
public void setBaseDao(BaseDao baseDao)
{
this.baseDao = baseDao;
}
@Override
public Perssion find(int id) throws Exception {
return baseDao.getById(Perssion.class, id);
}
@Override
public void save(Perssion perssion) throws Exception
{
baseDao.add(perssion);
}
@Override
public Integer addNumKey(RoleTable entity) throws Exception
{
return baseDao.addNumKey(entity);
}
}
package cn.com.wanhao.util;
import java.util.ArrayList;
import java.util.List;
public class Page
{
//当前页码
private int cunrrentPage;
//全部页码
private int totalPage;
//全部数据
private int totalCount;
//每页多少数据
private int pageSize;
//查询返回结果
private List list=new ArrayList();
//分页链接
private String url;
public String getUrl()
{
return url;
}
public void setUrl(String url)
{
this.url = url;
}
public int getCunrrentPage()
{
return cunrrentPage;
}
public void setCunrrentPage(int cunrrentPage)
{
if(cunrrentPage<0){
cunrrentPage=0;
}
this.cunrrentPage = cunrrentPage;
}
public int getTotalPage()
{
if(totalCount%pageSize==0){
totalPage=totalCount/pageSize;
}else{
totalPage=totalCount/pageSize+1;
}
return totalPage;
}
public void setTotalPage(int totalPage)
{
if(totalPage<0){
totalPage=0;
}
this.totalPage = totalPage;
}
public int getTotalCount()
{
return totalCount;
}
public void setTotalCount(int totalCount)
{
if(totalCount<0){
totalCount=0;
}
this.totalCount = totalCount;
}
public int getPageSize()
{
return pageSize;
}
public void setPageSize(int pageSize)
{
if(pageSize<20){
pageSize=20;
}
this.pageSize = pageSize;
}
public List getList()
{
return list;
}
public void setList(List list)
{
this.list = list;
}
}
记住还要在spring配置文件中
这个一定要注入公用dao中