1.需求
实现账户的CRUD操作
2.技术要求
使用Spring的IoC实现对象的管理
使用DBAssit作为持久层的解决方案
使用c3p0数据源
1.创建普通maven工程,配置pom.xml,导入依赖jar包坐标
<?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.beizhen</groupId>
<artifactId>spring02</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>5.1.6</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.10</version>
</dependency>
</dependencies>
</project>
2.创建数据库
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)character set utf8 collate utf8_general_ci;
insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);
package com.beizhen.domain;
import java.io.Serializable;
/**
* 账户实体类
* @author : Bei-Zhen
* @date : 2020-11-17 22:31
*/
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 +
'}';
}
}
4.编写持久层代码
package com.beizhen.dao;
import com.beizhen.domain.Account;
import java.util.List;
/**
* 账户的持久层接口
* @author : Bei-Zhen
* @date : 2020-11-17 22:33
*/
public interface IAccountDao {
/**
* 保存账户
* @param account
*/
void save(Account account);
/**
* 删除账户
* @param accountId
*/
void delete(Integer accountId);
/**
* 修改账户
* @param account
*/
void update(Account account);
/**
* 查询所有账户
* @return
*/
List<Account> findAll();
/**
* 根据id查询账户
* @param accountId
* @return
*/
Account findById(Integer accountId);
}
package com.beizhen.dao.impl;
import com.beizhen.dao.IAccountDao;
import com.beizhen.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;
/**
* 账户的持久层实现类
* @author : Bei-Zhen
* @date : 2020-11-17 22:37
*/
public class AccountDaoImpl implements IAccountDao {
private QueryRunner runner;
public void setQueryRunner(QueryRunner runner) {
this.runner = runner;
}
@Override
public void save(Account account) {
try{
runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
} catch (Exception e){
throw new RuntimeException(e);
}
}
@Override
public void delete(Integer accountId) {
try{
runner.update("delete from account where id=?",accountId);
} catch (Exception e){
throw new RuntimeException(e);
}
}
@Override
public void update(Account account) {
try{
runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
} catch (Exception e){
throw new RuntimeException(e);
}
}
@Override
public List<Account> findAll() {
try{
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
} catch (Exception e){
throw new RuntimeException(e);
}
}
@Override
public Account findById(Integer accountId) {
try{
return runner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),accountId);
} catch (Exception e){
throw new RuntimeException(e);
}
}
}
5.编写业务层代码
package com.beizhen.service.impl;
import com.beizhen.dao.IAccountDao;
import com.beizhen.domain.Account;
import com.beizhen.service.IAccountService;
import java.util.List;
/**
* 账户的业务层实现类
* @author : Bei-Zhen
* @date : 2020-11-17 22:58
*/
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void save(Account account) {
accountDao.save(account);
}
@Override
public void delete(Integer accountId) {
accountDao.delete(accountId);
}
@Override
public void update(Account account) {
accountDao.update(account);
}
@Override
public List<Account> findAll() {
return accountDao.findAll();
}
@Override
public Account findById(Integer accountId) {
return accountDao.findById(accountId);
}
}
package com.beizhen.service;
import com.beizhen.domain.Account;
import java.util.List;
/**
* 账户的业务层接口
* @author : Bei-Zhen
* @date : 2020-11-17 22:57
*/
public interface IAccountService {
/**
* 保存账户
* @param account
*/
void save(Account account);
/**
* 删除账户
* @param accountId
*/
void delete(Integer accountId);
/**
* 修改账户
* @param account
*/
void update(Account account);
/**
* 查询所有账户
* @return
*/
List<Account> findAll();
/**
* 根据id查询账户
* @param accountId
* @return
*/
Account findById(Integer accountId);
}
<?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">
</beans>
配置对象
<?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">
<!-- 配置serice-->
<bean id="accountService" class="com.beizhen.service.impl.AccountServiceImpl">
<property name="accountDao" ref="account"></property>
</bean>
<!-- 配置dao-->
<bean id="account" class="com.beizhen.dao.impl.AccountDaoImpl">
<property name="queryRunner" 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>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
测试类代码
package com.beizhen.test;
import com.beizhen.domain.Account;
import com.beizhen.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* 测试springIoC的CRUD
* @author : Bei-Zhen
* @date : 2020-11-17 23:13
*/
public class TestSpringIocCRUD {
@Test
public void testSave(){
Account account = new Account();
account.setName("beizhen");
account.setMoney(1000.0f);
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService",IAccountService.class);
//3.执行方法
as.save(account);
}
@Test
public void testDelete(){
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService",IAccountService.class);
//3.执行方法
as.delete(4);
}
@Test
public void testUpdate(){
Account account = new Account();
account.setId(1);
account.setName("beizhen");
account.setMoney(1000.0f);
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService",IAccountService.class);
//3.执行方法
as.update(account);
}
@Test
public void testfindAll(){
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService",IAccountService.class);
//3.执行方法
List<Account> accounts = as.findAll();
for (Account account : accounts) {
System.out.println(account);
}
}
@Test
public void testfindById(){
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService",IAccountService.class);
//3.执行方法
Account account = as.findById(1);
System.out.println(account);
}
}
查询所有运行结果