您的位置:首页 > 其它

对HibernateDaoSupport进行二次封装:hibernate增删改查组件

2014-12-03 13:56 441 查看
本组件继承了HibernateDaoSupport,并完成对HibernateDaoSupport进行二次封装。提取平时开发常用的底层操作方法,并根据个

人习惯,定义自己的编码规范。根据sun官方的最新开发规范,使用了jdk的新特性——泛型。所有的操作对象以泛型指定。

定义接口规范:IBaseDao.java

[java] view
plaincopyprint?

package com.hoo.dao;

import java.io.Serializable;

import java.util.List;

import org.hibernate.Session;

import org.hibernate.criterion.DetachedCriteria;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.hoo.entity.Page;

/***

* <b>function:</b> 增删改查组件规范接口

* @project NetWorkService

* @package com.hoo.dao

* @fileName IBaseDao.java

* @createDate 2010-8-2 下午05:28:03

* @author hoojo

* @email hoojo_@126.com

* @blog http://blog.csdn.net/IBM_hoojo
*/

public interface IBaseDao {

/**

* <b>function:</b> 增加一个entity对象,返回是否添加成功

* @createDate 2010-8-2 下午05:28:38

* @author hoojo

* @param <T> 对象类型

* @param entity 对象

* @return boolean true/false

* @throws Exception

*/

public <T> boolean add(T entity) throws Exception;

/**

* <b>function:</b> 添加一个entity对象,返回添加对象的Integer类型的主键

* @createDate 2010-8-2 下午05:29:39

* @author hoojo

* @param <T> 对象类型

* @param entity 将要添加的对象

* @return Integer 返回主键

* @throws Exception

*/

public <T> Integer addAndGetId4Integer(T entity) throws Exception;

/**

* <b>function:</b> 添加一个对象并且返回该对象的String类型的主键

* @createDate 2010-8-2 下午05:31:32

* @author hoojo

* @param <T> 对象类型

* @param entity 将要添加的对象

* @return String 返回的主键

* @throws Exception

*/

public <T> String addAndGetId4String(T entity) throws Exception;

/**

* <b>function:</b> 传入hql语句执行

* @createDate 2010-8-2 下午04:42:26

* @author hoojo

* @param hql String hql语句

* @return int 影响行数

* @throws Exception

*/

public int executeByHql(String hql) throws Exception;

/**

* <b>function:</b> 传入hql语句执行查询,返回list集合

* @createDate 2010-8-3 上午10:00:34

* @author hoojo

* @param hql 查询的hql语句

* @return List集合

* @throws Exception

*/

public <T> List<T> findByHql(String hql) throws Exception;

/**

* <b>function:</b> 执行原生态的sql语句,添加、删除、修改语句

* @createDate 2010-8-2 下午05:33:42

* @author hoojo

* @param sql 将要执行的sql语句

* @return int

* @throws Exception

*/

public int executeBySql(String sql) throws Exception;

/**

* <b>function:</b> 传入sql语句执行查询,返回list集合

* @createDate 2010-8-3 上午10:00:34

* @author hoojo

* @param sql 查询的sql语句

* @return List集合

* @throws Exception

*/

public <T> List<T> findBySql(String sql) throws Exception;

/**

* <b>function:</b> 修改entity对象,返回是否修改成功

* @createDate 2010-8-2 下午05:35:47

* @author hoojo

* @param <T> 对象类型

* @param entity 将要修改的对象

* @return boolean true/false 是否修改成功

* @throws Exception

*/

public <T> boolean edit(T entity) throws Exception;

/**

* <b>function:</b> 传入hql语句执行修改,返回是否修改成功

* @createDate 2010-8-2 下午05:36:31

* @author hoojo

* @param hql 查询的hql语句

* @return boolean true/false 返回是否修改成功

* @throws Exception

*/

public boolean edit(String hql) throws Exception;

/**

* <b>function:</b> 执行修改的hql语句,返回修改的行数

* @createDate 2010-8-2 下午05:38:58

* @author hoojo

* @param hql 修改语句

* @return int 返回修改的行数

* @throws Exception

*/

public int editByHql(String hql) throws Exception;

/**

* <b>function:</b> 传入一个将要删除的entity对象,返回删除是否成功

* @createDate 2010-8-2 下午05:42:02

* @author hoojo

* @param <T> 传入对象类型

* @param entity 将要传入的对象

* @return boolean true/false

* @throws Exception

*/

public <T> boolean remove(T entity) throws Exception;

/**

* <b>function:</b> 传入一个entity对象Class和String型主键,返回该对象

* @createDate 2010-8-2 下午05:44:53

* @author hoojo

* @param <T> 返回、传入对象类型

* @param c 对象Class

* @param id 主键

* @return T 返回传入类型对象

* @throws Exception

*/

public <T> T getById(Class<T> c, String id) throws Exception;

/**

* <b>function:</b> 传入一个entity对象Class和Integer类型主键,返回该对象

* @createDate 2010-8-2 下午05:47:20

* @author hoojo

* @param <T> 返回、传入对象类型

* @param c 对象Class

* @param id 主键

* @return T 返回该类型的对象

* @throws Exception

*/

public <T> T getById(Class<T> c, Integer id) throws Exception;

/**

* <b>function:</b> 传入一个entity对象Class和Serializable类型主键,返回该对象

* @createDate 2010-8-2 下午05:48:36

* @author hoojo

* @param <T> 返回、传入对象类型

* @param c 对象Class

* @param id 主键

* @return T 返回该类型的对象

* @throws Exception

*/

public <T> T get(Class<T> c, Serializable id) throws Exception;

/**

* <b>function:</b> 传入hql语句,查询对象

* @createDate 2010-8-2 下午05:49:31

* @author hoojo

* @param <T> 返回对象类型

* @param hql 查询的hql语句

* @return 对象T

* @throws Exception

*/

public <T> T get(String hql) throws Exception;

/**

* <b>function:</b> 通过hql语句查询List集合

* @createDate 2010-8-2 下午05:51:05

* @author hoojo

* @param hql 查询hql语句

* @return List<?>

* @throws Exception

*/

public <T> List<T> getList(String hql) throws Exception;

/**

* <b>function:</b> 传入删除的hql语句,删除记录

* @createDate 2010-8-3 上午09:53:49

* @author hoojo

* @param hql 将要被执行删除的hql语句

* @return 是否删除成功

* @throws Exception

*/

public boolean remove(String hql) throws Exception;

/**

* <b>function:</b> 动态查询

* @createDate 2010-8-3 上午10:53:37

* @author hoojo

* @param <T> 查询类的类型

* @param c 动态查询组合对象

* @return list集合

* @throws Exception

*/

public <T> List<T> getList(Class<T> c) throws Exception;

/**

* <b>function:</b> 传入hql查询语句和object数组类型的参数,返回查询list集合

* @createDate 2010-8-2 下午05:52:36

* @author hoojo

* @param hql 查询的hql语句

* @param obj 查询参数

* @return 返回list集合

* @throws Exception

*/

public <T> List<T> getList(String hql, Object[] obj) throws Exception;

/**

* <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、当前页数、每页显示调试数;返回查询后的list集合;

* list集合保存总记录调试和记录结果

* @createDate 2010-8-2 下午05:54:01

* @author hoojo

* @param queryHql 查询记录hql语句

* @param queryCountHql 查询记录条数hql语句

* @param firstResult 当前查询页

* @param maxResult 每页显示多少条

* @return List返回集合 集合0保存查询结果、集合1保存总记录条数

* @throws Exception

*/

public List<?> showPage(String queryHql, String queryCountHql, int firstResult, int maxResult) throws Exception;

/**

* <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、page分页对象;返回查询后的list集合;

* @createDate 2010-8-3 上午11:16:59

* @author hoojo

* @param queryHql list集合结果查询

* @param queryCountHql 总记录调试查询

* @param page 分页对象

* @throws Exception

*/

public <T> void showPage(String queryHql, String queryCountHql, Page<T> page) throws Exception;

/**

* <b>function:</b> 分页查询,传入查询count的hql语句和DetachedCriteria动态查询条件进行查询分页

* @createDate 2010-8-3 上午11:04:39

* @author hoojo

* @param queryCountHql hql查询count语句总条数

* @param cResult DetachedCriteria 动态查询条件

* @param firstResult 起始

* @param maxResult 最大页数

* @return List<?> 查询集合

* @throws Exception

*/

public List<?> showPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception;

/**

* <b>function:</b> 分页查询,传入查询的count的hql语句和动态查询DetachedCriteria类及page分页entity

* @createDate 2010-8-3 上午11:14:30

* @author hoojo

* @param queryCountHql 查询count语句

* @param cResult DetachedCriteria 动态查询组合类

* @param page Page分页实体类

* @throws Exception

*/

public <T> void showPage(String queryCountHql, DetachedCriteria cResult, Page<T> page) throws Exception;

/**

* <b>function:</b> 传入查询条件DetachedCriteria进行查询

* @createDate 2010-8-3 上午11:55:28

* @author hoojo

* @param <T> 类型

* @param dc DetachedCriteria动态条件查询

* @return List

* @throws Exception

*/

public <T> List<T> find(DetachedCriteria dc) throws Exception;

/**

* <b>function:</b> 暴露基类session供用户使用

* @createDate 2010-8-3 上午11:59:54

* @author hoojo

* @return Session

*/

public Session session();

/**

* <b>function:</b> 暴露HibernateTemplate模板,当基类(增删改查组件)方法不够用可以用模板进行操作

* @createDate 2010-8-3 上午11:58:51

* @author hoojo

* @return HibernateTemplate

*/

public HibernateTemplate getTemplate();

}

