前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 spring 的 IoC 实现账户的 CRUD

使用 spring 的 IoC 实现账户的 CRUD

作者头像
别团等shy哥发育
发布2023-02-27 10:47:36
1840
发布2023-02-27 10:47:36
举报
文章被收录于专栏:全栈开发那些事
大致步骤:
  • 1.创建数据库
  • 2.账户实体类
  • 3.编写持久层代码
  • 4.账户的持久层实现类
  • 5.编写业务层代码
  • 6.业务层实现类
  • 7.配置文件

基本结构

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.创建数据库

代码语言:javascript
复制
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);
在这里插入图片描述
在这里插入图片描述

2.账户实体类

代码语言:javascript
复制
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.编写持久层代码

代码语言:javascript
复制
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.账户的持久层实现类

代码语言:javascript
复制
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.编写业务层代码

代码语言:javascript
复制
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.业务层实现类

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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&amp;serverTimezone=UTC"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
</beans>

8.测试类:

代码语言:javascript
复制
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);
    }

}

执行结果:

查询所有:

在这里插入图片描述
在这里插入图片描述

查询id为1的:

在这里插入图片描述
在这里插入图片描述

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

在这里插入图片描述
在这里插入图片描述

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

在这里插入图片描述
在这里插入图片描述

更新:(注意money)

在这里插入图片描述
在这里插入图片描述

删除id为5的:

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-08-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 大致步骤:
  • 基本结构
  • 1.创建数据库
  • 2.账户实体类
  • 3.编写持久层代码
  • 4.账户的持久层实现类
  • 5.编写业务层代码
  • 6.业务层实现类
  • 7.配置文件:
  • 8.测试类:
  • 执行结果:
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档