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

每次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.propertiesvalue>
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.beanvalue>
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     
14     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
15       <property name="locations" value="classpath:jdbc.properties"/>
16     bean>
17      
18     
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     
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.xmlvalue>
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     

你可能感兴趣的:(spring)