下面是实现IBaseDao接口的代码BaseDaoImpl.java:

[java] view
plaincopyprint?

package com.hoo.dao.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 com.hoo.dao.IBaseDao;

import com.hoo.entity.Page;

/**

* <b>function:</b> 删除改查组件实现类

* @project NetWorkService

* @package com.hhh.dao

* @fileName BaseDAOImpl.java

* @createDate 2010-8-2 下午05:58:45

* @author hoojo

* @email hoojo_@126.com

* @blog http://blog.csdn.net/IBM_hoojo
*/

@SuppressWarnings("unchecked")

public class BaseDaoImpl extends HibernateDaoSupport implements IBaseDao {

/**

* <b>function:</b> 增加一个entity对象,返回是否添加成功

* @createDate 2010-8-2 下午05:28:38

* @author hoojo

* @param <T> 对象类型

* @param entity 对象

* @return boolean true/false

* @throws Exception

*/

public <T> boolean add(T entity) throws Exception {

boolean bo = false;

try {

Serializable io = this.getHibernateTemplate().save(entity);

if (io != null) {

bo = true;

}

} catch (Exception e) {

bo = false;

throw new RuntimeException(e);

}

return bo;

}

/**

* <b>function:</b> 添加一个entity对象,返回添加对象的Integer类型的主键

* @createDate 2010-8-2 下午05:29:39

* @author hoojo

* @param <T> 对象类型

* @param entity 将要添加的对象

* @return Integer 返回主键

* @throws Exception

*/

public <T> Integer addAndGetId4Integer(T entity) throws Exception {

Integer id = null;

try {

id = (Integer) this.getHibernateTemplate().save(entity);

} catch (Exception e) {

throw new RuntimeException(e);

}

return id;

}

/**

* <b>function:</b> 添加一个对象并且返回该对象的String类型的主键

* @createDate 2010-8-2 下午05:31:32

* @author hoojo

* @param <T> 对象类型

* @param entity 将要添加的对象

* @return String 返回的主键

* @throws Exception

*/

public <T> String addAndGetId4String(T entity) throws Exception {

String id = null;

try {

id = (String) this.getHibernateTemplate().save(entity);

} catch (Exception e) {

throw new RuntimeException(e);

}

return id;

}

/**

* <b>function:</b> 修改entity对象,返回是否修改成功

* @createDate 2010-8-2 下午05:35:47

* @author hoojo

* @param <T> 对象类型

* @param entity 将要修改的对象

* @return boolean true/false 是否修改成功

* @throws Exception

*/

public <T> boolean edit(T entity) throws Exception {

boolean bo = false;

try {

this.getHibernateTemplate().update(entity);

bo = true;

} catch (Exception e) {

bo = false;

throw new RuntimeException(e);

}

return bo;

}

/**

* <b>function:</b> 传入hql语句执行修改,返回是否修改成功

* @createDate 2010-8-2 下午05:36:31

* @author hoojo

* @param hql 查询的hql语句

* @return boolean true/false 返回是否修改成功

* @throws Exception

*/

public boolean edit(String hql) throws Exception {

boolean bo = false;

try {

int count = this.getHibernateTemplate().bulkUpdate(hql);

bo = count > 0 ? true : false;

} catch (Exception e) {

bo = false;

throw new RuntimeException(e);

}

return bo;

}

/**

* <b>function:</b> 执行修改的hql语句,返回修改的行数

* @createDate 2010-8-2 下午05:38:58

* @author hoojo

* @param hql 修改语句

* @return int 返回修改的行数

* @throws Exception

*/

public int editByHql(String hql) throws Exception {

int count = 0;

try {

count = this.getHibernateTemplate().bulkUpdate(hql);

} catch (Exception e) {

throw new RuntimeException(e);

}

return count;

}

/**

* <b>function:</b> 传入hql语句执行

* @createDate 2010-8-2 下午04:42:26

* @author hoojo

* @param hql String hql语句

* @return int 影响行数

* @throws Exception

*/

public int executeByHql(String hql) throws Exception {

try {

return this.getHibernateTemplate().bulkUpdate(hql);

} catch (Exception e) {

throw new RuntimeException(e);

}

}

/**

* <b>function:</b> 传入hql语句执行查询,返回list集合

* @createDate 2010-8-3 上午10:00:34

* @author hoojo

* @param hql 查询的hql语句

* @return List集合

* @throws Exception

*/

public <T> List<T> findByHql(String hql) throws Exception {

List list = null;

try {

list = (List<T>) this.getHibernateTemplate().find(hql);

} catch (Exception e) {

throw new RuntimeException(e);

}

return list;

}

/**

* <b>function:</b> 执行原生态的sql语句,添加、删除、修改语句

* @createDate 2010-8-2 下午05:33:42

* @author hoojo

* @param sql 将要执行的sql语句

* @return int

* @throws Exception

*/

public int executeBySql(String sql) throws Exception {

try {

return this.getSession().createSQLQuery(sql).executeUpdate();

} catch (Exception e) {

throw new RuntimeException(e);

}

}

/**

* <b>function:</b> 传入sql语句执行查询,返回list集合

* @createDate 2010-8-3 上午10:00:34

* @author hoojo

* @param sql 查询的sql语句

* @return List集合

* @throws Exception

*/

public <T> List<T> findBySql(String sql) throws Exception {

List list = null;

try {

list = (List<T>) this.getSession().createSQLQuery(sql).list();

} catch (Exception e) {

throw new RuntimeException(e);

}

return list;

}

/**

* <b>function:</b> 传入一个entity对象Class和Serializable类型主键,返回该对象

* @createDate 2010-8-2 下午05:48:36

* @author hoojo

* @param <T> 返回、传入对象类型

* @param c 对象Class

* @param id 主键

* @return T 返回该类型的对象

* @throws Exception

*/

public <T> T get(Class<T> c, Serializable id) throws Exception {

T ety = null;

try {

ety = (T) this.getHibernateTemplate().get(c, id);

} catch (Exception e) {

throw new RuntimeException(e);

}

return ety;

}

/**

* <b>function:</b> 传入hql语句,查询对象

* @createDate 2010-8-2 下午05:49:31

* @author hoojo

* @param <T> 返回对象类型

* @param hql 查询的hql语句

* @return 对象T

* @throws Exception

*/

public <T> T get(String hql) throws Exception {

T ety = null;

try {

ety = (T) this.getSession().createQuery(hql).setMaxResults(1).uniqueResult();

} catch (Exception e) {

throw new RuntimeException(e);

}

return ety;

}

/**

* <b>function:</b> 通过hql语句查询List集合

* @createDate 2010-8-2 下午05:51:05

* @author hoojo

* @param hql 查询hql语句

* @return List<?>

* @throws Exception

*/

public <T> List<T> getList(String hql) throws Exception {

List<T> list = null;

try {

list = (List<T>) this.getHibernateTemplate().find(hql);

} catch (Exception e) {

throw new RuntimeException(e);

}

return list;

}

/**

* <b>function:</b> 传入一个entity对象Class和Integer类型主键,返回该对象

* @createDate 2010-8-2 下午05:47:20

* @author hoojo

* @param <T> 返回、传入对象类型

* @param c 对象Class

* @param id 主键

* @return T 返回该类型的对象

* @throws Exception

*/

public <T> T getById(Class<T> c, Integer id) throws Exception {

T ety = null;

try {

ety = (T) this.getHibernateTemplate().get(c, id);

} catch (Exception e) {

throw new RuntimeException(e);

}

return ety;

}

/**

* <b>function:</b> 传入一个entity对象Class和String型主键,返回该对象

* @createDate 2010-8-2 下午05:44:53

* @author hoojo

* @param <T> 返回、传入对象类型

* @param c 对象Class

* @param id 主键

* @return T 返回传入类型对象

* @throws Exception

*/

public <T> T getById(Class<T> c, String id) throws Exception {

T ety = null;

try {

ety = (T) this.getHibernateTemplate().get(c, id);

} catch (Exception e) {

throw new RuntimeException(e);

}

return ety;

}

/**

* <b>function:</b> 传入hql查询语句和object数组类型的参数,返回查询list集合

* @createDate 2010-8-2 下午05:52:36

* @author hoojo

* @param hql 查询的hql语句

* @param obj 查询参数

* @return 返回list集合

* @throws Exception

*/

public <T> List<T> getList(String hql, Object[] obj) throws Exception {

List<T> list = null;

try {

list = (List<T>) this.getHibernateTemplate().find(hql, obj);

} catch (Exception e) {

throw new RuntimeException(e);

}

return list;

}

/**

* <b>function:</b> 传入一个将要删除的entity对象,返回删除是否成功

* @createDate 2010-8-2 下午05:42:02

* @author hoojo

* @param <T> 传入对象类型

* @param entity 将要传入的对象

* @return boolean true/false

* @throws Exception

*/

public <T> boolean remove(T entity) throws Exception {

boolean bo = false;

try {

this.getHibernateTemplate().delete(entity);

bo = true;

} catch (Exception e) {

bo = false;

throw new RuntimeException(e);

}

return bo;

}

/**

* <b>function:</b> 传入删除的hql语句,删除记录

* @createDate 2010-8-3 上午09:53:49

* @author hoojo

* @param hql 将要被执行删除的hql语句

* @return 是否删除成功

* @throws Exception

*/

public boolean remove(String hql) throws Exception {

try {

return this.executeByHql(hql) > 0 ? true : false;

} catch (Exception e) {

throw new RuntimeException(e);

}

}

/**

* <b>function:</b> 动态查询

* @createDate 2010-8-3 上午10:53:37

* @author hoojo

* @param <T> 查询类的类型

* @param c 动态查询组合对象

* @return list集合

* @throws Exception

*/

public <T> List<T> getList(Class<T> c) throws Exception {

List<T> list = null;

try {

this.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(c));

} catch (Exception e) {

throw new RuntimeException(e);

}

return list;

}

