强调:简化开发,以后DAO可以继承JdbcDaoSupport类
步骤一:创建WEB工程,引入需要的jar包
* IoC的6个包
* AOP的4个包
* C3P0的1个包
* MySQL的驱动包
* JDBC的2个包
* 整合JUnit测试包
步骤二:引入配置文件
* 引入log4j.properties
* 引入applicationContext.xml
步骤三:创建对应的包结构和类
AccountService:
public interface AccountService {
public void pay(String out, String in, double money);
}
AccountDao:
public interface AccountDao {
// 扣钱
public void outMoney(String out, double money);
// 加钱
public void inMoney(String in, double money);
}
步骤四: 引入Spring的配置文件,将类配置到Spring中
<!-- 配置业务层 -->
<bean id="accountService" class="com.jadan.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置持久层 -->
<bean id="accountDao" class="com.jadan.demo1.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
步骤五:在业务层注入DAO,在DAO中注入JDBC模板(简化开发,DAO可以继承JdbcDaoSupport类)
<!-- 配置持久层 -->
<bean id="accountDao" class="com.jadan.demo1.AccountDaoImpl">
<!-- <property name="jdbcTemplate" ref="jdbcTemplate"></property> -->
<property name="dataSource" ref="dataSource"></property>
</bean>
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: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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置C3P0的连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_day03"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
<!-- 配置JDBC的模板类
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
-->
<!-- 配置业务层 -->
<bean id="accountService" class="com.jadan.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置持久层 -->
<bean id="accountDao" class="com.jadan.demo1.AccountDaoImpl">
<!-- <property name="jdbcTemplate" ref="jdbcTemplate"></property> -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
步骤六:编写DAO和Service中的方法
AccountServiceImpl:
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
/**
* 转账的方法
*/
public void pay(String out, String in, double money) {
// 先扣钱
accountDao.outMoney(out, money);
// 模拟异常
//int a = 10/0;
// 然后加钱
accountDao.inMoney(in, money);
}
}
AccountDaoImpl:
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
// 扣钱
public void outMoney(String out, double money) {
this.getJdbcTemplate().update("update t_account set money = money - ? where name = ?", money, out);
}
// 加钱
public void inMoney(String in, double money) {
this.getJdbcTemplate().update("update t_account set money = money + ? where name = ?", money, in);
}
}
步骤七:编写测试程序
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
@Resource(name="accountService")
private AccountService accountService;
@Test
public void run1() {
// 调用支付的方法
accountService.pay("熊大", "小泽", 1000);
}
}