[原创]spring及springmvc精简版--继承数据源,声明式事物

1.前期:导入c3p0 jar包,相关数据库连接jar包,我用的是mysql

2.关注事物管理器的配置和AOP配置

 

 

代码: 核心关注bean配置文件

 application.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
11     <!-- 加载属性文件 -->
12     <context:property-placeholder location="classpath:db.properties"/>
13     
14     
15     
16     
17     <!-- 初始化数据源的bean -->
18     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
19         <property name="driverClass" value="${driverClassName1}"></property>
20         <property name="jdbcUrl" value="${url1}"></property>
21         <property name="user" value="${username1}"></property>
22         <property name="password" value="${password1}"></property>
23         <property name="maxPoolSize" value="${maxActive1}"></property>
24         <property name="initialPoolSize" value="${initialSize1}"></property>
25     </bean>
26     <!-- 配置jdbcTemplate -->
27     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
28         <property name="dataSource" ref="dataSource"></property>
29         
30     </bean>
31     
32     <bean id="person" class="com.bean.Person"></bean>
33     
34     <bean id="dao" class="com.dao.Dao">
35         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
36     </bean>
37     <bean id="service" class="com.service.Service">
38         <property name="dao" ref="dao"></property>
39     </bean>
40     
41     <!-- 购买商品的bean -->
42     <bean id="users" class="com.bean.Users"></bean>
43     <bean id="goods" class="com.bean.Goods"></bean>
44     <bean id="usersDao" class="com.dao.UsersDao">
45         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
46     </bean>
47     <bean id="goodsDao" class="com.dao.GoodsDao">
48         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
49     </bean>
50     <bean id="buyService" class="com.service.BuyService">
51         <property name="goodsDao" ref="goodsDao"></property>
52         <property name="usersDao" ref="usersDao"></property>
53     </bean>
54     <bean id="buyMarthService" class="com.service.BuyMarthService">
55         <property name="buyService" ref="buyService"></property>
56     </bean>
57     
58 
59     <!-- 配置事务管理器 -->
60     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
61         <property name="dataSource" ref="dataSource"></property>
62     </bean>
63     <!-- 配置事务属性 -->
64     <tx:advice id="txa" transaction-manager="transactionManager">
65         <tx:attributes>
66             <tx:method name="buyOneGoods" propagation="REQUIRES_NEW"/>
67             <tx:method name="*"/>
68         </tx:attributes>
69     </tx:advice>
70     <!-- 配置事务aop
71     
72      -->
73     
74     <aop:config>
75         <aop:pointcut expression="execution(* com.service.*.*(..))" id="pc"/>
76         <aop:advisor advice-ref="txa" pointcut-ref="pc"/>
77     </aop:config>
78 </beans>

 

db.properties

1 driverClassName1 = com.mysql.jdbc.Driver
2 url1 = jdbc:mysql://localhost:3306/test
3 username1 = root
4 password1 = mysql
5 maxActive1 = 50
6 initialSize1 =20

 

 

com.bean

Users

 1 package com.bean;
 2 
 3 public class Goods {
 4 
 5     private int gid;
 6     private int gprice;
 7     private int gnum;
 8     public int getGid() {
 9         return gid;
10     }
11     public void setGid(int gid) {
12         this.gid = gid;
13     }
14     public int getGprice() {
15         return gprice;
16     }
17     public void setGprice(int gprice) {
18         this.gprice = gprice;
19     }
20     public int getGnum() {
21         return gnum;
22     }
23     public void setGnum(int gnum) {
24         this.gnum = gnum;
25     }
26     
27 }

 

 

Person

 1 package com.bean;
 2 
 3 public class Person {
 4 
 5     private String name;
 6     private int age;
 7     public String getName() {
 8         return name;
 9     }
10     public void setName(String name) {
11         this.name = name;
12     }
13     public int getAge() {
14         return age;
15     }
16     public void setAge(int age) {
17         this.age = age;
18     }
19     
20 }

 

 

