1.bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置Service-->
<bean id="accountService" class="com.service.Impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置Dao-->
<bean id="accountDao" class="com.dao.Impl.AccountDaoImpl">
<!--注入QueryRunner-->
<property name="runner" ref="runner"></property>
</bean>
<!--配置QueryRunner对象-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--注入数据源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
</bean>
</beans>
2.com.service.Impl.AccountServiceImpl
package com.service.Impl;
import com.dao.IAccountDao;
import com.domain.Account;
import com.service.IAccountService;
import java.util.List;
public class AccountServiceImpl implements IAccountService{
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
public Account findAccountById(Integer id) {
return accountDao.findAccountById(id);
}
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
public void deleteAccount(Integer id) {
accountDao.deleteAccount(id);
}
}
3.com.service.IAccountService
package com.service;
import com.domain.Account;
import java.util.List;
/*
账户的业务层接口
*/
public interface IAccountService {
//查询所有
List<Account> findAllAccount();
//查询一个
Account findAccountById(Integer id);
//保存账户
void saveAccount(Account account);
//更新
void updateAccount(Account account);
//删除
void deleteAccount(Integer id);
}
4.com.dao.impl.AccountDaoImpl
package com.dao.Impl;
import com.dao.IAccountDao;
import com.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class AccountDaoImpl implements IAccountDao{
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> findAllAccount() {
try {
String sql = "select * from account";
return runner.query(sql,new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException("运行出错");
}
}
public Account findAccountById(Integer id) {
try {
String sql = "select * from account where id = ?";
return runner.query(sql,new BeanHandler<Account>(Account.class),id);
} catch (SQLException e) {
throw new RuntimeException("运行出错");
}
}
public void saveAccount(Account account) {
try {
String sql = "insert into Account(name,money)values(?,?)";
runner.update(sql,account.getName(),account.getMoney());
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateAccount(Account account) {
try {
String sql = "update account set name = ?,money = ? where id=?";
runner.update(sql,account.getName(),account.getMoney(),account.getId());
} catch (SQLException e) {
throw new RuntimeException("运行出错");
}
}
public void deleteAccount(Integer id) {
try {
String sql = "delete from account where id = ?";
runner.update(sql,id);
} catch (SQLException e) {
throw new RuntimeException("运行出错");
}
}
}
5.com.dao.IAccountDao
package com.dao;
import com.domain.Account;
import java.util.List;
public interface IAccountDao {
//查询所有
List<Account> findAllAccount();
//查询一个
Account findAccountById(Integer id);
//保存账户
void saveAccount(Account account);
//更新
void updateAccount(Account account);
//删除
void deleteAccount(Integer id);
}
6.com.domain.Account
package com.domain;
import java.io.Serializable;
/*
账户的实体类
*/
public class Account implements Serializable{
private Integer id;
private String name;
private Float money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
7.com.test.AccountServiceTest
package com.test;
import com.domain.Account;
import com.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
//使用Junit单元测试,测试我们的配置
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:bean.xml"})
public class AccountServiceTest {
@Autowired
private IAccountService accountService;
@Test
public void testFindAll(){
List<Account> accounts = accountService.findAllAccount();
for(Account account : accounts){
System.out.println(account);
}
}
@Test
public void testFindOne(){
Account account = accountService.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave(){
Account account = new Account();
account.setName("ddd");
account.setMoney(4000.0f);
accountService.saveAccount(account);
}
@Test
public void testUpdate(){
Account account = new Account();
account.setId(4);
account.setName("eee");
account.setMoney(4000.0f);
accountService.updateAccount(account);
}
@Test
public void testDelete(){
accountService.deleteAccount(4);
}
}