使用spring ioc基于纯xml配置模拟crud

1、需求和技术要求

1.1 需求
实现账户的 CRUD 操作

1.2 技术要求
使用 spring 的 IoC 实现对象的管理,并且使用纯xml配置。
持久层用打印语句模拟

2、环境配置

2.1 maven导入相应的依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.1</version>
    </dependency>

  
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
</dependencies>

3、模拟持久层

// 接口
public interface AccountDao {

    void findAllAccount();

    void findAccountById();

    void saveAccount();

    void deleteAccount();

}

//接口实现
public class AccountDaoImpl implements AccountDao {
    @Override
    public void findAllAccount() {
        System.out.println("查询所有账户");
    }

    @Override
    public void findAccountById() {
        System.out.println("根据id查询账户成功");
    }

    @Override
    public void saveAccount() {
        System.out.println("保存新的账户成功");
    }

    @Override
    public void deleteAccount() {
        System.out.println("删除账户成功");
    }

4、模拟业务层

// 接口
public interface AccountService {

    void findAllAccount();

    void findAccountById();

    void saveAccount();

    void deleteAccount();

}

// 接口实现
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

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

    @Override
    public void findAllAccount() {
        accountDao.findAllAccount();
    }

    @Override
    public void findAccountById() {
        accountDao.findAccountById();
    }

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

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

}

5、编写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">

    <!--配置dao加入ioc容器中-->
    <bean id="accountDao" class="com.yzx.dao.impl.AccountDaoImpl"></bean>

    <!--配置service加入ioc容器中,-->
    <bean id="accountService" class="com.yzx.service.impl.AccountServiceImpl">
        <!--注入持久层-->
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    

</beans>

6、测试crud

public class Demo01 {


    /**
     * 查询所有
     */
    @Test
    public void testFindAllAccount(){
        // 加载配置文件创建容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        // 利用容器获取业务层对象
        AccountService accountService = ac.getBean("accountService", AccountService.class);
        // 执行操作
        accountService.findAllAccount();
    }

    /**
     * 根据id查询账户
     */
    @Test
    public void testFindAccountById(){
        // 加载配置文件创建容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        // 利用容器获取业务层对象
        AccountService accountService = ac.getBean("accountService", AccountService.class);
        // 执行操作
        accountService.findAccountById();
    }

    /**
     * 保存账户
     */
    @Test
    public void testSaveAccount(){
        // 加载配置文件创建容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        // 利用容器获取业务层对象
        AccountService accountService = ac.getBean("accountService", AccountService.class);
        // 执行操作
        accountService.saveAccount();
    }

    /**
     * 删除账户
     */
    @Test
    public void testDeleteAccount(){
        // 加载配置文件创建容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        // 利用容器获取业务层对象
        AccountService accountService = ac.getBean("accountService", AccountService.class);
        // 执行操作
        accountService.deleteAccount();
    }
    
}

测试结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值