每次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" |
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"> |
20 | <value>classpath:jdbc.properties</value> |
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}" /> |
35 | <bean id="sessionFactory" |
36 | class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> |
37 | <property name="dataSource" ref="c3p0DataSource" /> |
38 | <property name="packagesToScan"> |
40 | <value>com.test.bean</value> |
43 | <property name="hibernateProperties"> |
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> |
54 | class="org.springframework.orm.hibernate4.HibernateTransactionManager"> |
55 | <property name="sessionFactory" ref="sessionFactory" /> |
57 | <tx:advice id="txAdvice" transaction-manager="txManager"> |
59 | <tx:method name="get*" read-only="true" /> |
60 | <tx:method name="*" /> |
64 | <aop:pointcut id="bizMethods" expression="execution(* com.test.biz.*.*(..))" /> |
65 | <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" /> |
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"> |
14 | <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> |
15 | <property name="locations" value="classpath:jdbc.properties"/> |
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}" /> |
31 | <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> |
32 | <property name="dataSource" ref="c3p0Source" /> |
33 | <property name="mappingResources"> |
35 | <value>/com/cdzg/spring/bean/User.hbm.xml</value> |
38 | <property name="hibernateProperties"> |
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> |
50 | <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> |
51 | <property name="sessionFactory" ref="sessionFactory" /> |
55 | <tx:advice id="txAdvice" transaction-manager="txManager"> |
57 | <tx:method name="get*" read-only="true"/> |
64 | <aop:pointcut id="xxxBizImpl" expression="execution(* com.cdzg.spring.biz.*.*(..))"/> |
65 | <aop:advisor pointcut-ref="xxxBizImpl" advice-ref="txAdvice"/> |
68 | <bean id="userDaoImpl" class="com.cdzg.spring.dao.impl.UserDaoImpl"> |
69 | <property name="sessionFactory" ref="sessionFactory" /> |
71 | <bean id="userBizImpl" class="com.cdzg.spring.biz.impl.UserBizImpl"> |
72 | <property name="userDao" ref="userDaoImpl" /> |
74 | <bean id="userAction" class="com.cdzg.spring.web.actions.UserAction"> |
75 | <property name="userBiz" ref="userBizImpl" /> |
注解区别: