码迷,mamicode.com
首页 > 编程语言 > 详细

spring与hibernate整合配置基于Annotation注解方式管理实务

时间:2015-06-24 00:33:45      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

 1、配置数据源

数据库连接基本信息存放到properties文件中,因此先加载properties文件

1 <!-- jdbc连接信息 -->
2     <context:property-placeholder
3         location="classpath:io/shuqi/ssh/spring/transactionalAnnotation/jdbc.properties"/>

使用DBCP数据源配置xml如下

技术分享
 1 <!-- dbcp数据源配置 -->
 2     <bean id="dbcpDataSources" class="org.apache.commons.dbcp2.BasicDataSource">
 3         <property name="driverClassName" value="${jdbc.driver}" />
 4         <property name="username" value="${jdbc.user}" />
 5         <property name="password" value="${jdbc.password}" />
 6         <property name="url" value="${jdbc.url}" />
 7 
 8         <property name="initialSize" value="${jdbc.initPoolSize}"></property>
 9         <property name="maxOpenPreparedStatements" value="${jdbc.maxPoolSize}"></property>
10     </bean>
View Code

使用C3P0数据源配置

技术分享
 1 <!-- c3p0数据源配置 -->
 2     <bean id="c3p0DataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 3         <property name="driverClass" value="${jdbc.driver}" />
 4         <property name="user" value="${jdbc.user}" />
 5         <property name="password" value="${jdbc.password}" />
 6         <property name="jdbcUrl" value="${jdbc.url}" />
 7 
 8         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
 9         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
10     </bean>
View Code

2、配置Hibernate的sessionFactory

hibernate3的sessionFactory有两种方式配置,分别是org.springframework.orm.hibernate3.LocalSessionFactoryBean基于*.hbm.xmlorg.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 基于 JPA注解方式的配置

  1. 使用 org.springframework.orm.hibernate3.LocalSessionFactoryBean的方式
技术分享
 1 <bean id="sessionFactory"
 2         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
 3         p:dataSource-ref="c3p0DataSources">
 4         <property name="hibernateProperties">
 5             <props>
 6                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 7                 <prop key="hibernate.hbm2ddl.auto">update</prop>
 8                 <prop key="hibernate.show_sql">true</prop>
 9             </props>
