spring整合hibernate(注解、xml)applicationContext.xml配置

来源:互联网 发布:linux 映射内网端口 编辑:程序博客网 时间:2024/05/29 15:53

每次ssh框架整合都很麻烦,今天有空余时间总结下(主要总结spring+hibernate):

1.注解方式整合:

    applicationContext.xml配置文件:

01<?xml version="1.0" encoding="UTF-8"?>
02<beans xmlns="http://www.springframework.org/schema/beans"
03     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04     xmlns:aop="http://www.springframework.org/schema/aop"
05     xmlns:tx="http://www.springframework.org/schema/tx"
06     xmlns:context="http://www.springframework.org/schema/context"
07     xsi:schemaLocation="
08         http://www.springframework.org/schema/beans
09         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/tx
11         http://www.springframework.org/schema/tx/spring-tx.xsd
12         http://www.springframework.org/schema/aop
13         http://www.springframework.org/schema/aop/spring-aop.xsd
14         http://www.springframework.org/schema/context
15         http://www.springframework.org/schema/context/spring-context.xsd">
16    <context:component-scan base-package="com.test" />
17    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
18      <property name="locations">
19        <list>
20            <value>classpath:jdbc.properties</value>
21        </list>
22      </property>
23    </bean>
24    <bean id="c3p0DataSource" destroy-method="close"
25        class="com.mchange.v2.c3p0.ComboPooledDataSource">
26        <property name="driverClass" value="${driverClass}" />
27        <property name="jdbcUrl" value="${url}" />
28        <property name="user" value="${user}" />
29        <property name="password" value="${password}" />
30        <property name="initialPoolSize" value="${initialPoolSize}" />
31        <property name="minPoolSize" value="${minPoolSize}" />
32        <property name="maxPoolSize" value="${maxPoolSize}" />
33        <property name="maxIdleTime" value="${maxIdleTime}" />
34    </bean>                
35    <bean id="sessionFactory"
36        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
37        <property name="dataSource" ref="c3p0DataSource" />
38        <property name="packagesToScan">
39            <list>
40                <value>com.test.bean</value>
41            </list>
42        </property>
43        <property name="hibernateProperties">
44            <props>
45                <prop key="hibernate.dialect">${dialect}</prop>
46                <prop key="hibernate.show_sql">${show_sql}</prop>
47                <prop key="hibernate.format_sql">${format_sql}</prop>
48                <prop key="hibernate.use_sql_commants">${use_sql_comments}</prop>
49                <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop>
50            </props>
51        </property>
52    </bean>
53    <bean id="txManager"
54        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
55        <property name="sessionFactory" ref="sessionFactory" />
56    </bean>
57    <tx:advice id="txAdvice" transaction-manager="txManager">
58        <tx:attributes>
59            <tx:method name="get*" read-only="true" />
60            <tx:method name="*" />
61        </tx:attributes>
62    </tx:advice>
63    <aop:config>
64        <aop:pointcut id="bizMethods" expression="execution(* com.test.biz.*.*(..))" />
65        <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />
66    </aop:config>
67</beans>

2.xml方式实现

    applicationContext.xml配置:

01<?xml version="1.0" encoding="UTF-8"?>
02<beans xmlns="http://www.springframework.org/schema/beans"
03  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04  xmlns:aop="http://www.springframework.org/schema/aop"
05  xmlns:tx="http://www.springframework.org/schema/tx"
06  xsi:schemaLocation="http://www.springframework.org/schema/beans
07                      http://www.springframework.org/schema/beans/spring-beans.xsd
08                      http://www.springframework.org/schema/tx
09                      http://www.springframework.org/schema/tx/spring-tx.xsd
10                      http://www.springframework.org/schema/aop
11                      http://www.springframework.org/schema/aop/spring-aop.xsd">
12          
13    <!-- 讓spring 去读取指定路径下的资源文件 -->
14    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
15      <property name="locations" value="classpath:jdbc.properties"/>
16    </bean>
17     
18    <!-- 配置c3p0连接池 -->
19    <bean id="c3p0Source" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
20      <property name="driverClass" value="${driverClass}" />
21      <property name="jdbcUrl" value="${url}" />
22      <property name="user" value="${user}" />
23      <property name="password" value="${password}" />
24      <property name="initialPoolSize" value="${initialPoolSize}" />
25      <property name="minPoolSize" value="${minPoolSize}" />
26      <property name="maxPoolSize" value="${maxPoolSize}" />
27      <property name="maxIdleTime" value="${maxIdleTime}" />
28    </bean>
29     
30    <!-- 配置SessionFactory -->
31    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
32      <property name="dataSource" ref="c3p0Source" />
33      <property name="mappingResources">
34          <list>
35            <value>/com/cdzg/spring/bean/User.hbm.xml</value>
36          </list>
37      </property>
38      <property name="hibernateProperties">
39        <props>
40                <prop key="hibernate.dialect">${dialect}</prop>
41                <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop>
42                <prop key="hibernate.show_sql">${show_sql}</prop>
43                <prop key="hibernate.format_sql">${format_sql}</prop>
44                <prop key="hibernate.use_sql_comments">${use_sql_comments}</prop>
45            </props>
46      </property>
47    </bean>
48     
49    <!-- 配置事务管理器 -->
50    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
51      <property name="sessionFactory" ref="sessionFactory" />
52    </bean>
53     
54    <!-- 定义事务通知 -->
55    <tx:advice id="txAdvice" transaction-manager="txManager">
56      <tx:attributes>
57        <tx:method name="get*" read-only="true"/>
58        <tx:method name="*"/>
59      </tx:attributes>
60    </tx:advice>
61      
62     <!-- 定义事务切面,并应用事务通知 -->   
63     <aop:config>
64      <aop:pointcut id="xxxBizImpl" expression="execution(* com.cdzg.spring.biz.*.*(..))"/>
65      <aop:advisor pointcut-ref="xxxBizImpl" advice-ref="txAdvice"/>
66     </aop:config>
67          
68    <bean id="userDaoImpl" class="com.cdzg.spring.dao.impl.UserDaoImpl">
69        <property name="sessionFactory" ref="sessionFactory" />
70    </bean>
71    <bean id="userBizImpl" class="com.cdzg.spring.biz.impl.UserBizImpl">
72        <property name="userDao" ref="userDaoImpl" />
73    </bean>
74    <bean id="userAction" class="com.cdzg.spring.web.actions.UserAction">
75        <property name="userBiz" ref="userBizImpl" />
76    </bean>
77</beans>

注解区别:

 

 

 

 

0 0