|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (4) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-04-05
前段时间在学习Hibernate,今天正式开始用,在用的过程中我为每个表(对象)做了一个DAO,主要用来实现增删改查,因为里面的很多的表(对象)需要写很多DAO,所以我就抽象了一个类,先前也没觉得怎么,不过到后来觉得我抽象出来的这个东西好象能通用吧...
现在拿出来让大家看看我这样抽象是否正确,是否对大家有点帮助...同时也希望它能完善... 自己只是简单的测了一下下
package com.st.dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.st.hibernate.HibernateSessionFactory;
public abstract class abstractDAO
{
private Object obj;
// 实现增加,删除,修改的功能
public boolean idu(char t)
{
if(this.obj!=null)
{
Session session=HibernateSessionFactory.getSession();
Transaction ts=session.beginTransaction();
switch (t)
{
case 'i':
insert(this.obj,session);
break;
case 'd':
delete(this.obj,session);
break;
case 'u':
update(this.obj,session);
break;
}
ts.commit();
session.close();
return true;
}
return false;
}
//查询
public List select(char t)
{
Session session=HibernateSessionFactory.getSession();
Transaction ts=session.beginTransaction();
List list=null;
switch (t)
{
case 'f':
list=selectFactor(this.obj,session);
break;
case 'a':
list=selectAll(session);
break;
}
ts.commit();
session.close();
return list;
}
//删除
private void delete(Object obj,Session session)
{
session.delete(obj);
}
//更新
private void update(Object obj,Session session)
{
session.update(obj);
}
//添加
private void insert(Object obj,Session session)
{
session.save(obj);
}
//查询出所有的记录
private List selectAll(Session session)
{
Criteria criteria=session.createCriteria(this.obj.getClass());
return criteria.list();
}
//查询,这个查询主要是用来进行模糊查询,或有条件的查询
protected abstract List selectFactor(Object obj,Session session);
// {
// if(p!=null)
// {
// Products tempp=null;
//// Query qu=session.createQuery("from Products");
// Criteria criteria=session.createCriteria(Products.class);
// //得到一个Products的克隆版本
// try
// {
// tempp=(Products) p.clone();
// } catch (CloneNotSupportedException e)
// {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// criteria.add(Example.create(tempp));
// if(this.products.getName()!=null && !this.products.getName().equals(""))
// criteria.add(Restrictions.like("name", "%"+p.getName()+"%"));
// return criteria.list();
// }
// else
// return null;
// }
public Object getObj()
{
return obj;
}
public void setObj(Object obj)
{
this.obj = obj;
}
}
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2008-04-05
public boolean idu(char t)这个方法严重有问题。为什么要把增删改用这个间接实现?查询方法也一样。头一次看见这么写的。而且方法传入的那几个char也没有做静态属性,别人不知道你源代码的情况下谁知道应该往里头传什么。增删改方法如果一个请求内多次调用,每次都要提交一次,等于实际上没有事务控制。如果中间操作出错,事务原子性根本无法保证。建议你去看看Spring的HibernateSupper。
|
|
| 返回顶楼 | |
|
时间:2008-04-05
是啊,这种写法 public boolean idu(char t) 真的很奇怪,外界调用的人,根本不知道该怎么使用,增删改查 四个操作本来就是很独立的,完全可以分拆为独立的对外调用接口,干嘛非要这么融合在一起啊,感觉就是为了融合而融合
|
|
| 返回顶楼 | |
|
时间:2008-04-05
建议楼主搜索一下“泛型DAO”,或许会有收获
|
|
| 返回顶楼 | |
|
时间:2008-04-05
c,,u,d等方法最好不要混在一起
|
|
| 返回顶楼 | |
|
时间:2008-04-07
我最初的想法只是觉得增删改它们基本上都没有什么返回
所以就把它们给合并了 谢谢 魔力猫咪 hahastone movingboy 348105874 几位朋友对我的指点 我会再去注意的 |
|
| 返回顶楼 | |
|
时间:2008-04-08
帖一段我们的baseDao给你看看
public class BaseDaoHibernate extends HibernateDaoSupport implements BaseDao {
private static Log log = LogFactory.getLog(BaseDaoHibernate.class);
public void save(final Object obj) {
getHibernateTemplate().save(obj);
// save2(obj);
}
private Serializable save(Session session, Object p) {
Transaction t = session.beginTransaction();
Serializable o = session.save(p);
t.commit();
session.flush();
session.close();
return o;
}
public Serializable save2(final Object entity) throws DataAccessException {
Object o = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
return save(session, entity);
}
});
return null;
}
public void deleteAll(final Object o) {
String hql = " delete from " + o.getClass().getName() + " ";
executeUpdate(hql);
}
/**
* 根据查询语句,返回对象列表
*
* @param hql
* 查询语句
* @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
*/
public List find(final String hql) {
final int firstResult = 0;
return find(hql, firstResult, Constants.MAXRESULTS);
}
/**
* 根据查询语句,返回对象列表
*
* @param hql
* 查询语句
* @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
*/
public List find(final String hql, List tablesName) {
final int firstResult = 0;
return find(hql, firstResult, Constants.MAXRESULTS);
}
/**
* 返回指定起始位置,指定条数的对象
*
* @param hql
* 查询语句
* @param firstResult
* 起始位置
* @param maxResults
* 最多纪录数
* @return list 结果列表
*/
public List find(final String hql, final int firstResult,
final int maxResults) {
if (log.isDebugEnabled()) {
log.debug("hql=" + hql);
}
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
try {
session.connection().setReadOnly(true);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Query queryObject = session.createQuery(hql);
queryObject.setReadOnly(true);
queryObject.setFirstResult(firstResult);
queryObject.setMaxResults(maxResults);
return queryObject.list();
}
});
}
/**
* 返回指定起始位置,指定条数的对象
*
* @param hql
* 查询语句
* @param firstResult
* 起始位置
* @param maxResults
* 最多纪录数
* @return list 结果列表
*/
public List findByIterate(final String hql) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
Query queryObject = session.createQuery(hql);
queryObject.setCacheable(true);
Iterator iter = queryObject.iterate();
List ls = new ArrayList();
while (iter.hasNext()) {
ls.add(iter.next());
}
return ls;
}
});
}
/**
* 查询语句需要的条件参数。通过map传递
*
* @param hql
* 查询语句
* @param map
* 参数
* @param firstResult
* 起始位置
* @param maxResults
* 最多纪录数
* @return list 结果列表
*/
public List find(final String hql, final Map map, final int firstResult,
final int maxResults) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
Query queryObject = session.createQuery(hql);
String[] params = queryObject.getNamedParameters();
for (int i = 0, max = params.length; i < max; i++) {
queryObject.setParameter(params[i], map.get(params[i]));
}
queryObject.setFirstResult(firstResult);
queryObject.setMaxResults(maxResults);
return queryObject.list();
}
});
}
/**
* 根据查询语句,返回对象列表
*
* @param hql
* 查询语句
* @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
*/
public List find(final String hql, final Map map) {
final int firstResult = 0;
return find(hql, map, firstResult, Constants.MAXRESULTS);
}
/**
* 通过Hql 执行update/delete操作
*
* @param hql
* @return
*/
public int executeUpdate(final String hql) {
int result = 0;
Object o = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
int result = 0;
Query queryObject = session.createQuery(hql);
result = queryObject.executeUpdate();
return result;
}
});
Integer i = (Integer) o;
result = i.intValue();
return result;
}
public void deleteAllByObject(Object obj) {
List l = null;
String hql = " from " + obj.getClass().getName() + " ";
if (log.isDebugEnabled()) {
log.debug("hql=" + hql);
}
l = find(hql);
Iterator ait = l.iterator();
while (ait != null && ait.hasNext()) {
getHibernateTemplate().delete(ait.next());
}
}
/**
* 通过 DetachedCriteria 进行查询
*
* @param dc
* @return
*/
public List findByCriteria(final DetachedCriteria dc) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
/**
* 通过 DetachedCriteria 进行查询
*
* @param dc
* @return
*/
public List findByCriteria(final DetachedCriteria dc,
final int firstResult, final int maxResults) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("findByCriteria dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
criteria.setFirstResult(firstResult);
criteria.setMaxResults(maxResults);
return criteria.list();
}
}, true);
}
public int countByCriteria(final DetachedCriteria dc) {
Integer count = (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("countByCriteria dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount())
.uniqueResult();
}
}, true);
if (count != null) {
return count.intValue();
} else {
return 0;
}
}
/**
* 通过 DetachedCriteria 进行查询
*
* @param dc
* @return
*/
public List findByCriteriaCache(final DetachedCriteria dc) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
criteria.setCacheable(true);
return criteria.list();
}
}, true);
}
/**
* 通过 DetachedCriteria 进行查询
*
* @param dc
* @return
*/
public List findByCriteriaCache(final DetachedCriteria dc,
final int firstResult, final int maxResults) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
criteria.setCacheable(true);
criteria.setFirstResult(firstResult);
criteria.setMaxResults(maxResults);
return criteria.list();
}
}, true);
}
/**
* count search cache
*/
public int countByCriteriaCache(final DetachedCriteria dc) {
Integer count = (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
criteria.setCacheable(true);
return criteria.setProjection(Projections.rowCount())
.uniqueResult();
}
}, true);
if (count != null) {
return count.intValue();
} else {
return 0;
}
}
public int executeUpdate(final String hql, final Map pMap) {
int result = 0;
Object o = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
int result = 0;
Query queryObject = session.createQuery(hql);
String[] params = queryObject.getNamedParameters();
for (int i = 0, max = params.length; i < max; i++) {
queryObject.setParameter(params[i], pMap.get(params[i]));
}
result = queryObject.executeUpdate();
return result;
}
});
Integer i = (Integer) o;
result = i.intValue();
return result;
}
public void delete(Object obj) {
getHibernateTemplate().delete(obj);
}
public Object load(Class aclass, Serializable id)
throws DataAccessException {
Object obj = getHibernateTemplate().load(aclass, id);
return obj;
}
public Object get(Class aclass, Serializable id) {
Object obj = getHibernateTemplate().get(aclass, id);
return obj;
}
public void saveorUpdate(Object obj) {
getHibernateTemplate().saveOrUpdate(obj);
getHibernateTemplate().flush();
}
public void update(Object o) {
getHibernateTemplate().update(o);
}
/**
* count hql 方法 .
*/
public int count(String hql, List ls) {
String countQueryString = " select count (*) " + hql;
List countlist = getHibernateTemplate().find(countQueryString);
Long count = (Long) countlist.get(0);
return count.intValue();
}
/**
* count hql 方法 .
*/
public int count(String hql, Map params) {
String countQueryString = " select count (*) " + hql;
List countlist = find(countQueryString, params);
Object co = countlist.get(0);
if (co instanceof Integer) {
Integer count = (Integer) countlist.get(0);
return count.intValue();
} else {
Long count = (Long) countlist.get(0);
return count.intValue();
}
}
/**
* TODO count hql 方法 .
*/
public int countCache(String hql, Map params) {
String countQueryString = " select count (*) " + hql;
List countlist = findCache(countQueryString, params);
Object co = countlist.get(0);
if (co instanceof Integer) {
Integer count = (Integer) countlist.get(0);
return count.intValue();
} else {
Long count = (Long) countlist.get(0);
return count.intValue();
}
}
/**
* 根据查询语句,返回对象列表 TODO
*
* @param hql
* 查询语句
* @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
*/
public List findCache(final String hql, final Map map) {
final int firstResult = 0;
return findCache(hql, map, firstResult, Constants.MAXRESULTS);
}
/**
* 查询语句需要的条件参数。通过map传递 TODO
*
* @param hql
* 查询语句
* @param map
* 参数
* @param firstResult
* 起始位置
* @param maxResults
* 最多纪录数
* @return list 结果列表
*/
public List findCache(final String hql, final Map map,
final int firstResult, final int maxResults) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
Query queryObject = session.createQuery(hql);
// 设置 查询cache
queryObject.setCacheable(true);
String[] params = queryObject.getNamedParameters();
for (int i = 0, max = params.length; i < max; i++) {
queryObject.setParameter(params[i], map.get(params[i]));
}
queryObject.setFirstResult(firstResult);
queryObject.setMaxResults(maxResults);
return queryObject.list();
}
});
}
public int count(String hql) {
String countQueryString = " select count (*) " + hql;
List countlist = find(countQueryString);
Object co = countlist.get(0);
if (co instanceof Integer) {
Integer count = (Integer) countlist.get(0);
return count.intValue();
} else {
Long count = (Long) countlist.get(0);
return count.intValue();
}
}
@Override
public HibernateTemplate getTemplate() {
return getHibernateTemplate();
}
|
|
| 返回顶楼 | |
|
时间:2008-04-08
lsk 写道 帖一段我们的baseDao给你看看
public class BaseDaoHibernate extends HibernateDaoSupport implements BaseDao {
private static Log log = LogFactory.getLog(BaseDaoHibernate.class);
public void save(final Object obj) {
getHibernateTemplate().save(obj);
// save2(obj);
}
private Serializable save(Session session, Object p) {
Transaction t = session.beginTransaction();
Serializable o = session.save(p);
t.commit();
session.flush();
session.close();
return o;
}
public Serializable save2(final Object entity) throws DataAccessException {
Object o = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
return save(session, entity);
}
});
return null;
}
public void deleteAll(final Object o) {
String hql = " delete from " + o.getClass().getName() + " ";
executeUpdate(hql);
}
/**
* 根据查询语句,返回对象列表
*
* @param hql
* 查询语句
* @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
*/
public List find(final String hql) {
final int firstResult = 0;
return find(hql, firstResult, Constants.MAXRESULTS);
}
/**
* 根据查询语句,返回对象列表
*
* @param hql
* 查询语句
* @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
*/
public List find(final String hql, List tablesName) {
final int firstResult = 0;
return find(hql, firstResult, Constants.MAXRESULTS);
}
/**
* 返回指定起始位置,指定条数的对象
*
* @param hql
* 查询语句
* @param firstResult
* 起始位置
* @param maxResults
* 最多纪录数
* @return list 结果列表
*/
public List find(final String hql, final int firstResult,
final int maxResults) {
if (log.isDebugEnabled()) {
log.debug("hql=" + hql);
}
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
try {
session.connection().setReadOnly(true);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Query queryObject = session.createQuery(hql);
queryObject.setReadOnly(true);
queryObject.setFirstResult(firstResult);
queryObject.setMaxResults(maxResults);
return queryObject.list();
}
});
}
/**
* 返回指定起始位置,指定条数的对象
*
* @param hql
* 查询语句
* @param firstResult
* 起始位置
* @param maxResults
* 最多纪录数
* @return list 结果列表
*/
public List findByIterate(final String hql) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
Query queryObject = session.createQuery(hql);
queryObject.setCacheable(true);
Iterator iter = queryObject.iterate();
List ls = new ArrayList();
while (iter.hasNext()) {
ls.add(iter.next());
}
return ls;
}
});
}
/**
* 查询语句需要的条件参数。通过map传递
*
* @param hql
* 查询语句
* @param map
* 参数
* @param firstResult
* 起始位置
* @param maxResults
* 最多纪录数
* @return list 结果列表
*/
public List find(final String hql, final Map map, final int firstResult,
final int maxResults) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
Query queryObject = session.createQuery(hql);
String[] params = queryObject.getNamedParameters();
for (int i = 0, max = params.length; i < max; i++) {
queryObject.setParameter(params[i], map.get(params[i]));
}
queryObject.setFirstResult(firstResult);
queryObject.setMaxResults(maxResults);
return queryObject.list();
}
});
}
/**
* 根据查询语句,返回对象列表
*
* @param hql
* 查询语句
* @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
*/
public List find(final String hql, final Map map) {
final int firstResult = 0;
return find(hql, map, firstResult, Constants.MAXRESULTS);
}
/**
* 通过Hql 执行update/delete操作
*
* @param hql
* @return
*/
public int executeUpdate(final String hql) {
int result = 0;
Object o = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
int result = 0;
Query queryObject = session.createQuery(hql);
result = queryObject.executeUpdate();
return result;
}
});
Integer i = (Integer) o;
result = i.intValue();
return result;
}
public void deleteAllByObject(Object obj) {
List l = null;
String hql = " from " + obj.getClass().getName() + " ";
if (log.isDebugEnabled()) {
log.debug("hql=" + hql);
}
l = find(hql);
Iterator ait = l.iterator();
while (ait != null && ait.hasNext()) {
getHibernateTemplate().delete(ait.next());
}
}
/**
* 通过 DetachedCriteria 进行查询
*
* @param dc
* @return
*/
public List findByCriteria(final DetachedCriteria dc) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
/**
* 通过 DetachedCriteria 进行查询
*
* @param dc
* @return
*/
public List findByCriteria(final DetachedCriteria dc,
final int firstResult, final int maxResults) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("findByCriteria dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
criteria.setFirstResult(firstResult);
criteria.setMaxResults(maxResults);
return criteria.list();
}
}, true);
}
public int countByCriteria(final DetachedCriteria dc) {
Integer count = (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("countByCriteria dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount())
.uniqueResult();
}
}, true);
if (count != null) {
return count.intValue();
} else {
return 0;
}
}
/**
* 通过 DetachedCriteria 进行查询
*
* @param dc
* @return
*/
public List findByCriteriaCache(final DetachedCriteria dc) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
criteria.setCacheable(true);
return criteria.list();
}
}, true);
}
/**
* 通过 DetachedCriteria 进行查询
*
* @param dc
* @return
*/
public List findByCriteriaCache(final DetachedCriteria dc,
final int firstResult, final int maxResults) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
criteria.setCacheable(true);
criteria.setFirstResult(firstResult);
criteria.setMaxResults(maxResults);
return criteria.list();
}
}, true);
}
/**
* count search cache
*/
public int countByCriteriaCache(final DetachedCriteria dc) {
Integer count = (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
if (log.isDebugEnabled()) {
log.debug("dc=" + dc);
}
Criteria criteria = dc.getExecutableCriteria(session);
criteria.setCacheable(true);
return criteria.setProjection(Projections.rowCount())
.uniqueResult();
}
}, true);
if (count != null) {
return count.intValue();
} else {
return 0;
}
}
public int executeUpdate(final String hql, final Map pMap) {
int result = 0;
Object o = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
int result = 0;
Query queryObject = session.createQuery(hql);
String[] params = queryObject.getNamedParameters();
for (int i = 0, max = params.length; i < max; i++) {
queryObject.setParameter(params[i], pMap.get(params[i]));
}
result = queryObject.executeUpdate();
return result;
}
});
Integer i = (Integer) o;
result = i.intValue();
return result;
}
public void delete(Object obj) {
getHibernateTemplate().delete(obj);
}
public Object load(Class aclass, Serializable id)
throws DataAccessException {
Object obj = getHibernateTemplate().load(aclass, id);
return obj;
}
public Object get(Class aclass, Serializable id) {
Object obj = getHibernateTemplate().get(aclass, id);
return obj;
}
public void saveorUpdate(Object obj) {
getHibernateTemplate().saveOrUpdate(obj);
getHibernateTemplate().flush();
}
public void update(Object o) {
getHibernateTemplate().update(o);
}
/**
* count hql 方法 .
*/
public int count(String hql, List ls) {
String countQueryString = " select count (*) " + hql;
List countlist = getHibernateTemplate().find(countQueryString);
Long count = (Long) countlist.get(0);
return count.intValue();
}
/**
* count hql 方法 .
*/
public int count(String hql, Map params) {
String countQueryString = " select count (*) " + hql;
List countlist = find(countQueryString, params);
Object co = countlist.get(0);
if (co instanceof Integer) {
Integer count = (Integer) countlist.get(0);
return count.intValue();
} else {
Long count = (Long) countlist.get(0);
return count.intValue();
}
}
/**
* TODO count hql 方法 .
*/
public int countCache(String hql, Map params) {
String countQueryString = " select count (*) " + hql;
List countlist = findCache(countQueryString, params);
Object co = countlist.get(0);
if (co instanceof Integer) {
Integer count = (Integer) countlist.get(0);
return count.intValue();
} else {
Long count = (Long) countlist.get(0);
return count.intValue();
}
}
/**
* 根据查询语句,返回对象列表 TODO
*
* @param hql
* 查询语句
* @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
*/
public List findCache(final String hql, final Map map) {
final int firstResult = 0;
return findCache(hql, map, firstResult, Constants.MAXRESULTS);
}
/**
* 查询语句需要的条件参数。通过map传递 TODO
*
* @param hql
* 查询语句
* @param map
* 参数
* @param firstResult
* 起始位置
* @param maxResults
* 最多纪录数
* @return list 结果列表
*/
public List findCache(final String hql, final Map map,
final int firstResult, final int maxResults) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException {
Query queryObject = session.createQuery(hql);
// 设置 查询cache
queryObject.setCacheable(true);
String[] params = queryObject.getNamedParameters();
for (int i = 0, max = params.length; i < max; i++) {
queryObject.setParameter(params[i], map.get(params[i]));
}
queryObject.setFirstResult(firstResult);
queryObject.setMaxResults(maxResults);
return queryObject.list();
}
});
}
public int count(String hql) {
String countQueryString = " select count (*) " + hql;
List countlist = find(countQueryString);
Object co = countlist.get(0);
if (co instanceof Integer) {
Integer count = (Integer) countlist.get(0);
return count.intValue();
} else {
Long count = (Long) countlist.get(0);
return count.intValue();
}
}
@Override
public HibernateTemplate getTemplate() {
return getHibernateTemplate();
}
使用了Spring的Hibernate支持,怎么不用Spring的声明式事务管理?private Serializable save(Session session, Object p) 方法居然自己控制事务。但是其他方法又没有事务。到底事务控制是方应用层控制还是扔DAO里了? 还有public Serializable save2(final Object entity) throws DataAccessException 命名不科学。没人知道save2是什么意思。public int executeUpdate(final String hql)暴露了DAO实现,没有把领域层和持久化层隔离。而且这个方法会造成缓存失效,没有清理缓存的语句。 |
|
| 返回顶楼 | |
|
时间:2008-04-08
movingboy 写道 建议楼主搜索一下“泛型DAO”,或许会有收获
我做的项目重中, 就为公司写了一个泛型DAO, 所有表或者视图的基本操作全封装在里面了。 |
|
| 返回顶楼 | |
|
时间:2008-04-08
恩。不用范型类型转换太麻烦,用范型,基本就需要一个DAO,别的具体操作可以方在Service层里。
|
|
| 返回顶楼 | |










