目錄:java
public void save(User user){ //不使用spring的原始代碼 Configuration config = new Configuration(); config.configure(); SessionFactory sf = config.buildSessionFactory(); Session session = sf.getCurrentSession(); session.beginTransaction(); session.save(user); session.getTransaction().commit(); }
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean>
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 示例化鏈接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/javaweb"></property> <property name="username" value="root"></property> <property name="password" value=""></property> <property name="initialSize" value="6"></property> <property name="maxActive" value="2"></property> </bean> <!-- spring建立SessionFactory對象 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingLocations"> <list> <value>classpath:claire/entity/User.hbm.xml</value> </list> </property> </bean> <bean id="userDao" class="claire.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userService" class="claire.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> <!-- spring增長對事務支持 --> <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!-- 配置切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* claire..*Service.*(..))"/> </aop:config> </beans>
spring整合後的代碼:spring
package claire.dao; import org.hibernate.SessionFactory; import claire.entity.User; public class UserDao { //接收spring 的Session對象 private SessionFactory sessionFactory; public void save(User user){ sessionFactory.getCurrentSession().save(user); } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
直接在dao中使用sessionFactory對象操做數據庫sql
public void save(User user){ sessionFactory.getCurrentSession().save(user); }
HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory); hibernateTemplate.save(user);
)