Spring和Hibernate整合xml配置

1、基于xml的配置

dbconfig.properties数据源文件配置

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/databaseName
user=root
password=root
initialPoolSize=6
minPoolSize=5
maxPoolSize=300
maxIdleTime=60
acquireIncrement=5
idleConnectionTestPeriod=60

基于xml的beans.xml 配置文件 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        ">


	<!-- 引入properties属性文件 -->
	<context:property-placeholder location="classpath:dbconfig.properties" />

	<!-- 配置数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${driverClass}" />
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>
		<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="${initialPoolSize}" />
		<!--连接池中保留的最小连接数。 -->
		<property name="minPoolSize" value="${minPoolSize}" />
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="${maxPoolSize}" />
		<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="${maxIdleTime}" />
		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="${acquireIncrement}" />
		<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
		<property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" />
	</bean>


	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="mappingLocations" value="com/hsj/domain/*.hbm.xml"></property>
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.hbm2ddl.auto=update
				hibernate.show_sql=true 
				hibernate.cache.use_second_level_cache=true
				hibernate.cache.provider_class=org.hibernate.cache.OSCacheProvider
			</value>
		</property>
	</bean>
	
	<!-- 定义事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 使用xml配置事务 -->
	<aop:config>
		<aop:pointcut expression=" execution(* com.hsj.dao.bean.*.*(..))" id="myPointcut"/>
		<aop:advisor advice-ref="txAdvice"  pointcut-ref="myPointcut"/>
	</aop:config>
	
	<!-- 配置通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			
			<tx:method name="get*" propagation="NEVER" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	

	<bean id="personDao" class="com.hsj.dao.bean.PersonDaoBean">
		<property name="sessionFactory" ref="sessionFactory"></property>
		<!-- <property name="studentDao"  ref="studentDao"/> -->
	</bean>
	
	<bean id="studentDao" class="com.hsj.dao.bean.StudentDaoBean">
		<property name="sessionFactory" ref="sessionFactory"></property>
		<property name="personDao" ref="personDao"></property>
	</bean>
	
</beans>

实体类配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hsj.domain">
	<class name="Person" table="t_person">
		<id name="id">
			<!-- 主键的生成策略 -->
			<generator class="native"></generator>
		</id>
		<property name="name" length="30" unique="true"></property>
		<property name="sex" length="6"></property>
		<property name="age"></property>
		<property name="birthday" type="date"></property>
	</class>
</hibernate-mapping> 

2、基于注解的配置

基于注解配置beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        ">

	<!-- 打开扫描器开关,并指定扫描范围 -->
	<context:component-scan base-package="com.hsj"></context:component-scan>

	<!-- 引入properties属性文件 -->
	<context:property-placeholder location="classpath:dbconfig.properties" />

	<!-- 配置数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${driverClass}" />
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>
		<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="${initialPoolSize}" />
		<!--连接池中保留的最小连接数。 -->
		<property name="minPoolSize" value="${minPoolSize}" />
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="${maxPoolSize}" />
		<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="${maxIdleTime}" />
		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="${acquireIncrement}" />
		<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
		<property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" />
	</bean>


	<!-- 使用注解配置sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 指定扫描实体范围的包,包中的类都可以被扫描到 -->
		<property name="packagesToScan" value="com.hsj.domain"></property>
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.hbm2ddl.auto=update
				hibernate.show_sql=true 
				hibernate.cache.use_second_level_cache=true
				hibernate.cache.provider_class=org.hibernate.cache.OSCacheProvider
			</value>
		</property>
	</bean>
	
	<!-- 定义事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	<!-- 采用注释的方式进行事务控制 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

所需jar包

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值