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。

posted on 2018-12-18 15:14  wyhluckydog  阅读(291)  评论(0编辑  收藏  举报