《Spring视频教程》(p5)

p5 使用注解事先声明式事务

使用注解实现事务(声明式事务)

目标:通过事务,使以下方法,要么全成功,要么全失败

public void addStudent

{   

//增加班级

//增加学生

//crud

}

a. jar包

b. 配置

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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-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">

	<!-- 配置扫描器 -->
	<context:component-scan base-package="org.lanqiao.dao.impl"></context:component-scan>

	<!-- 配置数据库相关事务 -->
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
		<property name="url" value="127.0.0.1:1521:ORCL"></property>
		<property name="username" value="system"></property>
		<property name="password" value="12345678"></property>
		<property name="maxIdle" value="6"></property>
	
	
	</bean>
	
	<!-- 配置事务管理器txManager -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
		
	<!-- 增加对事务的支持 -->
	<tx:annotation-driven transaction-manager="txManager" />






	<!-- 该文件中产生的所有对象,被spring放入了一个称之为spring ioc容器的地方  -->
	<!-- id:唯一标识符     class:指定类型 -->
	<bean id="student" class="org.lanqiao.entity.Student">
		<!-- 
			property: 该class所代表的类的属性
			name: 属性名
			value: 属性值
		
		 -->
		<property name="stuNo" value="2"></property>
		<property name="stuName" value="ls"></property>
		<property name="stuAge" value="24"></property>
	
	</bean>
	
	<bean id="javaCourse" class="org.lanqiao.newinstance.JavaCourse">
	</bean>
	<bean id="htmlCourse" class="org.lanqiao.newinstance.HtmlCourse">
	</bean>
	
	<bean id="teacher" class="org.lanqiao.entity.Teacher" p:age="25" p:name="ww">
		<!--  通过set方式赋值
		<property name="name" value="zs"></property>
		<property name="age" value="23"></property>
		-->
		
		<!-- 通过构造器赋值 -->
		<!--  
		<constructor-arg value="ls" index="0"></constructor-arg>
		<constructor-arg value="24" index="1"></constructor-arg>
	
		<constructor-arg value="ls" name="name"></constructor-arg>
		<constructor-arg value="24" name="age"></constructor-arg>
		
		<constructor-arg value="ls" type="String"></constructor-arg>
		<constructor-arg value="24" type="int"></constructor-arg>
		-->
		
		
	</bean>
	
	<bean id="course" class="org.lanqiao.entity.Course" p:courseHour="300" p:courseName="hadoop" p:teacher-ref="teacher">
		<!--  
		 xx.setCourseName("java")
		<property name="courseName" value="java"></property>
		<property name="courseHour" value="200"></property>
		 将teacher对象注入到course对象中 
			xx.setTeacher(teacher);
		
		<property name="teacher" ref="teacher"></property>
		-->
		
		<!-- 通过构造器 -->
		<!--  
		<constructor-arg value="c"></constructor-arg>
		<constructor-arg value="100"></constructor-arg>
		<constructor-arg ref="teacher"></constructor-arg>
		-->
	</bean>
	
	<bean id="collectionDemo" class="org.lanqiao.entity.AllCollectionType">
		<!--  通过set方式赋值  -->
		<property name="listElement">
			<list>
				<value>足球</value>
				<value>篮球</value>
				<value>乒乓球</value>
			</list>
		</property> 

		<!-- 通过constructor方法 			
		<constructor-arg name="listElement">
			<list>
				<value>足球xx</value>
				<value>篮球xx</value>
				<value>乒乓球xx</value>
			</list>			
		</constructor-arg>
		-->
		
		<property name="arrayElement">
			<array>
				<value>足球1</value>
				<value>篮球1</value>
				<value>乒乓球1</value>
			</array>
		</property>
		<property name="setElement">
			<set>
				<value>足球2</value>
				<value>篮球2</value>
				<value>乒乓球2</value>
			</set>
		</property>
		<property name="mapElement">
			<map>
				<entry>
					<key>
						<value>foot</value>
					</key>
					<value>足球3</value>
				</entry>
			
				<entry>
					<key>
						<value>basketball</value>
					</key>
					<value>篮球3</value>
				</entry>
				
				<entry>
					<key>
						<value>table tennis</value>
					</key>
					<value>乒乓球3</value>
				</entry>
			</map>
		</property>
		<property name="propsElement">
			<props>
				<prop key="foot4">足球4</prop>
				<prop key="basketball4">篮球4</prop>
				<prop key="pp4">乒乓4</prop>
			</props>
		</property>
	</bean>
	
	<!-- 配置前置通知 -->
	<!-- addStudent()所在方法 -->
	<!--  
	<bean id="studentService" class="org.lanqiao.service.StudentServiceImpl">
		<property name="studentDao" ref="studentDao"></property>
	</bean>
	-->
	
	<!-- 前置通知类 -->
	<bean id="logBefore" class="org.lanqiao.aop.LogBefore">
	</bean>
	
	<!-- 将addStudent()和通知进行关联 -->
	<aop:config>
		<!-- 配置切入点(在哪里执行通知) -->
		<aop:pointcut expression="execution(public void org.lanqiao.service.StudentServiceImpl.addStudent(Student student))" id="pcut"></aop:pointcut>
		<aop:advisor advice-ref="LogBefore" pointcut-ref="pcut"/>
	</aop:config>
	
	
	<bean id="studentDao" class="org.lanqiao.service.StudentServiceImpl">
	
	</bean>
	
	<bean id="studentService" class="org.lanqiao.service.StudentServiceImpl">
		<property name="studentDao" ref="studentDao"></property>
	</bean>
	
	
	
	
	
	
	
</beans>



















StudentServiceImpl.java

package org.lanqiao.service;

import org.lanqiao.dao.IStudentDao;
import org.lanqiao.dao.impl.StudentDaoImpl;
import org.lanqiao.entity.Student;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class StudentServiceImpl implements IStudentService{
	IStudentDao StudentDao;
	
	public void setStudentDao(IStudentDao studentDao) {
		StudentDao = studentDao;
	}

	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	@Override
	public void addStudent(Student student) {
		// TODO Auto-generated method stub
		//if(该学生存在)
		StudentDao.addStudent(student);
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我们的SpringCloud视频教程包含了对Dubbo和SpringCloud的比较,并深入讲解了SpringCloud的核心组件,包括Eureka、Ribbon、Feign、Hystrix、HystrixDashboard、Zuul和Config。\[1\] 此外,我们的课程还涵盖了SpringSpring MVC的内容,包括为什么要使用Spring、AOP的解释、IOC的解释、Spring的主要模块、常用的注入方式、Bean的线程安全性、Bean的作用域、自动装配Bean的方式、Spring事务的实现方式、Spring事务隔离、Spring MVC的运行流程、Spring MVC的组件以及@RequestMapping和@Autowired的作用。\[2\] 对于Spring Boot和Spring Cloud,我们也有相应的内容。我们解释了什么是Spring Boot,为什么要使用Spring Boot,Spring Boot的核心配置文件是什么,Spring Boot的配置文件有哪几种类型以及它们的区别,以及实现热部署的方式。我们还解释了JPA和Hibernate的区别,以及什么是Spring Cloud,Spring Cloud断路器的作用,以及Spring Cloud的核心组件。\[3\] 希望这些视频教程能够帮助您更好地理解和学习SpringCloud。 #### 引用[.reference_title] - *1* [视频教程-Spring Cloud微服务--入门到精通-Java](https://blog.csdn.net/weixin_32427413/article/details/106511569)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [springcloud视频教程全集](https://blog.csdn.net/m0_59157465/article/details/119219583)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值