博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
转账-AOP基于xml和基于注解
阅读量:4617 次
发布时间:2019-06-09

本文共 3653 字,大约阅读时间需要 12 分钟。

需求:完成转账操作

环境配置

-,创建表

CREATE DATABASE ee19_spring_day03;

USE ee19_spring_day03;
CREATE TABLE account(
  id INT PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(50),
  money INT
);
INSERT INTO account(username,money) VALUES('jack','10000');
INSERT INTO account(username,money) VALUES('rose','10000');

二,导入jar包

l  核心:4+1

l  aop : 4 (aop联盟、spring aop、aspectj规范、spring aspect)

l  数据库:2  (jdbc/tx)

l  驱动:mysql

l  连接池:c3p0

dao层

package com.itheima.dao.impl;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {    @Override    public void out(String outer, Integer money) {        this.getJdbcTemplate().update("update account set money = money - ? where username = ?", money,outer);    }    @Override    public void in(String inner, Integer money) {        this.getJdbcTemplate().update("update account set money = money + ? where username = ?", money,inner);    }}

service层

package com.itheima.service.impl;import com.itheima.dao.impl.AccountDao;public class AccountServiceImpl implements AccountService {    private AccountDao accountDao;    public void setAccountDao(AccountDao accountDao) {        this.accountDao = accountDao;    }    @Override    public void transfer(String outer, String inner, Integer money) {        accountDao.out(outer, money);        accountDao.in(inner, money);    }}

基于xml的形式如下:

applicationContext.xml文件,放在src下

测试类

package com.itheima;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.itheima.service.impl.AccountService;public class TestApp {       @Test    public void demo01(){        //获得容器        String xmlPath = "applicationContext.xml";        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);        //获得容器内容,不需要自己new,都是从spring中获得,applicationContext.getBean("accountService")指的是id        AccountService accountService =  (AccountService) applicationContext.getBean("accountService");        accountService.transfer("jack", "rose", 1000);    }}

转账完成

 基于注解的形式如下:修改的是applicationContext.xml

 

只修改AccountServiceImpl类,如下:添加注解

 

package com.itheima.service.impl;import org.springframework.transaction.annotation.Isolation;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import com.itheima.dao.impl.AccountDao;@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)    //这代表的是添加的注解形式public class AccountServiceImpl implements AccountService {    private AccountDao accountDao;    public void setAccountDao(AccountDao accountDao) {        this.accountDao = accountDao;    }    @Override    public void transfer(String outer, String inner, Integer money) {        accountDao.out(outer, money);        //断电//        int i = 1/0;        accountDao.in(inner, money);    }}

运行之前的测试类,即可完成转账,与aop基于xml的形式只有上述的改变而已

 

 

转载于:https://www.cnblogs.com/zjf-293916/p/7441513.html

你可能感兴趣的文章
joj1023
查看>>
动画原理——旋转
查看>>
Finding LCM LightOJ - 1215 (水题)
查看>>
python生成器
查看>>
PowerDesigner Constraint name uniqueness 错误
查看>>
系统子系统_GPRS子系统流程图
查看>>
为什么 NSLog 不支持 Swift 对象(转)
查看>>
Ubuntu 下搭建SVN服务器
查看>>
css3转换
查看>>
将字符串中不同字符的个数打印出来
查看>>
java第三次上机
查看>>
android Javah生成JNI头文件
查看>>
npm创建react项目
查看>>
关于u32中查找和定位最后到bit Number of 1 Bits
查看>>
sql数据库查询
查看>>
云计算技能图谱
查看>>
委托、Lambda表达式和事件
查看>>
typecho模板制作代码收集
查看>>
Python学习笔记4:集合方法
查看>>
elasticsearch的监控脚本
查看>>