package dao.hibernate;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import dao.Dao;
public class AccountDaoImpl extends HibernateDaoSupport implements Dao{
public Serializable sava(Object object) {
return getHibernateTemplate().save(object);
}
public List getObject(Class clazz){
return getHibernateTemplate().loadAll(clazz);
}
public Object getObject(Class clazz,Serializable id){
return getHibernateTemplate().get(clazz, id);
}
public List findAllBy(final Class clazz,final String name,final String value){
return (List) getHibernateTemplate().execute(
new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Criteria criteria=session.createCriteria(clazz);
criteria.add(Restrictions.eq(name,value));
List list=criteria.list();
return list;
}
});
}
public Object findBy(final Class clazz,final String name,final String value){
return getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Criteria critera=session.createCriteria(clazz);
critera.add(Restrictions.eq(name,value));
List list=critera.list();
return list==null||list.isEmpty()?null:list.iterator().next();
}
});
}
public void updateObject(Object object){
getHibernateTemplate().update(object);
}
public void removeObject(final Class clazz,Serializable id){
getHibernateTemplate().delete(getObject(clazz,id));
}
public void removeObject(Object o){
getHibernateTemplate().delete(o);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE
beans
PUBLIC
"-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="daoImpl" class="dao.hibernate.AccountDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean
id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
parent="AbstractSessionFactory">
<property name="annotatedClasses">
<list>
<value>domain.Account</value>
<value>domain.Info</value>
</list>
</property>
</bean>
<bean
id="AbstractSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
abstract="true">
<!--
<property name="mappingDirectoryLocations">
<list>
<value>classpath*:/cn/ynzc/news/domain/hbm/</value>
<value>classpath*:/cn/ynzc/news/document/domain/hbm/</value>
<value>classpath*:/cn/ynzc/news/guestbook/domain/hbm/</value>
</list>
</property>
-->
<property
name="dataSource"
ref="DataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
</props>
</property>
<property
name="lobHandler"
ref="DefaultLobHandler" />
</bean>
<!-- 连接信息配置 -->
<bean
id="PlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property
name="locations"
value="classpath:jdbc.properties" />
</bean>
<bean
id="DataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property
name="driverClassName"
value="${jdbc.driver}" />
<property
name="username"
value="${jdbc.user}" />
<property
name="password"
value="${jdbc.password}" />
<property
name="url"
value="${jdbc.url}" />
<property
name="initialSize"
value="${jdbc.initialSize}" />
<property
name="maxActive"
value="${jdbc.maxActive}" />
<property
name="maxIdle"
value="${jdbc.maxIdle}" />
<property
name="maxWait"
value="${jdbc.maxWait}" />
<property
name="minIdle"
value="${jdbc.minIdle}" />
</bean>
<bean
id="DefaultLobHandler"
class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="true" />
<bean
id="OracleLobHandler"
class="org.springframework.jdbc.support.lob.OracleLobHandler"
lazy-init="true">
<property name="nativeJdbcExtractor">
<bean
class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
lazy-init="true" />
</property>
</bean>
<bean
id="TransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property
name="sessionFactory"
ref="sessionFactory" />
</bean>
</beans>
jdbc.driver =com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql://127.0.0.1/test
jdbc.user =root
jdbc.password =
jdbc.initialSize =5
jdbc.maxActive =60
jdbc.maxIdle =10
jdbc.maxWait =50
jdbc.minIdle =5
#hibernate
hibernate.dialect =org.hibernate.dialect.MySQLDialect
#hibernate.dialect =org.hibernate.dialect.Oracle9Dialect
hibernate.show_sql =true
hibernate.format_sql =true
#hibernate.hbm2ddl.auto =create-drop
hibernate.hbm2ddl.auto =update
hibernate.cache.use_query_cache =false
hibernate.cache.provider_class =org.hibernate.cache.OSCacheProvider