10         </property>
11         <!-- 使用通配符 Resources -->
12         <property name="mappingLocations">
13             <list>
14                 <value>classpath:io/shuqi/ssh/spring/transactionalAnnotation/hibernate3/*.hbm.xml</value>
15             </list>
16         </property>
17     </bean>
使用通配符的方式(*.hbm.xml)
技术分享
 1  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="c3p0DataSources">
 2          <property name="hibernateProperties">
 3             <props>
 4                  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 5                  <prop key="hibernate.hbm2ddl.auto">update</prop>
 6                  <prop key="hibernate.show_sql">true</prop>
 7              </props>
 8          </property>
 9          <!-- 使用逐一列出配置文件的方式 -->    
10          <property name="mappingResources">
11              <list><value>io/shuqi/ssh/spring/transactionalAnnotation/hibernate3/User.hbm.xml</value>
12             </list>
13          </property>
14         
15     </bean>
使用逐一列出配置Mapping文件的方式

      2.使用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean的方式

技术分享
 1 <bean id="sessionFactory"
 2         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
 3         p:dataSource-ref="c3p0DataSources">
 4         <property name="hibernateProperties">
 5             <props>
 6                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 7                 <prop key="hibernate.hbm2ddl.auto">update</prop>
 8                 <prop key="hibernate.show_sql">true</prop>
 9             </props>
10         </property>
11          <property name="annotatedClasses">  
12              <list>  
13                <value>io.shuqi.ssh.spring.transactionalAnnotation.hibernate3.User</value>  
14              </list>  
15           </property>
16     </bean>
Hibernate3使用注解的反射,并逐一列出类的方式
技术分享
 1 <bean id="sessionFactory"
 2         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
 3         p:dataSource-ref="c3p0DataSources">
 4         <property name="hibernateProperties">
 5             <props>
 6                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 7                 <prop key="hibernate.hbm2ddl.auto">update</prop>
 8                 <prop key="hibernate.show_sql">true</prop>
 9             </props>
10         </property>
11         <property name="packagesToScan" value="io.shuqi.ssh.spring.transactionalAnnotation.hibernate3"/>
12     </bean>
Hibernate基于注解配置,使用包扫描的方式

Hibernate4的sessionFactory与Hibernate3的配置差不多,只是Hibernate3的两个类在Hibernate4变为了一个类(org.springframework.orm.hibernate4.LocalSessionFactoryBean

技术分享
 1 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
 2           p:dataSource-ref="c3p0DataSources">
 3         <property name="hibernateProperties">
 4             <props>
 5                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 6                 <prop key="hibernate.hbm2ddl.auto">update</prop>
 7                 <prop key="hibernate.show_sql">true</prop>
 8             </props>
 9         </property>
10         <property name="packagesToScan" value="io.shuqi.ssh.spring.transactionalAnnotation.hibernate4"/>
11     </bean>
Hibernate4基于注解,并使用包扫描的方式

3、配置Spring的事务管理,将程序的事务管理交由Spring管理

1 <!-- 将事务交由Spring管理 -->
2     <bean id="transactionManager" 
3           class="org.springframework.orm.hibernate4.HibernateTransactionManager"
4           p:sessionFactory-ref="sessionFactory"/>

4、启用Spring的@Transaction注解

1 <!-- 开启spring注解驱动,及@Transaction注解的开启 -->
2     <tx:annotation-driven transaction-manager="transactionManager"/>

5、项目的maven依赖

技术分享
  1 <properties>
  2         <spring.version4.1>4.1.6.RELEASE</spring.version4.1>
  3         <spring.version4>4.0.9.RELEASE</spring.version4>
  4         <spring.version3>3.2.13.RELEASE</spring.version3>
  5         <hibernate4.version>4.3.10.Final</hibernate4.version>
  6         <hibernate3.version>3.6.10.Final</hibernate3.version>
  7         <!-- 软件版本配置 -->
  8         <junit.version>4.12</junit.version>
  9         <jackjson.version>2.5.4</jackjson.version>
 10         <spring.version>4.1.6.RELEASE</spring.version>
 11         <jstl.version>1.2</jstl.version>
 12         <el.version>1.1.2</el.version>
 13         <c3p0.version>0.9.5</c3p0.version>
 14         <dbcp.version>2.1</dbcp.version>
 15         <mysql.version>5.1.35</mysql.version>
 16         <hibernate.version>3.6.10.Final</hibernate.version>
 17         <javassist.version>3.13.0-GA</javassist.version>
 18         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 19     </properties>
 20 
 21     <dependencies>
 22         <!-- 测试时依赖包 -->
 23         <dependency>
 24             <groupId>junit</groupId>
 25             <artifactId>junit</artifactId>
 26             <version>${junit.version}</version>
 27             <scope>test</scope>
 28         </dependency>
 29         <dependency>
 30             <groupId>org.springframework</groupId>
 31             <artifactId>spring-test</artifactId>
 32             <version>${spring.version4.1}</version>
 33             <scope>test</scope>
 34         </dependency>
 35         <!-- Spring 核心包 -->
 36         <dependency>
 37             <groupId>org.springframework</groupId>
 38             <artifactId>spring-core</artifactId>
 39             <version>${spring.version}</version>
 40         </dependency>
 41         <dependency>
 42             <groupId>org.springframework</groupId>
 43             <artifactId>spring-beans</artifactId>
 44             <version>${spring.version}</version>
 45         </dependency>
 46         <dependency>
 47             <groupId>org.springframework</groupId>
 48             <artifactId>spring-context</artifactId>
 49             <version>${spring.version}</version>
 50         </dependency>
 51         <dependency>
 52             <groupId>org.springframework</groupId>
 53             <artifactId>spring-context-support</artifactId>
 54             <version>${spring.version}</version>
 55         </dependency>
 56         <dependency>
 57             <groupId>org.springframework</groupId>
 58             <artifactId>spring-expression</artifactId>
 59             <version>${spring.version}</version>
 60         </dependency>
 61         <dependency>
 62             <groupId>org.springframework</groupId>
 63             <artifactId>spring-aop</artifactId>
 64             <version>${spring.version}</version>
 65         </dependency>
 66         <dependency>
 67             <groupId>org.springframework</groupId>
 68             <artifactId>spring-jdbc</artifactId>
 69             <version>${spring.version}</version>
 70         </dependency>
 71         <dependency>
 72             <groupId>org.springframework</groupId>
 73             <artifactId>spring-orm</artifactId>
 74             <version>${spring.version}</version>
 75         </dependency>
 76         <dependency>
 77             <groupId>org.springframework</groupId>
 78             <artifactId>spring-tx</artifactId>
 79             <version>${spring.version}</version>
 80         </dependency>
 81 
 82         <!-- c3p0数据源 -->
 83         <dependency>
 84             <groupId>com.mchange</groupId>
 85             <artifactId>c3p0</artifactId>
 86             <version>${c3p0.version}</version>
 87         </dependency>
 88         <!-- dbcp数据源 -->
 89         <dependency>
 90             <groupId>org.apache.commons</groupId>
 91             <artifactId>commons-dbcp2</artifactId>
 92             <version>${dbcp.version}</version>
 93         </dependency>
 94 
 95         <!-- mysql 驱动 -->
 96         <dependency>
 97             <groupId>mysql</groupId>
 98             <artifactId>mysql-connector-java</artifactId>
 99             <version>${mysql.version}</version>
100         </dependency>
101 
102         <!-- hibernate -->
103         <dependency>
104             <groupId>org.hibernate</groupId>
105             <artifactId>hibernate-core</artifactId>
106             <version>${hibernate.version}</version>
107         </dependency>
108 
109         <!-- hibernate3.6.10依赖 -->
110         <dependency>
111             <groupId>org.javassist</groupId>
112             <artifactId>javassist</artifactId>
113             <version>${javassist.version}</version>
114         </dependency>
115 
116     </dependencies>
maven依赖

 

spring与hibernate整合配置基于Annotation注解方式管理实务

标签:

原文地址:http://www.cnblogs.com/shuqi/p/4596571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!