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。

最新文章

  1. Python异常总结(出处: 鱼C论坛)
  2. AX7: HOW TO USE CLASS EXTENSION METHODS
  3. 通过SharePoint Designer对SharePoint 2010的Master Page进行自定制
  4. (easy)LeetCode 191.Number of 1 Bits
  5. LevelDB源码之四LOG文件
  6. Linux常用命令之sed
  7. Android AES加密算法及事实上现
  8. 打印机服务器搭建 -cups
  9. git-ftp 用git管理ftp空间
  10. [SCOI2007]压缩 区间dp
  11. CASE WHEN 及 SELECT CASE WHEN的用法(转)
  12. Activity的状态保存
  13. 在文件保存中 os.getcwd() os.listdir() os.makedirs() os.mkdir() xx.join() ... 等函数 的使用介绍
  14. js弹出div层,弹出层页面底部出现UL出现一条线问题
  15. 【校招面试 之 网络】第2题 TCP的可靠传输、流量控制、滑动窗口
  16. 难度并不NOIP的NOIP模拟赛
  17. C/C++有效对齐值的确定
  18. 3.2 Spark内置RPC框架
  19. dokuwiki安装部署
  20. Python基础 (上)

热门文章

  1. javascript继承之借用构造函数(二)
  2. VS:error C3872: '0xe044': this character is not allowed in an identifier解决方法
  3. Java 知识点(转)
  4. c++官方文档-class
  5. CUDA C Programming Guide 在线教程学习笔记 Part 8
  6. 基于request.getAttribute与request.getParameter的区别详解
  7. WPF 自定义属性
  8. 树结构之JavaScript
  9. HTML5 Canvas ( 线段的绘制 ) beginPath, moveTo, strokeStyle, stroke
  10. Excel 返回第2大的值