User

 1 package com.bean;
 2 
 3 public class Users {
 4 
 5     private int uid    ;
 6     private String uname;
 7     private int umoney;
 8     public int getUid() {
 9         return uid;
10     }
11     public void setUid(int uid) {
12         this.uid = uid;
13     }
14     public String getUname() {
15         return uname;
16     }
17     public void setUname(String uname) {
18         this.uname = uname;
19     }
20     public int getUmoney() {
21         return umoney;
22     }
23     public void setUmoney(int umoney) {
24         this.umoney = umoney;
25     }
26     
27 }

 

 

 

DAO

DAO

 1 package com.dao;
 2 import org.springframework.jdbc.core.JdbcTemplate;
 3 
 4 import com.bean.Person;
 5 
 6 
 7 public class Dao {
 8     
 9     private JdbcTemplate jdbcTemplate;
10     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
11         this.jdbcTemplate = jdbcTemplate;
12     }
13 
14     public int update(Person p){
15         int num = jdbcTemplate.update("update person set name=? where age=?", p.getName(),p.getAge());
16         return num;
17     }
18 }

 

 

GoodsDao

 1 package com.dao;
 2 
 3 import org.springframework.jdbc.core.JdbcTemplate;
 4 
 5 public class GoodsDao {
 6 
 7     private JdbcTemplate jdbcTemplate;
 8     
 9     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
10         this.jdbcTemplate = jdbcTemplate;
11     }
12     
13     public int getGpriceByGid(int gid){
14         int gprice = jdbcTemplate.queryForObject("select gprice from goods where gid=?", Integer.class, gid);
15         return gprice;
16     }
17     
18     public int getGnumByGid(int gid){
19         int gnum = jdbcTemplate.queryForObject("select gnum from goods where gid=?", Integer.class, gid);
20         return gnum;
21     }
22     
23     public void updateGnum(int gid,int num){
24         jdbcTemplate.update("update goods set gnum=gnum-? where gid=?", num,gid);
25     }
26 }

 

 

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

 1 package com.service;
 2 
 3 public class BuyMarthService {
 4 
 5     private BuyService buyService;
 6     public void setBuyService(BuyService buyService) {
 7         this.buyService = buyService;
 8     }
 9     
10     public void buygoodsList(int uid,int[] gids,int[] nums){
11         for(int i = 0;i<gids.length;i++){
12             buyService.buyOneGoods(uid, gids[i], nums[i]);
13         }
14         
15     }
16 }

 

 

 1 package com.service;
 2 
 3 import com.dao.GoodsDao;
 4 import com.dao.UsersDao;
 5 
 6 public class BuyService {
 7 
 8     private UsersDao usersDao;
 9     private GoodsDao goodsDao;
10     public void setGoodsDao(GoodsDao goodsDao) {
11         this.goodsDao = goodsDao;
12     }
13     public void setUsersDao(UsersDao usersDao) {
14         this.usersDao = usersDao;
15     }
16     
17     public void buyOneGoods(int uid,int gid,int num){
18         /*
19          * 1-获取商品的库存
20          * 2-验证库存是否足够
21          *  1-不足:抛出异常  (回滚)
22          *  2-足:修改商品的库存
23          * 3-修改余额等同2
24          */
25         int gnum = goodsDao.getGnumByGid(gid);
26         if(gnum <= num){
27             throw new RuntimeException("商品库存不足");
28         }
29         goodsDao.updateGnum(gid, num);
30         int gprice = goodsDao.getGpriceByGid(gid);
31         int umoney = usersDao.getMoneyByUid(uid);
32         if(umoney < gprice*num){
33             throw new RuntimeException("用户余额不足");
34         }
35         usersDao.updateUmoney(uid, gprice*num);
36     }
37     
38 }

 

 

 1 package com.service;
 2 
 3 import com.bean.Person;
 4 import com.dao.Dao;
 5 
 6 public class Service {
 7 
 8     private Dao dao;
 9     public void setDao(Dao dao) {
10         this.dao = dao;
11     }
12     public void updatePerson(Person p){
13         dao.update(p);
14     }
15 }

 

posted @ 2017-04-05 20:00  jason_zhangz  阅读(392)  评论(0编辑  收藏  举报