AOP__事务配置(简单代码xml配置,注解等)

不标准的MVC:

1、XML配置的方式
DAO:

@Repository
public class UserDao {
	@Resource
	private JdbcTemplate jdbcTemple = new JdbcTemplate();
	
	public void insUser() throws SQLException {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO emp(ename,job,mgr,sal,comm,deptno) VALUES(?,?,?,?,?,?);";
		int update = jdbcTemple.update(sql, "ename222","job",1000,1000,1000,20);
		System.out.println(update);
	}
	
	public void updUser() throws SQLException  {
		// TODO Auto-generated method stub
		String sql = "UPDATE emp SET ename = ?,job = ?,mgr = ?,sal = ?,comm = ?,deptno =? WHERE empno = ?;;";
		int update = jdbcTemple.update(sql, "ename111","job111",1000,1000,1000,20,7934);
		System.out.println(update);
		
	}
@Service
public class UserService {
	@Autowired
	private UserDao userDao;
	
		public int insUser() throws SQLException {
		// TODO Auto-generated method stub
		userDao.insUser();
		
		return 0;
	}
	
public int updUser() throws SQLException {
		// TODO Auto-generated method stub
		userDao.updUser();
		return 0;
	}
}

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: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.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<!-- 开启扫描 -->
	<context:component-scan base-package="com.dao,com.pojo,com.service"></context:component-scan>
	<!-- 配置DataSource -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 配置数据库的相关信息  -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8"/>
		<property name="username" value="xxx"/>
		<property name="password" value="xxxx"/>
	</bean>
	<!-- 配置JdbcTemplate,(可以更改这里为了简便) -->
	<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
		<!-- 构造注入的方式 设置DataSource,也可使用设值注入的方式 -->
		<constructor-arg name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 配置Spring的事务管理 -->
 	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 设置事务的传播属性 -->
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
		<!-- 方法的命名以ins或者upd开头的,类似相关命名,采用通配符 -->
			<tx:method name="ins*" propagation="REQUIRED"/>
			<tx:method name="upd*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	<!-- 配置AOP事务 -->
   <aop:config>
<!-- 切点 -->
		<aop:pointcut expression="execution(* com.service.UserService.*(..))" id="myPointcut"/>
		<aop:advisor advice-ref="txAdvice"  pointcut-ref="myPointcut"/>
	</aop:config>
</beans>

2、采用注解的方式:
DAO:

@Repository
public class UserDao {
	@Resource
	private JdbcTemplate jdbcTemple = new JdbcTemplate();
	
	/*public void selUser() {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM emp";
//		RowCallbackHandler rowMapper;BeanPropertyRowMapper
		List<User> list = jdbcTemple.query(sql, new BeanPropertyRowMapper(User.class));
		for (User user : list) {
			System.out.println(user);
		}
	}*/
	
	public void insUser() throws SQLException {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO emp(ename,job,mgr,sal,comm,deptno) VALUES(?,?,?,?,?,?);";
		int update = jdbcTemple.update(sql, "ename222","job",1000,1000,1000,20);
		System.out.println(update);
	}
	
	public void updUser() throws SQLException  {
		// TODO Auto-generated method stub
		String sql = "UPDATE emp SET ename = ?,job = ?,mgr = ?,sal = ?,comm = ?,deptno =? WHERE empno = ?;;";
		int update = jdbcTemple.update(sql, "ename111","job111",1000,1000,1000,20,7934);
		System.out.println(update);
		
	}
}

Service,使用@Transactional

@Service
public class UserService {
	@Autowired
	private UserDao userDao;
	
	@Transactional( propagation = Propagation.REQUIRES_NEW)
	public int insUser() throws SQLException {
		// TODO Auto-generated method stub
		userDao.insUser();
		
		return 0;
	}
	
	@Transactional( propagation = Propagation.REQUIRES_NEW)
	public int updUser() throws SQLException {
		// TODO Auto-generated method stub
		userDao.updUser();
		return 0;
	}
}

Application.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: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.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<!-- 开启扫描 -->
	<context:component-scan base-package="com.dao,com.pojo,com.service"></context:component-scan>
	<!-- 配置DataSource -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 配置数据库的相关信息  -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8"/>
		<property name="username" value="root"/>
		<property name="password" value="rootzsl"/>
	</bean>
	<!-- 配置JdbcTemplate,(可以更改这里为了简便) -->
	<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
		<!-- 构造注入的方式 设置DataSource,也可使用设值注入的方式 -->
		<constructor-arg name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 配置Spring的事务管理 -->
 	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
上一篇:php – Phar存档和存根文件用法


下一篇:字节跳动 京东 美团 腾讯面试总结,全网独家首发!