加入jar包:
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.3.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
然后是mysql驱动包即C3P0的jar包:
c3p0-0.9.1.2.jar
mysql-connector-java-5.1.37-bin.jar
在src目录下创建一个jdbc.properties文件
jdbc.user=root
jdbc.passowrd=123456
jdbc.url=jdbc:mysql://localhost:3306/tx
jdbc.driver=com.mysql.jdbc.Driver
在Spring配置文件中配置数据源:
<!-- 引入外部属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源 --> <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.passowrd}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driver}"></property> </bean>
在Spring配置文件中配置JdbcTemplate
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="comboPooledDataSource"></property> </bean>
在Spring配置文件中配置扫描的包
<context:component-scan base-package="com.neuedu"></context:component-scan>
创建Dao层的包:
@Repository public class BookDao { @Autowired private JdbcTemplate jdbcTemplate; /** * [1]根据isbn的值查询书的价格 [2]根据isbn的值减少书的库存,假设每次都只买1本书 [3]根据用户名减少用户账户中的余额,减少的额度就是书的价格 */ public int findPriceByIsbn(String isbn){ String sql = "SELECT price FROM book WHERE isbn = ?"; Integer price = jdbcTemplate.queryForObject(sql, Integer.class, isbn); return price; } }
//[2]根据isbn的值减少书的库存,假设每次都只买1本书public void updateStockByIsbn(String isbn){ String sql = "UPDATE book_stock SET stock = stock -1 WHERE isbn = ?"; jdbcTemplate.update(sql, isbn); } 继续测试: public class TestDataSource { private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); private BookDao bean = ioc.getBean(BookDao.class); @Test public void test02() throws SQLException { bean.updateStockByIsbn("ISBN-004"); } }
[3]根据用户名减少用户账户中的余额,减少的额度就是书的价格
public void updateBalance(String userName,int price){ String sql = "UPDATE account SET balance = balance - ? WHERE username = ?"; jdbcTemplate.update(sql, price,userName); }
创建service层:
@Service public class BookService { @Autowired private BookDao bookDao; public void doCash(String isbn,String username){ int price = bookDao.findPriceByIsbn(isbn); bookDao.updateStockByIsbn(isbn); bookDao.updateBalance(username, price); } }
测试:
public class TestDataSource { private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); private BookDao bean = ioc.getBean(BookDao.class); private BookService bookService = ioc.getBean(BookService.class); @Test public void test04() throws SQLException { bookService.doCash("ISBN-001","Tom"); } }
开启事务:
<!-- 配置事务管理器,并为事务管理器配置数据源!--> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="comboPooledDataSource"></property> </bean> <!-- 开启基于注解的声明式事务功能,需要设置transaction-manager属性--> <!-- 如果 事务管理器的id正好是transaction-manager的默认值transactionManager,则可以省略--> <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> <!--然后在service层的doCash方法上加上:@Transactional注解这样就开启了事务!需要注意的是事务一般是加在service层的!-->
时间: 2024-10-13 02:33:48