搭建事務管理轉賬案例的環境(強調:簡化開發,以後DAO可以繼承JdbcDaoSupport類)
阿新 • • 發佈:2018-12-18
1. 步驟一:建立WEB工程,引入需要的jar包 * IOC的6個包 * AOP的4個包 * C3P0的1個包 * MySQL的驅動包 * JDBC目標2個包 * 整合JUnit測試包
2.步驟二:建立資料庫的表結構
create database spring_day03; use spring_day03; create table t_account( id int primary key auto_increment, name varchar(20), money double );
3.步驟三:引入配置檔案
* 引入配置檔案
* 引入log4j.properties
4. 步驟四:引入applicationContext.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:jdbc="http://www.springframework.org/schema/jdbc"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> </beans>
* 使用C3P0連線池
* 先引入C3P0的jar包 * com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar * 編寫配置檔案 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean>
5.步驟五:建立對應的包結構和類
* com.huida.demo1
* AccountService
package com.huida.demo1; public interface AccountService { public void pay(String out,String in,double money); }
* AccountServlceImpl
package com.huida.demo1; import javax.annotation.Resource; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; public class AccountServiceImpl implements AccountService{ @Resource(name="accountDao") private AccountDaoImpl accountDao; public void setAccountDao(AccountDaoImpl accountDao) { this.accountDao = accountDao; } @Override public void pay(String out,String in,double money) { //扣錢 accountDao.outMoney(out, money); //加錢 accountDao.inMoney(in, money); } }
* AccountDao
package com.huida.demo1; public interface AccountDao { public void outMoney(String out,double money); public void inMoney(String in,double money); }
* AccountDaoImpl
package com.huida.demo1; import org.springframework.jdbc.core.support.JdbcDaoSupport; public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{ @Override public void outMoney(String out, double money) { this.getJdbcTemplate().update("update user set money = money - ? where name = ?", money,out); } @Override public void inMoney(String in, double money) { this.getJdbcTemplate().update("update user set money = money + ? where name=?",money,in); } }
6.步驟六:引入Spring的配置檔案,將類配置到Spring中
<bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
</bean>
<bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
</bean>
7.步驟七:在業務層注入DAO ,在DAO中注入JDBC模板(強調:簡化開發,以後DAO可以繼承JdbcDaoSupport類)
<
bean id="accountService" class="com.huida.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean> <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean>
8.所以總的applicationContext2.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:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean> <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="accountService" class="com.huida.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean> </beans>
9.步驟八:編寫測試程式.
package com.huida.demo1; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext2.xml") public class Demo1 { @Resource(name="accountService") private AccountService accountService; @Test public void run1(){ accountService.pay("小明","小紅",1000); } }
10.單元測試run1方法,重新整理spring-day03資料庫中的user表,可以看到小明同學的錢減少了1000,小紅同學的錢增加了1000。