代碼:
book.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:component-scan base-package="com.junruo.book"></context:component-scan>
<!-- 引入屬性檔案 -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="https://bbs.csdn.net/topics/db.properties"></property>
</bean> -->
<!-- 引入屬性檔案 -->
<context:property-placeholder location="db.properties"/>
<!-- 創建資料源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="https://bbs.csdn.net/topics/${jdbc.driver}"></property>
<property name="url" value="https://bbs.csdn.net/topics/${jdbc.url}"></property>
<property name="username" value="https://bbs.csdn.net/topics/${jdbc.username}"></property>
<property name="password" value="https://bbs.csdn.net/topics/${jdbc.password}"></property>
</bean>
<!-- 通過資料源配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事務管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 開啟注解驅動,即對事務相關的注解進行掃描,決議含義并執行功能 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
</beans>
BookDaoImpl.java持久層
package com.junruo.book.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.junruo.book.dao.BookDao;
import com.junruo.book.exception.MyException;
@Repository
public class BookDaoImpl implements BookDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Integer selectPrice(String bid) {
Integer price = jdbcTemplate.queryForObject("select price from book where bid = ?",new Object[] {bid}, Integer.class);
return price;
}
@Override
public void updateSt(String bid) {
//獲取該書籍的庫存
Integer st = jdbcTemplate.queryForObject("select st from stock where sid = ?",new Object[] {bid},Integer.class);
if (st <= 0) {
throw new RuntimeException();
}else {
jdbcTemplate.update("update stock set st = st -1 where sid = ?", bid);
}
}
@Override
public void updataBalance(String uid,Integer price) {
Integer balance = jdbcTemplate.queryForObject("select balance from money where uid = ?", new Object[] {uid}, Integer.class);
if (balance < price) {
throw new RuntimeException();
}else {
jdbcTemplate.update("update money set balance = balance - ? where uid = ?", price,uid);
}
}
}
BookServiceImpl.java 業務處理層
package com.junruo.book.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.junruo.book.dao.BookDao;
import com.junruo.book.exception.MyException;
import com.junruo.book.service.BookService;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao dao;
@Transactional
public void buyBook(String bid, String uid) {
Integer price = dao.selectPrice(bid);
dao.updateSt(bid);
dao.updataBalance(uid, price);
}
}
BookController.java控制層
package com.junruo.book.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import com.junruo.book.service.BookService;
import com.junruo.book.service.Cashier;
@Controller
public class BookController {
@Autowired
private BookService service;
@Autowired
private Cashier cashier;
//購買單本書
public void buyBook() {
service.buyBook("1", "1001");
}
}
Test.java測驗類
package com.junruo.book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.junruo.book.controller.BookController;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("book.xml");
BookController controller = ac.getBean("bookController",BookController.class);
controller.buyBook();
//controller.checkout();
}
}
還有兩個介面類和jdbc的組態檔沒有問題
資料表資料
書價格:50
庫存:10
余額:80
第一次運行后:
庫存:9
余額:30
第二次運行后(報RuntimeException() 但沒回滾):
庫存:8
余額:30
有沒有大佬知道為什么嘛!!!
uj5u.com熱心網友回復:
mysql的話檢查是否是innodb.uj5u.com熱心網友回復:
我剛剛看了一下,三個表都是myisam型別的
uj5u.com熱心網友回復:
謝謝了解決了,換一下資料表型別就可以了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/146473.html
標籤:Java SE
