添加依赖
pom.xml文件配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ethan</groupId>
<artifactId>day02_eesy_02account_xmlioc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
类的声明
类的结构如下:
在domain下创建账户的实体类:
package com.ethan.domain;
/**
* @author Ethan
* @date 2020/1/24 - 9:58
*/
import java.io.Serializable;
/**
* 账户的实体类
*/
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Float getMoney() {
return money;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
业务层接口:
package com.ethan.service;
import com.ethan.domain.Account;
import java.util.List;
/**
* @author Ethan
* @date 2020/1/24 - 9:58
* 账户的业务层接口
*/
public interface AccountService {
List<Account> findAllAccount();
Account findAccountById(Integer accountId);
void saveAccount(Account account);
void updateAccount(Account account);
void deleteAccount(Integer accountId);
}
业务层实现类:
package com.ethan.service.impl;
import com.ethan.dao.AccountDAO;
import com.ethan.service.AccountService;
import com.ethan.domain.Account;
import java.util.List;
/**
* @author Ethan
* @date 2020/1/24 - 10:02
*/
public class AccountServiceImpl implements AccountService {
private AccountDAO accountDAO;
public void setAccountDAO(AccountDAO accountDAO) {
this.accountDAO = accountDAO;
}
public List<Account> findAllAccount() {
return accountDAO.findAllAccount();
}
public Account findAccountById(Integer accountId) {
return accountDAO.findAccountById(accountId);
}
public void saveAccount(Account account) {
accountDAO.saveAccount(account);
}
public void updateAccount(Account account) {
accountDAO.updateAccount(account);
}
public void deleteAccount(Integer accountId) {
accountDAO.deleteAccount(accountId);
}
}
持久层接口:
package com.ethan.dao;
import com.ethan.domain.Account;
import java.util.List;
/**
* @author Ethan
* @date 2020/1/24 - 10:04
* 账户的持久层接口
*/
public interface AccountDAO {
List<Account> findAllAccount();
Account findAccountById(Integer accountId);
void saveAccount(Account account);
void updateAccount(Account account);
void deleteAccount(Integer accountId);
}
持久层的实现类:
package com.ethan.dao.impl;
import com.ethan.dao.AccountDAO;
import com.ethan.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;
/**
* @author Ethan
* @date 2020/1/24 - 10:08
*/
public class AccountDAOImpl implements AccountDAO {
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> findAllAccount() {
try {
return runner.query("select * from account", new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Account findAccountById(Integer accountId) {
try {
return runner.query("select * from account where id = ?", new BeanHandler<Account>(Account.class), accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void saveAccount(Account account) {
try {
runner.update("insert into account(name, money)values(? ,?)", account.getName(), account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void updateAccount(Account account) {
try {
runner.update("update account set name=?, money=? where id=?", account.getName(), account.getMoney(), account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void deleteAccount(Integer accountId) {
try {
runner.update("delete from account where id=?", accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
对象配置
在resources
下创建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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<!--配置Service对象-->
<bean id="accountService" class="com.ethan.service.impl.AccountServiceImpl">
<!--注入DAO对象-->
<property name="accountDAO" ref="accountDAO"></property>
</bean>
<!--配置DAO对象-->
<bean id="accountDAO" class="com.ethan.dao.impl.AccountDAOImpl">
<property name="runner" ref="runner"></property>
</bean>
<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.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy?serverTimezone=GMT%2b8"></property>
<property name="user" value="root"></property>
<property name="password" value="0000"></property>
</bean>
</beans>
类的测试
在以下目录创建测试类:
对类的方法进行测试:
package com.ethan.test;
import com.ethan.service.AccountService;
import com.ethan.service.domain.Account;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @author Ethan
* @date 2020/1/24 - 10:37
* 使用Junit单元测试:测试我们的配置
*/
public class AccountServiceTest {
@Test
public void testFindAll() {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
AccountService as = ac.getBean("accountService", AccountService.class);
//3.执行方法
List<Account> accounts = as.findAllAccount();
for (Account account : accounts) {
System.out.println(account);
}
}
@Test
public void testFindOne() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService as = ac.getBean("accountService", AccountService.class);
Account account = as.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave() {
Account account = new Account();
account.setMoney(9999f);
account.setName("Bob");
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService as = ac.getBean("accountService", AccountService.class);
as.saveAccount(account);
}
@Test
public void testUpdate() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService as = ac.getBean("accountService", AccountService.class);
Account account = as.findAccountById(4);
account.setMoney(7000f);
as.updateAccount(account);
}
@Test
public void testDelete() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService as = ac.getBean("accountService", AccountService.class);
as.deleteAccount(4);
}
}
为了在测试类中使用spring容器,导入整合Junit的spring框架坐标:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
使用@RunWith(SpringJUnit4ClassRunner.class)
和@ContextConfiguration
来使用spring容器:
package com.ethan.test;
import com.ethan.service.AccountService;
import com.ethan.domain.Account;
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;
/**
* @author Ethan
* @date 2020/1/24 - 10:37
* 使用Junit单元测试:测试我们的配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
@Autowired
private AccountService as;
@Test
public void testFindAll() {
List<Account> accounts = as.findAllAccount();
for (Account account : accounts) {
System.out.println(account);
}
}
@Test
public void testFindOne() {
Account account = as.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave() {
Account account = new Account();
account.setMoney(9999f);
account.setName("Bob");
as.saveAccount(account);
}
@Test
public void testUpdate() {
Account account = as.findAccountById(3);
account.setMoney(7000f);
as.updateAccount(account);
}
@Test
public void testDelete() {
as.deleteAccount(4);
}
}