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

1、配置数据源

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

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

使用DBCP数据源配置xml如下

 <!-- dbcp数据源配置 -->
     <bean id="dbcpDataSources" class="org.apache.commons.dbcp2.BasicDataSource">
         <property name="driverClassName" value="${jdbc.driver}" />
         <property name="username" value="${jdbc.user}" />
         <property name="password" value="${jdbc.password}" />
         <property name="url" value="${jdbc.url}" />

         <property name="initialSize" value="${jdbc.initPoolSize}"></property>
         <property name="maxOpenPreparedStatements" value="${jdbc.maxPoolSize}"></property>
     </bean>

使用C3P0数据源配置

 <!-- c3p0数据源配置 -->
     <bean id="c3p0DataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <property name="driverClass" value="${jdbc.driver}" />
         <property name="user" value="${jdbc.user}" />
         <property name="password" value="${jdbc.password}" />
         <property name="jdbcUrl" value="${jdbc.url}" />

         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
     </bean>

2、配置Hibernate的sessionFactory

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

  1. 使用 org.springframework.orm.hibernate3.LocalSessionFactoryBean的方式
 <bean id="sessionFactory"
         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
         p:dataSource-ref="c3p0DataSources">
         <property name="hibernateProperties">
             <props>
                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                 <prop key="hibernate.hbm2ddl.auto">update</prop>
                 <prop key="hibernate.show_sql">true</prop>
             </props>
         </property>
         <!-- 使用通配符 Resources -->
         <property name="mappingLocations">
             <list>
                 <value>classpath:io/shuqi/ssh/spring/transactionalAnnotation/hibernate3/*.hbm.xml</value>
             </list>
         </property>
     </bean>

使用通配符的方式(*.hbm.xml)

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="c3p0DataSources">
          <property name="hibernateProperties">
             <props>
                  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                  <prop key="hibernate.hbm2ddl.auto">update</prop>
                  <prop key="hibernate.show_sql">true</prop>
              </props>
          </property>
          <!-- 使用逐一列出配置文件的方式 -->
          <property name="mappingResources">
              <list><value>io/shuqi/ssh/spring/transactionalAnnotation/hibernate3/User.hbm.xml</value>
             </list>
          </property>

     </bean>

使用逐一列出配置Mapping文件的方式

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

 <bean id="sessionFactory"
         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
         p:dataSource-ref="c3p0DataSources">
         <property name="hibernateProperties">
             <props>
                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                 <prop key="hibernate.hbm2ddl.auto">update</prop>
                 <prop key="hibernate.show_sql">true</prop>
             </props>
         </property>
          <property name="annotatedClasses">
              <list>
                <value>io.shuqi.ssh.spring.transactionalAnnotation.hibernate3.User</value>
              </list>
           </property>
     </bean>

Hibernate3使用注解的反射,并逐一列出类的方式

 <bean id="sessionFactory"
         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
         p:dataSource-ref="c3p0DataSources">
         <property name="hibernateProperties">
             <props>
                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                 <prop key="hibernate.hbm2ddl.auto">update</prop>
                 <prop key="hibernate.show_sql">true</prop>
             </props>
         </property>
         <property name="packagesToScan" value="io.shuqi.ssh.spring.transactionalAnnotation.hibernate3"/>
     </bean>

Hibernate基于注解配置,使用包扫描的方式

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

 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
           p:dataSource-ref="c3p0DataSources">
         <property name="hibernateProperties">
             <props>
                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                 <prop key="hibernate.hbm2ddl.auto">update</prop>
                 <prop key="hibernate.show_sql">true</prop>
             </props>
         </property>
         <property name="packagesToScan" value="io.shuqi.ssh.spring.transactionalAnnotation.hibernate4"/>
     </bean>

Hibernate4基于注解,并使用包扫描的方式

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

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

4、启用Spring的@Transaction注解

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

5、项目的maven依赖

 <properties>
         <spring.version4.1>4.1.6.RELEASE</spring.version4.1>
         <spring.version4>4.0.9.RELEASE</spring.version4>
         <spring.version3>3.2.13.RELEASE</spring.version3>
         <hibernate4.version>4.3.10.Final</hibernate4.version>
         <hibernate3.version>3.6.10.Final</hibernate3.version>
         <!-- 软件版本配置 -->
         <junit.version>4.12</junit.version>
         <jackjson.version>2.5.4</jackjson.version>
         <spring.version>4.1.6.RELEASE</spring.version>
         <jstl.version>1.2</jstl.version>
         <el.version>1.1.2</el.version>
         <c3p0.version>0.9.5</c3p0.version>
         <dbcp.version>2.1</dbcp.version>
         <mysql.version>5.1.35</mysql.version>
         <hibernate.version>3.6.10.Final</hibernate.version>
         <javassist.version>3.13.0-GA</javassist.version>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>

     <dependencies>
         <!-- 测试时依赖包 -->
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>${junit.version}</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-test</artifactId>
             <version>${spring.version4.1}</version>
             <scope>test</scope>
         </dependency>
         <!-- Spring 核心包 -->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-core</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-beans</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context-support</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-expression</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-aop</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-jdbc</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-orm</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-tx</artifactId>
             <version>${spring.version}</version>
         </dependency>

         <!-- c3p0数据源 -->
         <dependency>
             <groupId>com.mchange</groupId>
             <artifactId>c3p0</artifactId>
             <version>${c3p0.version}</version>
         </dependency>
         <!-- dbcp数据源 -->
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-dbcp2</artifactId>
             <version>${dbcp.version}</version>
         </dependency>

         <!-- mysql 驱动 -->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>${mysql.version}</version>
         </dependency>

         <!-- hibernate -->
         <dependency>
             <groupId>org.hibernate</groupId>
             <artifactId>hibernate-core</artifactId>
             <version>${hibernate.version}</version>
         </dependency>

         <!-- hibernate3.6.10依赖 -->
         <dependency>
             <groupId>org.javassist</groupId>
             <artifactId>javassist</artifactId>
             <version>${javassist.version}</version>
         </dependency>

     </dependencies>

maven依赖

上一篇:ExtendsField


下一篇:Keras之Mask R-CNN:《极限挑战》第四季第2期助力高考—使用Mask R-CNN代替Photoshop抠图、颜色填充框出目标检测