[原创]spring及springmvc精简版--继承数据源,声明式事物
1.前期:导入c3p0 jar包,相关数据库连接jar包,我用的是mysql
2.关注事物管理器的配置和AOP配置
代码: 核心关注bean配置文件
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 加载属性文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 初始化数据源的bean -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClassName1}"></property>
<property name="jdbcUrl" value="${url1}"></property>
<property name="user" value="${username1}"></property>
<property name="password" value="${password1}"></property>
<property name="maxPoolSize" value="${maxActive1}"></property>
<property name="initialPoolSize" value="${initialSize1}"></property>
</bean>
<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property> </bean> <bean id="person" class="com.bean.Person"></bean> <bean id="dao" class="com.dao.Dao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="service" class="com.service.Service">
<property name="dao" ref="dao"></property>
</bean> <!-- 购买商品的bean -->
<bean id="users" class="com.bean.Users"></bean>
<bean id="goods" class="com.bean.Goods"></bean>
<bean id="usersDao" class="com.dao.UsersDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="goodsDao" class="com.dao.GoodsDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="buyService" class="com.service.BuyService">
<property name="goodsDao" ref="goodsDao"></property>
<property name="usersDao" ref="usersDao"></property>
</bean>
<bean id="buyMarthService" class="com.service.BuyMarthService">
<property name="buyService" ref="buyService"></property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务属性 -->
<tx:advice id="txa" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="buyOneGoods" propagation="REQUIRES_NEW"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务aop --> <aop:config>
<aop:pointcut expression="execution(* com.service.*.*(..))" id="pc"/>
<aop:advisor advice-ref="txa" pointcut-ref="pc"/>
</aop:config>
</beans>
db.properties
driverClassName1 = com.mysql.jdbc.Driver
url1 = jdbc:mysql://localhost:3306/test
username1 = root
password1 = mysql
maxActive1 = 50
initialSize1 =20
com.bean
Users
package com.bean;
public class Goods {
private int gid;
private int gprice;
private int gnum;
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public int getGprice() {
return gprice;
}
public void setGprice(int gprice) {
this.gprice = gprice;
}
public int getGnum() {
return gnum;
}
public void setGnum(int gnum) {
this.gnum = gnum;
}
}
Person
package com.bean;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
User
package com.bean;
public class Users {
private int uid ;
private String uname;
private int umoney;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getUmoney() {
return umoney;
}
public void setUmoney(int umoney) {
this.umoney = umoney;
}
}
DAO
DAO
package com.dao;
import org.springframework.jdbc.core.JdbcTemplate; import com.bean.Person; public class Dao { private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} public int update(Person p){
int num = jdbcTemplate.update("update person set name=? where age=?", p.getName(),p.getAge());
return num;
}
}
GoodsDao
package com.dao;
import org.springframework.jdbc.core.JdbcTemplate;
public class GoodsDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int getGpriceByGid(int gid){
int gprice = jdbcTemplate.queryForObject("select gprice from goods where gid=?", Integer.class, gid);
return gprice;
}
public int getGnumByGid(int gid){
int gnum = jdbcTemplate.queryForObject("select gnum from goods where gid=?", Integer.class, gid);
return gnum;
}
public void updateGnum(int gid,int num){
jdbcTemplate.update("update goods set gnum=gnum-? where gid=?", num,gid);
}
}
UserDao
package com.dao;
import org.springframework.jdbc.core.JdbcTemplate;
public class UsersDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int getMoneyByUid(int uid){
int umoney = jdbcTemplate.queryForObject("select umoney from users where uid=?", Integer.class, uid);
return umoney;
}
public void updateUmoney(int uid,int price){
jdbcTemplate.update("update users set umoney=umoney-? where uid=?", price,uid);
}
}
Service
package com.service;
public class BuyMarthService {
private BuyService buyService;
public void setBuyService(BuyService buyService) {
this.buyService = buyService;
}
public void buygoodsList(int uid,int[] gids,int[] nums){
for(int i = 0;i<gids.length;i++){
buyService.buyOneGoods(uid, gids[i], nums[i]);
}
}
}
package com.service; import com.dao.GoodsDao;
import com.dao.UsersDao; public class BuyService { private UsersDao usersDao;
private GoodsDao goodsDao;
public void setGoodsDao(GoodsDao goodsDao) {
this.goodsDao = goodsDao;
}
public void setUsersDao(UsersDao usersDao) {
this.usersDao = usersDao;
} public void buyOneGoods(int uid,int gid,int num){
/*
* 1-获取商品的库存
* 2-验证库存是否足够
* 1-不足:抛出异常 (回滚)
* 2-足:修改商品的库存
* 3-修改余额等同2
*/
int gnum = goodsDao.getGnumByGid(gid);
if(gnum <= num){
throw new RuntimeException("商品库存不足");
}
goodsDao.updateGnum(gid, num);
int gprice = goodsDao.getGpriceByGid(gid);
int umoney = usersDao.getMoneyByUid(uid);
if(umoney < gprice*num){
throw new RuntimeException("用户余额不足");
}
usersDao.updateUmoney(uid, gprice*num);
} }
package com.service; import com.bean.Person;
import com.dao.Dao; public class Service { private Dao dao;
public void setDao(Dao dao) {
this.dao = dao;
}
public void updatePerson(Person p){
dao.update(p);
}
}
[原创]spring及springmvc精简版--继承数据源,声明式事物的更多相关文章
- [原创]spring及springmvc精简版--AOP
接上一篇:[原创]spring及springmvc精简版--IOC 理解AOP.java是一种面向对象的语言.而AOP是面向切面,在我看来是面向逻辑或者业务编程,它是对一组逻辑的抽象和分配. 经典例子 ...
- [原创]spring及springmvc精简版--IOC
本篇博客为自己学习spring和springmvc的一个总结.主要以代码为主,至于文字性描述理解性东西,可以自行百度.有认识不妥的地方,还望指出,相互学习. 以前很困惑spring中的一些概念,在学习 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- 事务管理(下) 配置spring事务管理的几种方式(声明式事务)
配置spring事务管理的几种方式(声明式事务) 概要: Spring对编程式事务的支持与EJB有很大的区别.不像EJB和Java事务API(Java Transaction API, JTA)耦合在 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- spring事务管理——编程式事务、声明式事务
本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本教程假定您已经掌握了 ...
- Spring学习之声明式事物管理
public List<Student> selectStudent() { Student s = new Student(); s.setName("zhengbin&quo ...
- 9.spring:事务管理(下):声明式事务管理
声明式事务管理 sprin的声明式事务是管理AOP技术实现的事务管理,其本质是是对方法前后进行拦截,然后 在目标方法开始之前创建或者加入一个事务,在执行完成目标方法之后根据执行情况提交或者回滚事务. ...
- SSH学习——声明式事物管理(Spring)
1.什么是事物? 事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等,事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出现了差错,比 ...
随机推荐
- 5.25思考双向绑定为什么容易接收js的方式
一直在考虑为什么html上面的双向绑定好像比winform上面的好用. 那是因为html可以输入任意的东西,不会有问题.而winform不行.这样导致了.html可以比较直观的处理这类需求. 举个例子 ...
- markdown编辑器的小建议
markdown编辑器使用建议 yaung by 2012.12.1-------- 这里主要说明一下我们在windows和linux下对md文件的编辑方法,为大家提供一点个人建议,如果有更好的选择 ...
- Ultra-QuickSort - poj 2299 (归并排序+统计逆序数)
利用归并排序统计逆序数,利用归并求逆序在对子序列s1和s2在归并时(s1,s2已经排好序),若s1[i]>s2[j](逆序状况),则逆序数加上s1.length-i,因为s1中i后面的数字对于s ...
- MAC信息摘要
MAC(Message Authentication Code ,消息认证码算法)是含有密钥散列函数算法,兼容MD和SHA算法的特性,并在此基础上加入了密钥.因此,MAC也称为HMAC. ...
- plsql programming 07 使用数据
数据类型 char, Nchar varchar2, Nvarchar2 clob, Nclob number number(9, 2); -- 定点小数, 小数点左边7位, 右边2位 number ...
- ThinkPHP与EasyUI整合之二(datagrid):删除多条记录
学习EasyUI已有一段时间了,现在开始逐步把平时学习的细节和难点记录下来. 1. datagrid选中多条记录的语句是: var rows = $('#dg').datagrid('getSelec ...
- android自定义View_2——Making the View Interactive
前言:绘制出一个view只是自定义view的一个部分,还需要自定义一些view的行为,来反馈用户的使用操作,反馈的 行为要合理性,就像真是的物理世界一样,不要太玄幻哦. 可以将view的行为封装到in ...
- 一步步学习python
python是一种功能比较强的脚本,尤其是在网络应用上,又称作:胶水语言.具体的简介可以在维基百科.百度百科等查得到他的发展史,有事一贯犹 如unix linux等老外打发无聊时间发明的强大工具,这是 ...
- Win下httpd+php+mysql环境集成
apache+php+mysql: php下载: VC6就是legacy Visual Studio 6 compiler,就是使用这个编译器编译的, VC9就是the Visual Studio ...
- Jmeter做读取csv接口测试
最近在工作中,对jmeter实践的点滴的记录这里分享,不一定正确,仅供参考和讨论,有想法的欢迎留言.谈论. 1技巧1:从csv中获取带引号的数据详情 背景:我们从csv中获取数据,在jmeter中使用 ...