






2.0

Spring applicationContext.xml配置(例子)
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
Spring basedao(通用)
package com.test.mydb;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class SpringBasedao extends HibernateDaoSupport {
//添加单对象
public int save(Object o){
int row=0;
try {
super.getHibernateTemplate().save(o);
row=1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
//修改单对象
public int update(Object o){
int row=0;
try {
super.getHibernateTemplate().clear();
super.getHibernateTemplate().update(o);
row=1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
//删除单对象
public int delete(Class cls,Serializable id){
int row=0;
Object o=get(cls, id);
try {
super.getHibernateTemplate().delete(o);
row=1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
//根据ID(主键)获取单个对象
public Object get(Class cls,Serializable id){
return getHibernateTemplate().get(cls, id);
}
//查询获取对象集合,hql查询语句
public List getlistbyhql(String hql){
List list=new ArrayList();
list=getHibernateTemplate().find(hql);
return list;
}
public List getlistbyhql(final String hql,final String[] parms){
List list=new ArrayList();
list=getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query=session.createQuery(hql);
if(parms!=null){
for (int i = 0; i < parms.length; i++) {
query.setString(i, parms[i]);
}
}
return query.list();
}
});
return list;
}
//查询数据量
public Object getuniqbyhql(final String hql){
Object o=getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query=session.createQuery(hql);
return query.uniqueResult();
}
});
return o;
}
public Object getuniqbyhql(final String hql,final String[] parms){
Object o=getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLExcepti