完成转账代码的编写
service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void pay(String out, String in, Double money) {
accountDao.outMoney(out,money);
accountDao.inMoney(in,money);
}
}
dao
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void outMoney(String out, double money) {
jdbcTemplate.update("update account set money = money - ? where name = ?",money,out);
}
public void inMoney(String in, double money) {
jdbcTemplate.update("update account set money = money + ? where name = ?",money,in);
}
}
配置文件
<!--第二种写法:使用提供标签的方式-->
<context:property-placeholder location="classpath:jd.properties"/>
<!--加载属性的文件(使用开源连接池)-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置jdbc模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置 service-->
<bean id="accountService" class="com.qcby.demo2.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--配置 dao-->
<bean id="accountDao" class="com.qcby.demo2.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Demo1 {
@Autowired
private AccountService accountService;
@Test
public void run1(){
accountService.pay("熊大","熊二",10.0);
}
}
Dao编写的方式(第二种方式)
service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void pay(String out, String in, Double money) {
accountDao.outMoney(out,money);
accountDao.inMoney(in,money);
}
}
dao
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
//付款
public void outMoney(String out, double money) {
this.getJdbcTemplate().update("update account set money = money-? where name = ?",money,out);
}
public void inMoney(String in, double money) {
this.getJdbcTemplate().update("update account set money = money + ? where name = ?",money,in);
}
}
配置文件编写
<!--第二种写法:使用提供标签的方式-->
<context:property-placeholder location="classpath:jd.properties"/>
<!--加载属性的文件(使用开源连接池)-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置 service-->
<bean id="accountService" class="com.qcby.demo2.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--配置 dao-->
<bean id="accountDao" class="com.qcby.demo2.AccountDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
测试方法
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Demo1 {
@Autowired
private AccountService accountService;
@Test
public void run1(){
accountService.pay("熊大","熊二",10.0);
}
}