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();
}
}
測試結果