sping整合hibernate之二:dao層開發,spinghibernate
在上一篇日誌中將hibernate的會話工廠sessionFactory注入到了spring的容器中,但這樣還不夠,因為hibernate的增刪改查是要使用事務機制的, 所以還要在spring中配置交易管理,將hibernate管理事物的權利交給spring,這樣,在代碼中就無需手動管理事務了。1.首先在spring中配置一個hibernate的jdbc//applicationContext.xml(spring設定檔) 當然,在此之前要將實體類對應檔配置在sessionFactory<!-- 配置hibernateTemplate 相當於jdbc --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> <!-- 注入sessionFactory --> </bean>
2.配置交易管理 <!-- spring交易管理 --> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean>
<!-- 為增刪改查的方法聲明事務,我這裡把事務放到dao上的,應該放在service比較好 --> <aop:config> <aop:pointcut id="txServices" expression="execution(* com.scitc.ssh.dao..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txServices"/> </aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="create*" propagation="REQUIRED"/> <tx:method name="find*" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
3.注入dao層的類 <!-- 注入dao層需要用hibernateTemplate的類 --> <bean id="BaseDao" class="com.scitc.ssh.dao.BaseDao"> <!-- 為此類注入一個屬性,這樣在類中就可以擷取到這個對象 --> <!-- 功能:HibernateTemplate hibernateTemplate = new HibernateTemplate() --> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean>
4.將事務和資料來源,sessionFactory,jdbc,dao層注入這些弄好了之後,就可以寫dao層了
完整的sping設定檔applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 9 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">11 12 13 <!-- 此對象用來讀取資料庫設定檔jdbc.properties14 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 15 <property name="location"> 16 <value>/WEB-INF/jdbc.properties</value>17 </property> 18 </bean> -->19 20 <!-- 配置資料來源(這裡是使用spring預設的資料來源,後面會換成c3p0) -->21 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">22 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>23 <property name="url" value="jdbc:mysql://localhost:3306/ssh"></property>24 <property name="username" value="root"></property>25 <property name="password" value="123456"></property>26 </bean>27 28 <!-- 配置hibernate的會話工廠sessionFactory-->29 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 30 <property name="dataSource" ref="dataSource"></property> <!-- 資料來源採用上面的 -->31 <property name="mappingResources"> <!-- 自動掃描model包下的實體類 -->32 <list>33 <value>/com/scitc/ssh/model/User.hbm.xml</value> 34 </list>35 </property>36 <property name="hibernateProperties">37 <props>38 <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>39 <!-- <prop key="hibernate.current_session_context_class">thread</prop>-->40 <prop key="hibernate.show_sql">true</prop>41 <prop key="hibernate.format_sql">true</prop>42 <prop key="hibernate.hbm2ddl.auto">update</prop>43 </props>44 </property>45 </bean> 46 47 <!-- 配置hibernateTemplate 相當於jdbc -->48 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">49 <property name="sessionFactory" ref="sessionFactory"></property> <!-- 注入sessionFactory -->50 </bean>51 52 <!-- spring交易管理 -->53 <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 54 <property name="dataSource" ref="dataSource" /> 55 <property name="sessionFactory" ref="sessionFactory" />56 </bean> 57 58 <!-- 開啟spring的註解 -->59 <context:annotation-config /> <!-- 可以不要這個,下面的掃描包可以自動開啟註解 -->60 61 <!-- 自動掃描services層和dao層的註解 -->62 <context:component-scan base-package="com.scitc.ssh.services" />63 <context:component-scan base-package="com.scitc.ssh.services.impl" />64 <context:component-scan base-package="com.scitc.ssh.dao" />65 <context:component-scan base-package="com.scitc.ssh.dao.impl" />66 67 <!-- 聲明事務 -->68 <aop:config>69 <aop:pointcut id="txServices" expression="execution(* com.scitc.ssh.dao..*.*(..))"/>70 <aop:advisor advice-ref="txAdvice" pointcut-ref="txServices"/>71 </aop:config>72 73 <tx:advice id="txAdvice" transaction-manager="transactionManager">74 <tx:attributes>75 <tx:method name="add*" propagation="REQUIRED"/>76 <tx:method name="insert*" propagation="REQUIRED"/>77 <tx:method name="update*" propagation="REQUIRED"/>78 <tx:method name="delete*" propagation="REQUIRED"/>79 <tx:method name="create*" propagation="REQUIRED"/>80 <tx:method name="find*" propagation="REQUIRED"/>81 </tx:attributes>82 </tx:advice>83 84 <!-- 注入dao層需要用hibernateTemplate的類 -->85 <bean id="BaseDao" class="com.scitc.ssh.dao.BaseDao">86 <!-- 為此類注入一個屬性,這樣在類中就可以擷取到這個對象 -->87 <!-- 功能:HibernateTemplate hibernateTemplate = new HibernateTemplate() -->88 <property name="hibernateTemplate" ref="hibernateTemplate"></property> 89 </bean>90 91 </beans>
//資料訪問層通用類basedao.java
1 package com.scitc.ssh.dao; 2 3 4 import java.util.List; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.context.ApplicationContext; 8 import org.springframework.context.support.ClassPathXmlApplicationContext; 9 import org.springframework.orm.hibernate4.HibernateTemplate;10 11 12 import com.scitc.ssh.model.User;13 14 //資料訪問層通用類15 public class BaseDao {16 17 //@Autowired可以自動擷取spring建立的對象18 @Autowired private HibernateTemplate hibernateTemplate; //這裡的屬性名稱一定要和配置中的屬性名稱一致19 20 //返回hibernateTemplate方法 21 public HibernateTemplate getHibernateTemplate(){22 return this.hibernateTemplate; 23 }24 25 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {26 this.hibernateTemplate = hibernateTemplate;27 }28 29 //添加30 public boolean add(Object entity){31 this.hibernateTemplate.save(entity); //hibernateTemplate.save(entity)儲存32 return true;33 }34 35 //刪除36 public boolean delete(Object entity){37 this.hibernateTemplate.delete(entity);38 return true;39 }40 41 //查詢全部42 @SuppressWarnings("unchecked")43 public List<Object> findAll(String queryString){44 return (List<Object>) this.hibernateTemplate.find(queryString);45 46 }47 48 //修改49 public boolean update(Object entity){50 this.hibernateTemplate.update(entity);51 return true;52 53 }54 55 // //按參數查詢56 // public List<Object> findAll(String queryString, Object[] args){57 // List<Object> list = new ArrayList<Object>();58 // this.hibernateTemplate.find(queryString);59 // return list;60 // }61 62 public static void main(String[] args) {63 User user = new User();64 user.setU_id(1);65 user.setU_name("社會你海哥");66 //啟動spring67 ApplicationContext applicationContexts = new ClassPathXmlApplicationContext("applicationContext.xml");68 //擷取Ioc容器中的對象69 BaseDao baseDao = applicationContexts.getBean("BaseDao", BaseDao.class);70 // baseDao.add(user);71 baseDao.update(user);72 // User user2 = (User) baseDao.findAll("from User").get(0);73 // baseDao.delete(user);74 // System.out.println(user2.getU_name());75 76 }77 78 }