您好,欢迎来到华拓网。
搜索
您的当前位置:首页Spring学习 一 JDBC配置文件

Spring学习 一 JDBC配置文件

来源:华拓网
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans 
    
    
        ">
    
    <!-- 读取数据库配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 1. 将连接池放入spring容器 -->
    <bean name="dataSource"
        
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <bean name="accountDao" class="com.mon7ey.spring.dao.AccoutnDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <bean name="accountService" class="com.mon7ey.spring.service.AccountServiceImpl">
        <property name="ad" ref="accountDao"></property>
    </bean>

</beans>
dp.properties
jdbc.jdbcUrl=jdbc:mysql:///hib_demo
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
AccoutnDaoImpl
public class AccoutnDaoImpl extends JdbcDaoSupport implements AccountDao {

    @Override
    public void increaseMoney(Integer id, Double money) {
        super.getJdbcTemplate().
            update("update t_account set money = money + ? where id = ?",money,id);
    }

    @Override
    public void decreaseMoney(Integer id, Double money) {
        super.getJdbcTemplate().
        update("update t_account set money = money - ? where id = ?",money,id);

    }

}
AccountServiceImpl
public class AccountServiceImpl implements AccountService {

    private AccountDao ad;
    
    public void setAd(AccountDao ad) {
        this.ad = ad;
    }
    
    @Override
    public void transfer(Integer from, Integer to, Double money) {
        // 减钱
        ad.decreaseMoney(from, money);
        // 加钱
        ad.increaseMoney(to, money);
        
    }
}
image.png

Copyright © 2019- huatuo3.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务