/**

* <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、当前页数、每页显示调试数;返回查询后的list集合;

* list集合保存总记录调试和记录结果

* @createDate 2010-8-2 下午05:54:01

* @author hoojo

* @param queryHql 查询记录hql语句

* @param queryCountHql 查询记录条数hql语句

* @param firstResult 当前查询页

* @param maxResult 每页显示多少条

* @return List返回集合 集合0保存查询结果、集合1保存总记录条数

* @throws Exception

*/

public List<?> showPage(String queryHql, String queryCountHql, int firstResult, int maxResult) throws Exception {

List<Object> list = new ArrayList<Object>();

try {

Session session = this.getSession();

list.add(session.createQuery(queryHql)

.setFirstResult(firstResult).setMaxResults(maxResult).list());

list.add(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult());

} catch (Exception e) {

throw new RuntimeException(e);

}

return list;

}

/**

* <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、page分页对象;返回查询后的list集合;

* @createDate 2010-8-3 上午11:16:59

* @author hoojo

* @param queryHql list集合结果查询

* @param queryCountHql 总记录调试查询

* @param page 分页对象

* @throws Exception

*/

public <T> void showPage(String queryHql, String queryCountHql, Page<T> page) throws Exception {

try {

Session session = this.getSession();

page.setResult(session.createQuery(queryHql)

.setFirstResult(page.getCurrentPage()).setMaxResults(page.getPageSize()).list());

page.setTotalsCount(Integer.parseInt(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult().toString()));

} catch (Exception e) {

throw new RuntimeException(e);

}

}

