javaEE SSH三大框架整合,Spring整合Hibernate,Spring事务。(Hibernate中不用配置事务)

博客围绕Spring整合Hibernate展开,包含Spring配置文件src/applicationContext.xml,在Service层的UserServiceImpl.java中通过注解配置事务,还有用于测试的Test.java,涉及Java、JavaEE、Spring事务等信息技术内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

src/applicationContext.xml(Spring配置文件):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns="http://www.springframework.org/schema/beans" 
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:aop="http://www.springframework.org/schema/aop" 
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
							http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
							http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
							http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	
	<!-- 读取db.properties文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 配置c3p0连接池(连接池中的连接需要配置事务后才能正常提交) -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
		<property name="driverClass" value="${jdbc.driverClass}" ></property>
		<property name="user" value="${jdbc.user}" ></property>
		<property name="password" value="${jdbc.password}" ></property>
	</bean>
	
	<!-- 核心事务管理器(不同的平台有不同的实现类) -->
	<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
		<property name="sessionFactory" ref="sessionFactory" ></property>
	</bean>
	
	<!-- 事务配置方式1:通过XML方式配置 -->
	<!-- 配置事务通知 -->
	<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager" >
		<tx:attributes>
			<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
			<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
			<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
			<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
			<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
			<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
			<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
			<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
		</tx:attributes>
	</tx:advice> -->
	<!-- 配置将通知织入目标对象。  配置切点、配置切面 -->
	<!-- <aop:config>
		<aop:pointcut expression="execution(* cn.xxx.service.impl.*ServiceImpl.*(..))" id="txPc"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
	</aop:config> -->
	
	<!-- 事务配置方式2:开启注解事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<!-- ========================================================================== -->
	
	<!-- 将SessionFactory配置到spring容器中(Spring整合Hibernate) -->
	<!-- 加载Hibernate配置信息方案:在spring配置中放置hibernate配置信息 -->
	<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
		<property name="dataSource" ref="dataSource" ></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql" >true</prop>
				<prop key="hibernate.format_sql" >true</prop>
				<prop key="hibernate.hbm2ddl.auto" >update</prop>
			</props>
		</property>
		<!-- 引入orm数据方式一。 -->
		<property name="mappingDirectoryLocations" value="classpath:cn/xxx/domain" ></property>
 
		<!-- 引入orm数据方式二。注入hibernate的映射文件(Maven项目,必须使用此方式) -->
		<property name="mappingLocations">
			<list>
				<value>classpath:com/xxx/bos/domain/*.xml</value>
			</list>
		</property>

	</bean>
	
	
	<!-- Action配置(Spring整合Struts2) -->
	<bean name="userAction" class="cn.xxx.web.action.UserAction" scope="prototype" >
		<!-- 手动配置注入Action的依赖属性 -->
		<property name="userService" ref="userService" ></property>
	</bean>
	<!-- service层 -->
	<bean name="userService" class="cn.xxx.service.impl.UserServiceImpl" >
		<property name="ud" ref="userDao" ></property>
	</bean>
	<!-- dao层 -->
	<bean name="userDao" class="cn.xxx.dao.impl.UserDaoImpl" >
		<!-- 注入sessionFactory -->
		<property name="sessionFactory" ref="sessionFactory" ></property>
	</bean>
	
</beans>

UserServiceImpl.java(Service层,通过注解配置事务):

package cn.xxx.service.impl;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.xxx.dao.UserDao;
import cn.xxx.domain.User;
import cn.xxx.service.UserService;

@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
//通过注解配置事务(类上一般配置 readOnly=true)
public class UserServiceImpl implements UserService{
	
	private UserDao ud;

	@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
	//通过注解配置事务(修改数据库的方法上一般配置 readOnly=false)
	public void saveUser(User u) {
		ud.save(u);
	}

	public void setUd(UserDao ud) {
		this.ud = ud;
	}

}

Test.java(测试类):

package cn.xxx.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.xxx.domain.User;
import cn.xxx.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test {
	
	@Resource(name="userService")
	//注入Service层对象
	private UserService us;
	@Test
	//测试aop事务
	public void fun1(){
		User u = new User();
		
		u.setUser_code("Jack");
		u.setUser_name("杰克");
		u.setUser_password("1234");
		
		us.saveUser(u);
	}
		
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值