天天看点

使用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();
    }
    
}
           

测试结果

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