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);
}
}