一、疑惑
一直以来,我在使用SSH框架的时候经常会发现后者有疑虑到底使用hibernate的那种方法或者如何配置hibernate来操作数据库,经过
一段时间的学习下面我来总结一下,常用的dao层配置。
二、常用的hibernate操作dao
- 第一种,通过继承HibernateDaoSupport来操作
- 第二种,通过HibernateTemplate来操作
- 第三种,通过使用Hibernate的session来操作
- 第四种,直接写JDBC来实现数据库操作
三、四种常用方法介绍及配置
- 通过继承HibernateDaoSupport来操作
spring为Hibernate的Dao提供的工具类,其底层是通过HibernateTemplate来实现来数据库的操作,但我觉得使用它的时候需要向每个
Dao层注入sessionFactory感觉有点不方便,因为这样注解就不方便了,但使用的时候就不需要在Dao层里面写SessionFactory的set方法了
直接在配置文件中进行配置就可以了。可以看源码发现:
使用的配置:applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" ???xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ???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.xsd ???http://www.springframework.org/schema/context ???http://www.springframework.org/schema/context/spring-context.xsd ???http://www.springframework.org/schema/aop ???http://www.springframework.org/schema/aop/spring-aop.xsd ???http://www.springframework.org/schema/tx ????http://www.springframework.org/schema/tx/spring-tx.xsd"> ???<!-- 配置连接池: --> ???<!-- 引入外部属性文件 --> ???<context:property-placeholder location="classpath:c3p0-db.properties"/> ???<!-- 配置c3p0数据源 --> ???<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> ???????<property name="driverClass" value="${jdbc.driverClass}"></property> ???????<property name="jdbcUrl" value="${jdbc.url}"></property> ???????<property name="user" value="${jdbc.user}"></property> ???????<property name="password" value="${jdbc.password}"></property> ???</bean> ???????<!-- 配置hibernate的相关信息 --> ???<!-- 配置SessionFactory --> ???<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> ???????<!--配置数据源 注入连接池--> ???????<property name="dataSource" ref="dataSource"></property> ???????<!-- 配置hibernate的其他属性 --> ???????<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> ???????????????<prop key="hibernate.connection.autocommit">false</prop> ???????????</props> ???????</property> ???????<!-- 配置hibernate的映射文件 --> ???????<property name="mappingResources"> ???????????<list> ???????????????<value>com/itwang/entity/User.hbm.xml</value> ???????????????<value>com/itwang/entity/Category.hbm.xml</value> ???????????</list> ???????</property> ???</bean> ???????<!-- 事务管理 --> ???<!-- 配置一个事务管理器 --> ???<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> ???????<property name="sessionFactory" ref="sessionFactory"></property> ???</bean> ???<!-- 开启注解事务 ?--> ???<tx:annotation-driven transaction-manager="transactionManager"/> ???<!-- Dao的配置 =============================--> ???<!-- 用户的Dao --> ???<bean id="userDao" class="com.itwang.dao.UserDao"> ???????<property name="sessionFactory" ref="sessionFactory"></property> ???</bean> ???????<!-- 一级分类的Dao --> ???<bean id="categoryDao" class="com.itwang.dao.CategoryDao"> ???????<property name="sessionFactory" ref="sessionFactory"></property> ???</bean> ???????<!-- 二级分类的Dao --> ???<bean id="categorySecondDao" class="com.itwang.dao.CategorySecondDao"> ???????<property name="sessionFactory" ref="sessionFactory"></property> ???</bean> ???????<!--商品的Dao ?--> ???<bean id="productDao" class="com.itwang.dao.ProductDao"> ???????<property name="sessionFactory" ref="sessionFactory"></property> ???</bean></beans>
使用直接继承,方法里面通过this.getHibernateTemplate()直接使用HibernateTemplate操作数据库
可以看到这种方法有点繁琐
2. 通过HibernateTemplate来操作(推荐)
当然在配置文件中也可以只需要配置hibernateTemplate对象就可以了,因为可以在dao层直接使用注解方式(@Autowired,@Repository)来注入属性和创建Bean
@Repositorypublic class UserDaoImpl implements UserDao { ???//得到hibernateTemplate对象 ???@Autowired ???private HibernateTemplate hibernateTemplate; ???@Override ???public void add() { ???????User user = new User(); ???????user.setAddress("japan"); ???????user.setUsername("rose");; ???????hibernateTemplate.save(user); ???}
}
这种方式是不是很方便,只需要在配置文件中配置一个HibernateTemplate对象就可以了。
3. 通过使用Hibernate的session来操作
这一种是Hibernate的最基础的方式,也是最灵活的一种方式,以为Session实现了hibernate的所有数据库操作方法,所谓越原始越灵活。
这种方式只需要在Dao层注入SessionFactory对象即可
???<!-- 配置SessionFactory --> ???<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> ???????<!--配置数据源 注入连接池--> ???????<property name="dataSource" ref="dataSource"></property> ???????<!-- 配置hibernate的其他属性 --> ???????<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> ???????????????<prop key="hibernate.connection.autocommit">false</prop> ???????????</props> ???????</property> ???????<!-- 配置hibernate的映射文件 --> ???????<property name="mappingResources"> ???????????<list> ???????????????<value>com/itwang/entity/User.hbm.xml</value> ???????????????<value>com/itwang/entity/Category.hbm.xml</value> ???????????</list> ???????</property> ???</bean>
@Repositorypublic class UserDaoImpl implements UserDao { ???@Autowired ???private SessionFactory sessionFactory; ???@Override ???public void saveUser() { ???????Session session = null; ???????Transaction tx = null; ???????try { ???????????session = sessionFactory.getCurrentSession(); ???????????//开启事务 ???????????tx = session.beginTransaction(); ???????????//添加 ???????????User user = new User(); ???????????user.setAddress("American"); ???????????user.setUsername("老王"); ???????????session.save(user); ???????????tx.commit(); ???????}catch(Exception e) { ???????????e.printStackTrace(); ???????????//回滚事务 ???????????tx.rollback(); ???????}finally { ???????????session.close(); ???????} ???}}
4. 直接写JDBC来实现数据库操作,这种方式我就不多说了,以为这种方式太原始了,不推荐
SSH框架整合中Hibernate实现Dao层常用结构
原文地址:https://www.cnblogs.com/ya-qiang/p/9335844.html