大致步骤:

  • 1.创建数据库
  • 2.账户实体类
  • 3.编写持久层代码
  • 4.账户的持久层实现类
  • 5.编写业务层代码
  • 6.业务层实现类
  • 7.配置文件

基本结构

使用 spring 的 IoC 实现账户的 CRUD_spring

使用 spring 的 IoC 实现账户的 CRUD_mybatis_02

1.创建数据库

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);

使用 spring 的 IoC 实现账户的 CRUD_mysql_03

2.账户实体类

package com.itheima.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 +
'}';
}
}

3.编写持久层代码

package com.itheima.Dao;

import com.itheima.domain.Account;

import java.util.List;

/*
* 账户的持久层接口
* */
public interface IAccountDao {
/*
* 查询所有
* */
List<Account> findAllAccount();
/*
* 查询一个
* */
Account findAccountById(Integer accountId);
/*
* 保存
* */
void saveAccount(Account account);
/*
* 更新
* */
void updateAccount(Account account);
/*
* 删除
* */
void deleteAccount(Integer accountId);
}

4.账户的持久层实现类

package com.itheima.Dao.Impl;
/*
* 账户的持久层实现类
* */
import com.itheima.Dao.IAccountDao;
import com.itheima.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 runner;

public void setRunner(QueryRunner runner) {
this.runner = runner;
}

@Override
public List<Account> findAllAccount() {
try {
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
}catch (Exception e){
throw new RuntimeException(e);
}
}

@Override
public Account findAccountById(Integer accountId) {
try {
return runner.query("select * from account where id=?",new BeanHandler<Account>(Account.class),accountId);
}catch (Exception e){
throw new RuntimeException(e);
}
}

@Override
public void saveAccount(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 updateAccount(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 void deleteAccount(Integer accountId) {
try {
runner.update("delete from account where id=?",accountId);
}catch (Exception e){
throw new RuntimeException(e);
}
}
}

5.编写业务层代码

package com.itheima.service;

import com.itheima.domain.Account;

import java.util.List;

/*
* 账户的业务层接口
* */
public interface IAccountService {
/*
* 查询所有
* */
List<Account> findAllAccount();
/*
* 查询一个
* */
Account findAccountById(Integer accountId);
/*
* 保存
* */
void saveAccount(Account account);
/*
* 更新
* */
void updateAccount(Account account);

void deleteAccount(Integer accountId);
}

6.业务层实现类

package com.itheima.service.Impl;

import com.itheima.Dao.IAccountDao;
import com.itheima.domain.Account;
import com.itheima.service.IAccountService;

import java.util.List;

/*
* 账户的业务层实现类
* */
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;

public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}

@Override
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}

@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}

@Override
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}

@Override
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}

@Override
public void deleteAccount(Integer accountId) {
accountDao.deleteAccount(accountId);
}
}

7.配置文件:

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.itheima.service.Impl.AccountServiceImpl">
<!--注入dao-->
<property name="accountDao" ref="accountDao"></property>
</bean>

<!--配置Dao对象-->
<bean id="accountDao" class="com.itheima.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.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy?useSSL=false&serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>

8.测试类:

package com.itheima.test;

import com.itheima.domain.Account;
import com.itheima.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/*
* 使用Junit单元测试,测试我们的配置
* */
public class AccountServiceTest {

@Test
public void testFindAll(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
List<Account> accounts=as.findAllAccount();
for(Account account:accounts){
System.out.println(account);
}

}
@Test
public void testFindOne(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
Account account=as.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave(){
Account account=new Account();
account.setName("test");
account.setMoney(12345F);
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
as.saveAccount(account);
}
@Test
public void testUpdate(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
Account account=as.findAccountById(4);
account.setMoney(23456F);
as.updateAccount(account);
}
@Test
public void testDelete(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
as.deleteAccount(4);
}

}

执行结果:

查询所有:

使用 spring 的 IoC 实现账户的 CRUD_java_04


查询id为1的:

使用 spring 的 IoC 实现账户的 CRUD_spring_05

插入:(id字段在定义时就是自增的)

使用 spring 的 IoC 实现账户的 CRUD_数据库_06

我自己测试已经删除过4了,id自增,所以插入的是5

使用 spring 的 IoC 实现账户的 CRUD_mysql_07


更新:(注意money)

使用 spring 的 IoC 实现账户的 CRUD_java_08

删除id为5的:

使用 spring 的 IoC 实现账户的 CRUD_java_09