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