/**

* <b>function:</b> 分页查询,传入查询count的hql语句和DetachedCriteria动态查询条件进行查询分页

* @createDate 2010-8-3 上午11:04:39

* @author hoojo

* @param queryCountHql hql查询count语句总条数

* @param cResult DetachedCriteria 动态查询条件

* @param firstResult 起始

* @param maxResult 最大页数

* @return List<?> 查询集合

* @throws Exception

*/

public List<?> showPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception {

List<Object> list = new ArrayList<Object>();

try {

Session session = this.getSession();

list.add(this.getHibernateTemplate().findByCriteria(cResult, firstResult, maxResult));

list.add(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult());

} catch (Exception e) {

throw new RuntimeException(e);

}

return list;

}

/**

* <b>function:</b> 分页查询,传入查询的count的hql语句和动态查询DetachedCriteria类及page分页entity

* @createDate 2010-8-3 上午11:14:30

* @author hoojo

* @param queryCountHql 查询count语句

* @param cResult DetachedCriteria 动态查询组合类

* @param page Page分页实体类

* @throws Exception

*/

public <T> void showPage(String queryCountHql, DetachedCriteria cResult, Page<T> page) throws Exception {

try {

Session session = this.getSession();

page.setResult(this.getHibernateTemplate().findByCriteria(cResult, page.getCurrentPage(), page.getPageSize()));

page.setTotalsCount(Integer.parseInt(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult().toString()));

} catch (Exception e) {

throw new RuntimeException(e);

}

}

