一、整體架構
二、代碼
第一步:
編寫實體類(domain) Account.java 並實現序列化
package cn.lemon.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 +
'}';
}
}
第二步:
編寫持久層(dao) IAccountDao.java接口以及實現類
package cn.lemon.dao;
import cn.lemon.domain.Account;
import java.util.List;
public interface IAccountDao {
/**
* 添加數據
*
* @param account
*/
void addAccount(Account account);
/**
* 修改數據
*
* @param account
*/
void updateAccount(Account account);
/**
* 刪除數據
*
* @param accountid
*
*/
void deleteAccount(Integer accountid);
/**
* 根據 id 查詢數據
*
* @param accountId
*/
Account findAccountById(Integer accountId);
/**
* 查詢所有
*
* @return list 集合
*/
List<Account> findAllAccount();
}
package cn.lemon.dao.impl;
import cn.lemon.dao.IAccountDao;
import cn.lemon.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.List;
public class AccountDaoImpl implements IAccountDao {
private QueryRunner queryRunner;
public void setQueryRunner(QueryRunner queryRunner) {//set方法
this.queryRunner = queryRunner;
}
@Override
public void addAccount(Account account) {
try {
queryRunner.update("insert into account (name,money) value (?,?)",account.getName(),account.getMoney());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void updateAccount(Account account) {
try {
queryRunner.update("update account set name = ? ,money = ? where id = ?",account.getName(),account.getMoney(),account.getId());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void deleteAccount(Integer accountid) {
try {
queryRunner.update("delete from account where id = ?",accountid);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public Account findAccountById(Integer accountId) {
try {
return queryRunner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),accountId);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public List<Account> findAllAccount() {
try {
return queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
第三步:
編寫業務層(service) IAccountService.java接口以及實現類
package cn.lemon.service;
import cn.lemon.domain.Account;
import java.util.List;
public interface IAccountService {
/**
* 添加數據
*
* @param account
*/
void addAccount(Account account);
/**
* 修改數據
*
* @param account
*/
void updateAccount(Account account);
/**
* 刪除數據
*
* @param accountid
*
*/
void deleteAccount(Integer accountid);
/**
* 根據 id 查詢數據
*
* @param accountId
*/
Account findAccountById(Integer accountId);
/**
* 查詢所有
*
* @return list 集合
*/
List<Account> findAllAccount();
}
package cn.lemon.service.impl;
import cn.lemon.dao.IAccountDao;
import cn.lemon.domain.Account;
import cn.lemon.service.IAccountService;
import java.util.List;
public class AccountServiceImpl implements IAccountService {
private IAccountDao iAccountDao;
public void setiAccountDao(IAccountDao iAccountDao) {//set 方法
this.iAccountDao = iAccountDao;
}
@Override
public void addAccount(Account account) {
iAccountDao.addAccount(account);
}
@Override
public void updateAccount(Account account) {
iAccountDao.updateAccount(account);
}
@Override
public void deleteAccount(Integer accountid) {
iAccountDao.deleteAccount(accountid);
}
@Override
public Account findAccountById(Integer accountId) {
return iAccountDao.findAccountById(accountId);
}
@Override
public List<Account> findAllAccount() {
return iAccountDao.findAllAccount();
}
}
第四步:
創建並編寫配置文件 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置Service -->
<bean id="accountService" class="cn.lemon.service.impl.AccountServiceImpl">
<!-- 注入dao -->
<property name="iAccountDao" ref="accountDao"></property>
</bean>
<!--配置Dao對象-->
<bean id="accountDao" class="cn.lemon.dao.impl.AccountDaoImpl">
<!-- 注入QueryRunner -->
<property name="queryRunner" ref="queryRenner"></property>
</bean>
<!--配置QueryRunner-->
<bean id="queryRenner" 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>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_account?serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value="lemon"></property>
</bean>
</beans>
第五步:
編寫測試類Client.java 測試 CURD
package cn.lemon.ui;
import cn.lemon.domain.Account;
import cn.lemon.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/**
* 使用Junit單元測試:測試我們的配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Client {
@Autowired
private IAccountService as;
@Test
public void testFindAll() {
//3.執行方法
List<Account> accounts = as.findAllAccount();
for(Account account : accounts){
System.out.println(account);
}
}
@Test
public void testFindOne() {
//3.執行方法
Account account = as.findAccountById(1);
System.out.println(account);
}
@Test
public void testAdd() {
Account account = new Account();
account.setName("test");
account.setMoney(12345f);
//3.執行方法
as.addAccount(account);
}
@Test
public void testUpdate() {
//3.執行方法
Account account = as.findAccountById(4);
account.setMoney(23456f);
as.updateAccount(account);
}
@Test
public void testDelete() {
//3.執行方法
as.deleteAccount(6);
}
}
三、總結
通過上面的測試類,我們可以看出,每個測試方法都重新獲取了一次 spring 的核心容器,造成了不必要的重複代碼,增加了我們開發的工作量。這種情況,在開發中應該避免發生。
能不能測試時直接就編寫測試方法,而不需要手動編碼來獲取容器呢?
答案是可以的,詳情請點擊Spring基於註解的IoC配置