BaseDaoHibernateImpl
package com.xy.dao.impl;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BaseDaoHibernateImpl extends HibernateDaoSupport
{
public Object get(final Class entity, final Serializable id)
{
return getHibernateTemplate().get(entity, id);
}
public Object load(final Class entity, final Serializable id)
{
return getHibernateTemplate().load(entity, id);
}
public void delete(final Object entity)
{
getHibernateTemplate().delete(entity);
}
public void save(final Object entity)
{
getHibernateTemplate().save(entity);
}
public void update(final Object entity)
{
getHibernateTemplate().update(entity);
}
public List findByHql(String Hql)
{
return getHibernateTemplate().find(Hql);
}
public void delBySql(String sqlStr)
{
final String sql = sqlStr;
getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session) throws HibernateException, SQLException
{
session.connection().createStatement().executeUpdate(sql);
return null;
}
});
}
}
ClassesDaoImpl
public class ClassesDaoImpl extends BaseDaoHibernateImpl implements IClassDao
{
public List<Classes> GetAllClasses()
{
String hql = "from Classes";
return super.findByHql(hql);
}
public Classes GetModel(int id)
{
return (Classes) super.get(Classes.class, id);
}
}
ManagerDaoImpl
public class ManagerDaoImpl extends BaseDaoHibernateImpl implements IManagerDao
{
public Manager FindManager(String uname, String pwd)
{
Manager m = null;
Session session = null;
Transaction tran = null;
try
{
session = super.getSession();
tran = session.beginTransaction();
String hql = "from Manager m where m.loginName = ? and m.passwd = ?";
Query query = session.createQuery(hql);
query.setString(0, uname);
query.setString(1, pwd);
m = (Manager) query.uniqueResult();
tran.commit();
}
catch (Exception e)
{
e.printStackTrace();
tran.rollback();
}
finally
{
this.getSession().close();
}
return m;
}
}
Spring的bean.xml注入
<!-- 整合hibernate -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/stuinfo" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- DAO的配置,通过Spring注入sessionFactory -->
<bean id="classDao" class="com.xy.dao.impl.ClassesDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="managerDao" class="com.xy.dao.impl.ManagerDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
为什么要注入sessionFactory呢?
继承了org.springframework.orm.hibernate3.support.HibernateDaoSupport,spring在初始化容器的时候会对这个对象作一个check, 看下HibernateDaoSupport源码中的checkDaoConfig()方法 就清楚啦:
protected final void checkDaoConfig()
{
if (this.hibernateTemplate == null)
{
throw new IllegalArgumentException
("'sessionFactory' or 'hibernateTemplate' is required");
}
}