/**

* <b>function:</b> 传入查询条件DetachedCriteria进行查询

* @createDate 2010-8-3 上午11:55:28

* @author hoojo

* @param <T> 类型

* @param dc DetachedCriteria动态条件查询

* @return List

* @throws Exception

*/

public <T> List<T> find(DetachedCriteria dc) throws Exception {

List<T> list = new ArrayList<T>();

try {

list = (List<T>) this.getHibernateTemplate().findByCriteria(dc);

} catch (Exception e) {

throw new RuntimeException(e);

}

return list;

}

/**

* <b>function:</b> 暴露基类session供用户使用

* @createDate 2010-8-3 上午11:59:54

* @author hoojo

* @return Session

*/

public Session session() {

return this.getSession();

}

/**

* <b>function:</b> 暴露HibernateTemplate模板,当基类(增删改查组件)方法不够用可以用模板进行操作

* @createDate 2010-8-3 上午11:58:51

* @author hoojo

* @return HibernateTemplate

*/

public HibernateTemplate getTemplate() {

return this.getHibernateTemplate();

}

}

分页对象Page.java:

[java] view
plaincopyprint?

package com.hoo.entity;

import java.util.ArrayList;

import java.util.List;

/**

* <b>function:</b> 分页entity,封装分页数据

* @project NetWorkService

* @package com.hoo.entity

* @fileName Page.java

* @createDate 2010-8-3 上午10:32:03

* @author hoojo

* @email hoojo_@126.com

* @blog http://blog.csdn.net/IBM_hoojo
*/

