HibernateDaoSupport类对HibernateTemplate的作用,等同于JdbcDaoSupport类对JdbcTemplate的作用。
HibernateDaoSupport依赖于Spring容器中的sessionFactory对象(Spring整合Hibernate)。JdbcDaoSupport依赖于DataSource连接池(单纯用Spring,未整合Hibernate)。
参考JdbcDaoSupport类(只用Spring,未整合Hibernate):https://blog.csdn.net/houyanhua1/article/details/82052999
UserDaoImpl.java(Dao层,继承HibernateDaoSupport类):
package cn.xxx.dao.impl;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.HibernateCallback; //根据Hibernate对应的版本导包
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import cn.xxx.dao.UserDao;
import cn.xxx.domain.User;
//HibernateDaoSupport依赖于sessionFactory对象
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { //继承HibernateTemplate类
//private HibernateTemplate ht; //HibernateDaoSupport类似JdbcDaoSupport可以省略在Spring中配置HibernateTemplate
@Override
public User getByUserCode(final String usercode) { //匿名内部类中使用的参数,必须final修饰
//HQL HibernateTemplate执行HQL操作数据库
return getHibernateTemplate().execute(new HibernateCallback<User>() {
@Override
public User doInHibernate(Session session) throws HibernateException {
//HQL (这里同样也可以使用原生SQL语句操作数据库)
String hql = "from User where user_code = ? ";
Query query = session.createQuery(hql);
query.setParameter(0, usercode);
User user = (User) query.uniqueResult();
return user;
}
});
//离线Criteria HibernateTemplate通过DetachedCriteria操作数据库
/*
DetachedCriteria dc = DetachedCriteria.forClass(User.class); //离线Criteria
dc.add(Restrictions.eq("user_code", usercode));
List<User> list = (List<User>) getHibernateTemplate().findByCriteria(dc); //查询结果不管是一个还是多个都返回List
if(list != null && list.size()>0){
return list.get(0);
}else{
return null;
}*/
}
}
src/applicationContext.xml(Spring配置文件,配置Dao层并注入sessionFactory对象):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0连接池(连接池中的连接需要配置事务后才能正常提交) -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
<property name="driverClass" value="${jdbc.driverClass}" ></property>
<property name="user" value="${jdbc.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- 核心事务管理器(不同的平台有不同的实现类) -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 将SessionFactory配置到spring容器中(Spring整合Hibernate) -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource" ></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql" >true</prop>
<prop key="hibernate.format_sql" >true</prop>
<prop key="hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>
<!-- 引入orm数据方式一。 -->
<property name="mappingDirectoryLocations" value="classpath:cn/xxx/domain" ></property>
<!-- 引入orm数据方式二。注入hibernate的映射文件(Maven项目,必须使用此方式) -->
<property name="mappingLocations">
<list>
<value>classpath:com/xxx/bos/domain/*.xml</value>
</list>
</property>
</bean>
<!-- Action配置(Spring整合Struts2) -->
<bean name="userAction" class="cn.xxx.web.action.UserAction" scope="prototype" >
<!-- 手动配置注入Action的依赖属性 -->
<property name="userService" ref="userService" ></property>
</bean>
<!-- service层 -->
<bean name="userService" class="cn.xxx.service.impl.UserServiceImpl" >
<property name="ud" ref="userDao" ></property>
</bean>
<!-- dao层 -->
<bean name="userDao" class="cn.xxx.dao.impl.UserDaoImpl" >
<!-- 注入sessionFactory (HibernateDaoSupport依赖于sessionFactory对象) -->
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
</beans>
Test.java(测试类):
package cn.xxx.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.xxx.dao.UserDao;
import cn.xxx.domain.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test {
@Resource(name="userDao")
//注入Spring中的Dao层对象(HibernateDaoSupport)
private UserDao ud;
@Test
//测试Dao HibernateTemplate模板
public void fun3(){
User u = ud.getByUserCode("tom");
System.out.println(u);
}
}