ibatis basedaoO接口继承了一个DAO接口,DAO是个空接口,请问继承它有什么用。

package com.hfxt.dao.
import java.io.S
import java.math.BigI
import java.util.HashM
import java.util.L
import java.util.M
import org.hibernate.Q
import org.hibernate.S
import org.slf4j.L
import org.slf4j.LoggerF
import org.springframework.dao.DataAccessE
import org.springframework.orm.hibernate3.HibernateC
import org.springframework.orm.hibernate3.support.HibernateDaoS
import org.springframework.transaction.annotation.T
import mon.P
import mon.V
import com.hfxt.dao.IBaseD
import com.hfxt.exception.HibernateDaoSupportE
public class BaseDao&T,PK extends Serializable& extends HibernateDaoSupport implements IBaseDao&T,PK&
protected Logger log = LoggerFactory.getLogger(this.getClass());
private boolean isCacheQueries =
public boolean isCacheQueries() {
return isCacheQ
public void setCacheQueries(boolean isCacheQueries) {
this.isCacheQueries = isCacheQ
* The &code&add(T entity)&/code& method is add object to database.
* @param entity
if you want to add entity.
* @throws HibernateDaoSupportException
Throws HibernateDaoSupportException when accessing and
manipulating database happen exception.
public PK add(T entity) throws HibernateDaoSupportException {
if (null == entity) {
throw new HibernateDaoSupportException(&Param(#&
+ this.getClass().getName() + &) with value null&);
PK pk = (PK)this.getHibernateTemplate().save(entity);
log.debug(&Executing save method is successful!&);
} catch (DataAccessException e) {
log.error(&Error saving entity:{}&, e);
throw new HibernateDaoSupportException(&Error saving (#&
+ this.getClass().getName() + &#):& + e);
* 更新或保存实体
* @param entity
public void saveOrUpdateEntity(T entity){
this.getHibernateTemplate().saveOrUpdate(entity);
* The &code&delete(T entity)&/code& method is delete object to database.
* @param entity
if you want to delete entity.
* @throws HibernateDaoSupportException
Throws HibernateDaoSupportException when accessing and
manipulating database happen exception.
public void delete(T entity) throws HibernateDaoSupportException {
if (null == entity) {
throw new HibernateDaoSupportException(&Param(#&
+ this.getClass().getName() + &) with value null&);
this.getHibernateTemplate().delete(entity);
log.debug(&Execute delete method is successful!&);
} catch (DataAccessException e) {
log.error(&Error deleting entity:{}&, e);
throw new HibernateDaoSupportException(&Error deleting (#&
+ this.getClass().getName() + &#):& + e);
* The &code&find(T entity)&/code& method is find object according object
* @param entity
if you want to find class condition.
* @return List&T& according entity to find object's connection.
* @throws HibernateDaoSupportException
Throws HibernateDaoSupportException when accessing and
manipulating database happen exception.
@SuppressWarnings(&unchecked&)
public List&T& find(T entity) throws HibernateDaoSupportException {
if (null == entity) {
throw new HibernateDaoSupportException(&Param(#&
+ this.getClass().getName() + &) with value null&);
List lists =
if (isCacheQueries) {
this.getHibernateTemplate().setCacheQueries(true);
isCacheQueries =
this.getHibernateTemplate().setCacheQueries(false);
lists = (List&T&) this.getHibernateTemplate().findByExample(entity);
log.debug(&Execute find method is successful!&);
} catch (DataAccessException e) {
log.error(&Error finding entity: {}&, e);
throw new HibernateDaoSupportException(&Error finding (#&
+ this.getClass().getName() + &#):& + e);
* find object's collection with class
* @param clazz
according class
* @return Object's connection
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
@SuppressWarnings(&unchecked&)
public List&T& find(Class&T& clazz) throws HibernateDaoSupportException {
if (null == clazz) {
throw new HibernateDaoSupportException(
&Param(#clazz) with value null&);
String hql = &FROM & + clazz.getName();
List&T& lists =
if (isCacheQueries) {
this.getHibernateTemplate().setCacheQueries(true);
isCacheQueries =
this.getHibernateTemplate().setCacheQueries(false);
lists = (List&T&) this.getHibernateTemplate().find(hql);
log.debug(&Execute find method is successful!&);
} catch (DataAccessException e) {
log.error(&Error finding entity:{}&, e);
throw new HibernateDaoSupportException(&Error finding (#&
+ this.getClass().getName() + &#):& + e);
* The &code&findById(PK id)&/code& method is find object according
* primary key.
* @param id
if you want to find object's primary key
* @return T insject object
* @throws HibernateDaoSupportException
Throws HibernateDaoSupportException when accessing and
manipulating database happen exception.
@SuppressWarnings(&unchecked&)
public T findById(PK id, Class&T& clazz)
throws HibernateDaoSupportException {
if (null == id) {
throw new HibernateDaoSupportException(&PK with value null&);
T entity =
String hql = &FROM & + clazz.getName() + & n where n.id = &;
if(id instanceof String){
&'&+id+&'&;
//use 2 leave cache
entity = (T) this.getUniqueBeanResult(hql);
log.debug(&Execute findById method is successful!&);
} catch (DataAccessException e) {
log.error(&Error finding entity:{}&, e);
throw new HibernateDaoSupportException(&Error finding (&
+ this.getClass().getName() + &):& + e);
* The &code&queryList(PK startRecord, PK pageSize)&/code& method is query
* objects according startRecord and pagesize're number, object type is
* according your implements this method's define type, and implements this
* interface abstract class must be override all method and inject entity
* @param startRecord
Where from the beginning to show this record
* @param pageSize
The number of records per page
* @param clazz
according class
* @return List&T& T is your inject object's type, List is query all object
connection
* @throws HibernateDaoSupportException
Throws HibernateDaoSupportException when accessing and
manipulating database happen exception.
public List&T& queryList(int startRecord, int pageSize, Class&T& clazz)
throws HibernateDaoSupportException {
return queryList(null, startRecord, pageSize, clazz, null);
* find object's connection with hql class and param map
* @param hql
according hql if class param is null
* @param startRecord
Where from the beginning to show this record
* @param pageSize
the number of records per page
* @param clazz
according class
* @param properties
according param map
* @return Object's connection
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
@SuppressWarnings(&unchecked&)
public List&T& queryList(String hql, final int startRecord,
final int pageSize, Class&T& clazz,
final Map&String, Object& properties)
throws HibernateDaoSupportException {
if (hql == null && clazz == null) {
throw new HibernateDaoSupportException(
&Param(#hql#) and param(#clazz#) is null&);
if (clazz != null) {
hql = &FROM & + clazz.getName();
final String queryHql =
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(queryHql);
if (isCacheQueries) {
query.setCacheable(true);
isCacheQueries =
query.setCacheable(false);
if (!Validity.isEmpty(properties)) {
query.setProperties(properties);
if (startRecord &= 0 && pageSize &= 0) {
query.setFirstResult(startRecord).setMaxResults(
pageSize);
return query.list();
} catch (DataAccessException e) {
log.error(&Error executing queryList ({}):{}&, hql, e);
throw new HibernateDaoSupportException(
&Error executing queryList (& + hql + &):& + e);
* The &code&getRecordCount()&/code& method is used for getting the total
* count of records.
* @return PK return total of record counts
* @throws HibernateDaoSupportException
Throws HibernateDaoSupportException when accessing and
manipulating database happen exception.
public long getRecordCount(Class&T& clazz)
throws HibernateDaoSupportException {
String hql = &SELECT COUNT(*) FROM & + clazz.getName();
Integer count =
if (isCacheQueries) {
this.getHibernateTemplate().setCacheQueries(true);
isCacheQueries =
this.getHibernateTemplate().setCacheQueries(false);
count = ((Long) this.getHibernateTemplate().find(hql).iterator()
.next()).intValue();
log.debug(&Execute getRecordCount method is successful!&);
} catch (DataAccessException e) {
log.error(&Error getting count:{}&, e);
throw new HibernateDaoSupportException(&Error getting count for (&
+ this.getClass().getName() + &):& + e);
* get count with select hql and param map
* @param selectHql
according select hql
* @param properties
according param map
* @return count of hql
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
public int getRecordCount(String selectHql, Map&String, Object& properties)
throws HibernateDaoSupportException {
String countHql = getCountHql(selectHql);
return ((Long) getUniqueBeanResult(countHql, properties)).intValue();
* The &code&modifty(T entity)&/code& method is update object to database.
* @param entity
if you want to update entity.
* @throws HibernateDaoSupportException
Throws HibernateDaoSupportException when accessing and
manipulating database happen exception.
public void modify(T entity) throws HibernateDaoSupportException {
if (null == entity) {
throw new HibernateDaoSupportException(&Param(#&
+ this.getClass().getName() + &) with value null&);
this.getHibernateTemplate().update(entity);
log.debug(&Execute update method is successful!&);
} catch (DataAccessException e) {
log.error(&Error updating entity:{}&, e);
throw new HibernateDaoSupportException(&Error updating (#&
+ this.getClass().getName() + &#):& + e);
* find page object's connection with class
* @param clazz
according class
* @param currentPage
current page
* @param pageSize
the number of records per page
* @return Object's connection
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
public Pager&T& findPager(int currentPage, int pageSize, Class&T& clazz)
throws HibernateDaoSupportException {
return findPager(null, currentPage, pageSize, clazz, null);
* find page object's connection with hql and param map
* @param hql
according hql if class param is null
* @param currentPage
current page
* @param pageSize
the number of records per page
* @param properties
according param map
* @return Object's connection
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
public Pager&T& findPager(String hql, int currentPage, int pageSize,
Map&String, Object& properties) throws HibernateDaoSupportException {
return findPager(hql, currentPage, pageSize, null, properties);
* find page object's connection with sql and param map
* @param sql
according sql if class param is null
* @param currentPage
current page
* @param pageSize
the number of records per page
* @param properties
according param map
* @return Object's connection
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
@SuppressWarnings(&unchecked&)
public Pager findPagerBySql(String sql, int currentPage,
int pageSize, Map&String, Object& properties)
throws HibernateDaoSupportException {
log.debug(&sql:{},currentPage:{},pageSize:{},propertie:{}&,
new Object[] { sql, currentPage, pageSize, properties });
if (currentPage &= 0) {
throw new HibernateDaoSupportException(
&Param(#currentPage#) value : { & + currentPage + & }&);
int totalRecords = 0;
String countSql = getCountHql(sql);
this.getUniqueBeanResultSql(countSql, properties);
totalRecords = ((BigInteger) this.getUniqueBeanResultSql(countSql,
properties)).intValue();
Pager page = new Pager();
List list =
page.setTotal(totalRecords);
page.setPageSize(pageSize);
page.setCurrentPage(currentPage);
list = this.queryListSql(sql, (currentPage - 1) * pageSize, pageSize,
properties);
page.setPageRecords(list);
* find object's connection with sql class and param map
* @param sql
according sql if class param is null
* @param startRecord
Where from the beginning to show this record
* @param pageSize
the number of records per page
* @param properties
according param map
* @return Object's connection
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
@SuppressWarnings(&unchecked&)
public List queryListSql(final String sql, final int startRecord,
final int pageSize, final Map&String, Object& properties)
throws HibernateDaoSupportException {
if (sql == null) {
throw new HibernateDaoSupportException(&Param(#sql#) is null&);
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createSQLQuery(sql);
if (!Validity.isEmpty(properties)) {
query.setProperties(properties);
if (startRecord &= 0 && pageSize &= 0) {
query.setFirstResult(startRecord).setMaxResults(
pageSize);
return query.list();
} catch (DataAccessException e) {
log.error(&Error executing queryList ({}):{}&, sql, e);
throw new HibernateDaoSupportException(
&Error executing queryList (& + sql + &):& + e);
* find page object's connection with hql class and param map
* @param hql
according hql
* @param currentPage
current page
* @param pageSize
the number of records per page
* @param clazz
according class
* @param properties
according param map
* @return Object's connection
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
private Pager&T& findPager(String hql, int currentPage, int pageSize,
Class&T& clazz, Map&String, Object& properties)
throws HibernateDaoSupportException {
log.debug(&hql:{},currentPage:{},pageSize:{},clazz:{},propertie:{}&,
new Object[] { hql, currentPage, pageSize, clazz, properties });
boolean tempCacheQueries = isCacheQ
if (currentPage &= 0) {
throw new HibernateDaoSupportException(
&Param(#currentPage#) value : { & + currentPage + & }&);
currentPage = 1;
int totalRecords = 0;
if (clazz != null) {
totalRecords = (int)this.getRecordCount(clazz);//TODO long to int?
totalRecords = this.getRecordCount(hql, properties);
if (!tempCacheQueries) {
isCacheQueries =
Pager&T& page = new Pager&T&();
List&T& list =
page.setTotal(totalRecords);
page.setPageSize(pageSize);
list = this.queryList(hql, (currentPage - 1) * pageSize, pageSize,
clazz, properties);
//直到找到有数据的页码为止
while(list.size() == 0 && currentPage & 1){
currentPage= currentPage -1;
list = this.queryList(hql, (currentPage - 1) * pageSize, pageSize,
clazz, properties);
page.setCurrentPage(currentPage);
page.setPageRecords(list);
* find object's connection with hql
* @param hql
according hql
* @return Object's connection
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
public List&T& getObjects(String hql) throws HibernateDaoSupportException {
return getObjects(hql, new HashMap&String, Object&(0));
* find object's connection with hql and param map
* @param hql
according hql
* @param properties
according param map
* @return Object's connection
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
@SuppressWarnings(&unchecked&)
public List&T& getObjects(final String hql,
final Map&String, Object& properties)
throws HibernateDaoSupportException {
log.debug(&hql:{}, properties:{}&, hql, properties);
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(hql);
if (isCacheQueries) {
query.setCacheable(true);
isCacheQueries =
query.setCacheable(false);
if (!Validity.isEmpty(properties)) {
query.setProperties(properties);
List&T& list = query.list();
} catch (DataAccessException e) {
log.error(&Error getObjects:{}&, e);
throw new HibernateDaoSupportException(&Error getObjects (#&
+ this.getClass().getName() + &#):& + e);
* find object with hql and param map
* @param hql
according hql
* @param properties
according param map
* @return Object which find
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
public Object getUniqueBeanResult(final String hql,
final Map&String, Object& properties)
throws HibernateDaoSupportException {
log.debug(&hql:{}, properties:{}&, hql, properties);
return getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(hql);
if (isCacheQueries) {
query.setCacheable(true);
isCacheQueries =
query.setCacheable(false);
if (!Validity.isEmpty(properties)) {
query.setProperties(properties);
Object object = query.uniqueResult();
} catch (DataAccessException e) {
log.error(&Error getUniqueBeanResult:{}&, e);
throw new HibernateDaoSupportException(
&Error getUniqueBeanResult (#& + this.getClass().getName()
+ &#):& + e);
* find object with sql and param map
* @param sql
according sql
* @param properties
according param map
* @return Object which find
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
public Object getUniqueBeanResultSql(final String sql,
final Map&String, Object& properties)
throws HibernateDaoSupportException {
log.debug(&sql:{}, properties:{}&, sql, properties);
return getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createSQLQuery(sql);
if (!Validity.isEmpty(properties)) {
query.setProperties(properties);
Object object = query.uniqueResult();
} catch (DataAccessException e) {
log.error(&Error getUniqueBeanResult:{}&, e);
throw new HibernateDaoSupportException(
&Error getUniqueBeanResult (#& + this.getClass().getName()
+ &#):& + e);
* execute with sql and param arrary
* @param sql
according sql
* @param values
according param arrary
* @return the count of success record
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
public int executeSql(final String sql, final Map&String, Object& values)
throws HibernateDaoSupportException {
log.debug(&hql:{}, params:{}&, sql, values);
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createSQLQuery(sql);
if (!Validity.isEmpty(values)) {
query.setProperties(values);
int i = query.executeUpdate();
} catch (DataAccessException e) {
log.error(&Error executeUpdate:{}&, e);
throw new HibernateDaoSupportException(&Error executeUpdate (#&
+ this.getClass().getName() + &#):& + e);
* find object with hql
* @param hql
according hql
* @return Object which find
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
public Object getUniqueBeanResult(String hql)
throws HibernateDaoSupportException {
return getUniqueBeanResult(hql, new HashMap&String, Object&(0));
* update entity with hql and param map
* @param hql
according hql
* @param properties
according param map
* @return the count of success record
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
public int executeUpdate(final String hql,
final Map&String, Object& properties)
throws HibernateDaoSupportException {
log.debug(&hql:{}, properties:{}&, hql, properties);
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(hql);
if (!Validity.isEmpty(properties)) {
query.setProperties(properties);
int i = query.executeUpdate();
} catch (DataAccessException e) {
log.error(&Error executeUpdate:{}&, e);
throw new HibernateDaoSupportException(&Error executeUpdate (#&
+ this.getClass().getName() + &#):& + e);
* update entity with hql and param arrary
* @param hql
according hql
* @param values
according param arrary
* @return the count of success record
* @throws HibernateDaoSupportException
when accessing and manipulating database happen exception
public int executeUpdate(final String hql, final Object[] values)
throws HibernateDaoSupportException {
String params = converArrayToString(values);
log.debug(&hql:{}, params:{}&, hql, params);
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(hql);
setQueryParam(query, values);
int i = query.executeUpdate();
} catch (DataAccessException e) {
log.error(&Error executeUpdate:{}&, e);
throw new HibernateDaoSupportException(&Error executeUpdate (#&
+ this.getClass().getName() + &#):& + e);
* group array to string like this (a,b,c)
* @param params
array param
* @return String like this (a,b,c)
protected String groupInParam(String[] params) {
StringBuffer inParam = new StringBuffer();
inParam.append(& (&);
for (int i = 0; i & params. i++) {
inParam.append(params[i]);
if (i != params.length - 1) {
inParam.append(&,&);
inParam.append(&) &);
return inParam.toString();
* convert special char
* @param value
string which need to convert
* @return converted string
protected String convertSpecialChar(String value) {
String s =
if (value != null) {
s = value.replace(&[&, &\\\\[&).replace(&]&, &\\\\]&).replace(&%&,
&\\\\%&).replace(&_&, &\\\\_&).replace(&^&, &\\\\^&)
.replace(&\\&, &&).replace(&'&, &&);
* get count hql with select hql
* @param hql
select hql
* @return count hql
protected String getCountHql(String hql) {
if (Validity.isEmpty(hql)) {
log.error(&Error getHqlBean because hql is empty&);
return &&;
return &select count(*) &
.substring(hql.indexOf(&from&))
.replace(&fetch&, &&)
.replaceAll(
&\\s((?mi)(left|right|inner)?\\s+join)\\s+[^\\s]*Set\\b&,
& &).split(&order by&)[0];
* set param in query
* @param query
* @param values
param value
private void setQueryParam(Query query, Object[] values) {
if (values != null) {
for (int i = 0; i & values. i++) {
query.setParameter(i, values[i]);
* conver array to string
* @param values
array value
* @return string value
private String converArrayToString(Object[] values) {
if (values == null) {
return &&;
StringBuffer formatValues = new StringBuffer();
for (Object value : values) {
formatValues.append(&{& + value + &}&);
return formatValues.toString();
@SuppressWarnings(&unchecked&)
public List&T& queryListByHql( String hql, Class&T& clazz,final Map&String, Object& properties) throws HibernateDaoSupportException{
if (hql == null) {
throw new HibernateDaoSupportException(
&Param(#hql#) and param(#clazz#) is null&);
if (clazz != null) {
hql = &FROM & + clazz.getName();
final String queryHql =
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(queryHql);
if (isCacheQueries) {
query.setCacheable(true);
isCacheQueries =
query.setCacheable(false);
if (!Validity.isEmpty(properties)) {
query.setProperties(properties);
return query.list();
} catch (DataAccessException e) {
log.error(&Error executing queryListByHql ({}):{}&, hql, e);
throw new HibernateDaoSupportException(
&Error executing queryListByHql (& + hql + &):& + e);
* @param hql
* @param properties
* @throws HibernateDaoSupportException
public List queryListByHql(String hql,final Map&String, Object& properties) throws HibernateDaoSupportException {
if (hql == null) {
throw new HibernateDaoSupportException(
&Param(#hql#) and param(#clazz#) is null&);
final String queryHql =
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(queryHql);
if (isCacheQueries) {
query.setCacheable(true);
isCacheQueries =
query.setCacheable(false);
if (!Validity.isEmpty(properties)) {
query.setProperties(properties);
return query.list();
} catch (DataAccessException e) {
log.error(&Error executing queryListByHql ({}):{}&, hql, e);
throw new HibernateDaoSupportException(
&Error executing queryListByHql (& + hql + &):& + e);
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:71549次
积分:3365
积分:3365
排名:第3283名
原创:277篇
转载:20篇
评论:30条
(4)(1)(4)(1)(7)(6)(3)(12)(7)(13)(14)(6)(21)(19)(26)(50)(42)(48)(13)

我要回帖

更多关于 ibatis basedao 的文章

 

随机推荐