public class Page<T> {

//当前页数

private int currentPage;

//总页数

private int totalsPage;

//每页显示记录条数

private int pageSize;

//总记录条数

private int totalsCount;

//查询返回结果

private List<T> result = new ArrayList<T>();

//分页链接

private String uri;

public String getUri() {

return uri;

}

public void setUri(String uri) {

this.uri = uri;

}

public int getCurrentPage() {

return currentPage;

}

public void setCurrentPage(int currentPage) throws Exception {

if (currentPage < 0) {

currentPage = 0;

}

this.currentPage = currentPage;

}

public int getTotalsPage() {

try {

if (totalsCount % pageSize == 0) {

totalsPage = totalsCount / pageSize;

} else {

totalsPage = (totalsCount / pageSize) + 1;

}

} catch (Exception e) {

throw new RuntimeException(e);

}

return totalsPage;

}

public void setTotalsPage(int totalsPage) {

if (totalsPage < 0) {

totalsPage = 0;

}

this.totalsPage = totalsPage;

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

if (pageSize <= 0) {

pageSize = 20;

}

this.pageSize = pageSize;

}

public int getTotalsCount() {

return totalsCount;

}

public void setTotalsCount(int totalsCount) {

if (totalsCount < 0) {

totalsCount = 0;

}

this.totalsCount = totalsCount;

}

public List<T> getResult() {

return result;

}

public void setResult(List<T> result) {

this.result = result;

}

}

