1. 程式人生 > >Spring入門知識 ———— 學習事務前的準備工作

Spring入門知識 ———— 學習事務前的準備工作

一、引言

在我們學習事物之前呢,肯定需要準備一個使用事務的場景,方便後面學習事務做準備。這個案例非常簡單,在我們生活隨處可見,也方便大家理解。

我相信大家肯定拿過爸媽給的零花錢,去買零食。在我們拿零花錢在購買的時候,是不是零花錢要減少,並且你購買的零食的商品庫存也要減少是吧。 舉個例子,注意這不是打廣告。 小編手裡面有10塊錢零花錢,這個時候小編口饞了,想來一包樂事的薯片。薯片的價格為3塊,小編就購買了一包,這時候小編就只剩下7元,並且薯片的庫存減少了1,因為被小編買走了。

這個場景應該都能理解吧~~~~

二、資料庫結構

表一:賬戶表,就是錢包,一個使用者對應一個錢包。

CREATE TABLE `tb_account` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '使用者編碼',
  `moeny` int(11) DEFAULT NULL COMMENT '賬戶金額',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

表二:商品表如下

CREATE TABLE `tb_commodity` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '商品編碼',
  `name` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '商品名稱',
  `price` int(11) DEFAULT NULL COMMENT '商品價格',
  `stock` int(12) DEFAULT NULL COMMENT '商品庫存',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

三、程式碼實現

持久層:其中有三個方法需要用到,Commodity對應的商品表的Java實體類。

public interface DataBaseDao {

    /**
     * 修改賬戶資金
     * @param id 使用者的賬戶id
     * @param money 購買商品的金額
     * @return
     */
     int updateAccount(int id,int money);

    /**
     * 修改商品庫存
     * @param id 商品id
     * @return
     */
     int updateStory(int id);

    /**
     * 通過id查詢商品
     * @param id
     * @return
     */
     Commodity selectCommodityByid(int id);

}
@Repository
public class DataBaseDaoImpl implements DataBaseDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int updateAccount(int id, int money) {
        String sql = "update tb_account set moeny = moeny - ? where id = ?";
        return jdbcTemplate.update(sql, money, id);
    }

    public int updateStory(int id) {
        String sql = "update tb_commodity set stock = stock - 1 where id = ?";
        return jdbcTemplate.update(sql, id);
    }

    public Commodity selectCommodityByid(int id) {
        String sql = "select * from tb_commodity where id = ?";
        RowMapper<Commodity> rowMapper = new BeanPropertyRowMapper<Commodity>(Commodity.class);
        Commodity commodity = jdbcTemplate.queryForObject(sql,rowMapper,id);
        return commodity;
    }
}

業務層:先通過商品編碼查詢具體商品,通過使用者編碼減去使用者對應的賬戶金額,最後更新商品庫存操作。

@Service
public class DataBaseService {

    @Autowired
    private DataBaseDao dataBaseDao;

    /**
     * 購物操作
     * @param userid 使用者編碼,減去該使用者賬戶相對應的購買商品金額
     * @param cid 商品編碼,查詢該商品的價格,以及購買成功是庫存-1
     * @return
     */
    public int goShopping(int userid,int cid){

        //查詢商品
        Commodity commodity =  dataBaseDao.selectCommodityByid(cid);

        //減去使用者賬戶金額
        dataBaseDao.updateAccount(userid,commodity.getPrice());

        //減去該商品的庫存
        dataBaseDao.updateStory(cid);

        return 1;
    }
}

XML配置檔案:都學習到這一章節了,小編應該不需要把配置檔案貼出來了,小夥伴最好動手寫一寫,上一個章節也有講到。

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入外部屬性檔案-->
    <context:property-placeholder location="db.properties"></context:property-placeholder>

    <!--掃描註解的包-->
    <context:component-scan base-package="com.spring.five"></context:component-scan>

    <!--配置阿里巴巴連線池-->
    <bean id="DruidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driver}"></property>
    </bean>

    <!--配置JdbcTemp laTemplate-->
    <bean class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="DruidDataSource"></property>
    </bean>
</beans>

最後測試測試:經小編測試,最後使用者1的賬戶金額減去了商品1的金額,並且商品庫存也減少了1。

如果效果不是小編上述所說,那就是存在問題的,可以留言,有我小編專業人士為您解答,哈哈哈哈啊哈~!

  public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext-five.xml");
        DataBaseService baseService = beanFactory.getBean(DataBaseService.class);
        //使用者編碼為1、商品編碼為1
        System.out.println(baseService.goShopping(1,1));
    }