欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring4配置hibernate3

程序员文章站 2022-04-07 14:33:35
...

0. 前言

本文章所有代码已经上传,可以免费下载了解。

本项目启动的是会自动创建表结构,只需要看客们提供数据库支持。

本文为作者研究时所写,如有错误望指教。

 

1. 准备

jdk:1.7

eclipse:4.4

maven:3.1.1

spring:4.1.6

hibernate:3.6.10

 

2. 创建一个maven项目,并配置pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.xkx</groupId>
	<artifactId>spring4-hibernate3-demo</artifactId>
	<packaging>jar</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring4-hibernate3-demo</name>
	<url>http://maven.apache.org</url>
	<dependencies>
<!-- 		oracle jdbc -->
		<dependency>
            <groupId>oracle</groupId>
            <artifactId>oracle-ojdbc6</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/ojdbc6.jar</systemPath>
        </dependency>
        
<!--         hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>3.6.10.Final</version>
		</dependency>

<!-- 		dbcp连接池 -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>

<!-- 		spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>

<!-- 		测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>spring4-hibernate3-demo</finalName>
	</build>
</project>

3. 编写spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
    <!-- 扫描有注解的文件  base-package 包路径 -->
    <context:component-scan base-package="org.xkx"/>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
		<property name="username" value="D_C_XKX" />
		<property name="password" value="D_C_XKX" />
		<property name="initialSize" value="10" />
	</bean>

<!-- 	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<value>
				<!-- 设置数据库方言 -->
				hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
				<!-- 是否在控制台显示sql -->
				hibernate.show_sql=true
				<!-- 是否格式化sql,优化显示 -->
				hibernate.format_sql=true
				<!-- 是否开启二级缓存 -->
				hibernate.cache.use_second_level_cache=false
				<!-- 是否开启查询缓存 -->
				hibernate.cache.use_query_cache=false
				<!-- 数据库批量查询最大数 -->
				hibernate.jdbc.fetch_size=50
				<!-- 数据库批量更新、添加、删除操作最大数 -->
				hibernate.jdbc.batch_size=50
				<!-- 系统启动时是否对数据库的表进行重建、更新等操作 -->
				hibernate.hbm2ddl.auto=update
			</value>
		</property>
		
		<property name="packagesToScan">
			<value>org.xkx</value>
		</property>
	</bean>

	<!-- 定义事务管理 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
        	<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
			<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
            <tx:method name="create" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="grant*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
	
	<aop:config>
        <aop:pointcut id="productServiceMethods" expression="execution(* org.xkx.*..*.*ServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
    </aop:config>

</beans> 

 4. JavaBean和service类请参见附件

 

 5. 编写测试类

    	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring/srping-ctx.xml");
    	UseAnnotationEntityService useAnnotationEntityService = applicationContext.getBean("useAnnotationEntityServiceImpl", UseAnnotationEntityService.class);
    	for (int i = 0; i < 2; i++) {
    		UseAnnotationEntity useAnnotationEntity = new UseAnnotationEntity();
    		useAnnotationEntity.setName("姓名" + i);
    		useAnnotationEntity.setAge(i);
    		useAnnotationEntity.setCreateTime(new Date());
    		useAnnotationEntityService.create(useAnnotationEntity);
    		System.out.println("a");
    	}

 6. 执行测试代码


spring4配置hibernate3
            
    
    博客分类: springhibernate springhibernatemavenspring4hibernate3 
 

 

  • spring4配置hibernate3
            
    
    博客分类: springhibernate springhibernatemavenspring4hibernate3 
  • 大小: 662.6 KB