增删改查简单示例:

[java] view
plaincopyprint?

package com.hoo.dao.impl;

import java.util.List;

import com.hoo.dao.IBaseDao;

import com.hoo.entity.UserInfo;

public class TestDaoImpl<T extends UserInfo> implements ITestDao<T> {

private IBaseDao dao;

//setter方法注入dao

public void setDao(IBaseDao dao) {

this.dao = dao;

}

//查询

public <T extends UserInfo> List<T> getSortListByParentId(T entity) throws Exception {

String hql = "....";

if (entity == null || entity.getUserName() == null || "".equals(entity.getId())) {

hql += " ...";

} else {

hql += " ...";

}

return dao.getList(hql);

}

//添加

public boolean addUser(T entity) {

return dao.add(entity);

}

//删除

public boolean removeUser(T entity) {

return dao.remove(entity);

}

//修改

public boolean editUser(T entity) {

return dao.edit(entity);

}

}

为TestDaoImpl注入BaseDao即可

[xhtml] view
plaincopyprint?

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~dao 注入~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

<!-- 数据库操作基类,所有的类要注入基类 -->

<bean id="baseDao" class="com.hhh.dao.impl.BaseDaoImpl">

<property name="sessionFactory" ref="sessionFactory"/>

</bean>

<!-- TestDao实现 -->

<bean id="testDao" class="com.hhh.dao.impl.TestDaoImpl">

<!-- 注入基类BaseDaoImpl -->

<property name="dao" ref="baseDao"/>

</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