Spring 02: Spring接管下的三層項目架構
業務背景
- 需求:使用三層架構開發,將用戶資訊導入到資料庫中
- 目標:初步熟悉三層架構開發
- 核心操作:開發兩套項目,對比Spring接管下的三層項目構建和傳統三層項目構建的區別
- 注意:本例中的數據訪問層,先不連接資料庫,只是進行簡單數據模擬
非Spring接管下的三層項目構建
實體類 + 各訪問層
- 實體類:com.example.pojo
- User 實體類
- User實體類默認含有:無參構造方法 + 全屬性的(有參構造方法 + getter,setter方法 + toString方法)
- 數據訪問層:com.example.dao
- UserMapper.java(介面)
- UserMapperImpl.java (實現類)
- 業務邏輯層:com.example.service
- UserService.java (介面)
- UserServiceImpl.java (實現類)
- 介面層:com.example.controller
- UserController.java
項目結構
實體類
package com.example.pojo;
public class User {
private String name;
private int age;
private String address;
}
數據訪問層
- 介面
package com.example.dao;
import com.example.pojo.User;
/**
* 數據訪問層介面
*/
public interface UserMapper {
//導入用戶資訊
int insertUser(User user);
}
- 實現類
package com.example.dao;
import com.example.pojo.User;
/**
* 數據訪問層的實現類
*/
public class UserMapperImpl implements UserMapper{
//模擬用戶資訊導入
@Override
public int insertUser(User user) {
System.out.println("用戶: " + user.getName() + ", 導入成功!");
return 1;
}
}
業務邏輯層
- 介面
package com.example.Service;
import com.example.pojo.User;
/**
* 業務邏輯層介面
*/
public interface UserService {
//導入用戶數據的功能
int insertUser(User user);
}
- 實現類
package com.example.Service.impl;
import com.example.Service.UserService;
import com.example.dao.UserMapper;
import com.example.dao.UserMapperImpl;
import com.example.pojo.User;
/**
* 業務邏輯層實現類
*/
public class UserServiceImpl implements UserService {
//數據訪問層介面指向數據訪問層實現類
UserMapper userMapper = new UserMapperImpl();
@Override
public int insertUser(User user) {
return userMapper.insertUser(user);
}
}
介面層
package com.example.controller;
import com.example.Service.UserService;
import com.example.Service.impl.UserServiceImpl;
import com.example.pojo.User;
/**
* 介面層
*/
public class UserController {
//業務邏輯層介面指向業務邏輯層實現類
UserService userService = new UserServiceImpl();
public int insertUser(User user){
return userService.insertUser(user);
}
}
測試
package com.example.test;
import com.example.controller.UserController;
import com.example.pojo.User;
import org.junit.Test;
public class TestInsert {
//測試非Spring框架的簡單三層架構
@Test
public void testInsertUser(){
UserController userController = new UserController();
int num = userController.insertUser(new User("荷包蛋", 20, "黑河"));
if(num == 1){
System.out.println("非Spring框架的簡單三層架構,運行成功!");
}else{
System.out.println("非Spring框架的簡單三層架構,運行失敗!");
}
}
}
輸出結果
用戶: 荷包蛋, 導入成功!
Spring接管下的簡單三層架構,運行成功!
Process finished with exit code 0
測試分析
- 測試執行流程示意圖
測試分析
層級變化:介面層 –> 業務邏輯層 –> 數據訪問層 –> 業務邏輯層 –> 介面層
對象訪問的變化:介面層對象 –> 業務邏輯層介面指向業務邏輯層實現類 –> 數據訪問層介面指向數據訪問層實現類 –> 數據訪問層實現類完成對數據的操作
方法調用變化:介面層對象的insertUser(User u) –> 業務邏輯層實現類的insertUser(User u) –> 數據訪問層實現類的insertUser(User u)
Spring接管下的三層項目構建
對傳統三層項目構建的修改:由上述測試分析中”對象訪問的變化可知”,需要用到的實現類有:UserController,UserServiceImpl,UserMapperImpl
在Spring接管下,需要在bean工廠中,註冊上述實體類的對象,將原先需要程式設計師手動創建管理的對象交給Spring框架去接手管理
- 在maven項目中添加Spring依賴和applicationContext.xml的操作不再贅述,可參考spring部落格集中Spring 01的部落格
業務邏輯層
- 實現類修改為
//此時的業務邏輯層實現類:不再手動創建數據訪問層的對象,交給Spring容器來管理,新增:setter方法和無參構造函數
package com.example.Service.impl;
import com.example.Service.UserService;
import com.example.dao.UserMapper;
import com.example.pojo.User;
/**
* 業務邏輯層實現類
*/
public class UserServiceImpl implements UserService {
//數據訪問層介面指向數據訪問層實現類
public UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public UserServiceImpl() {
}
@Override
public int insertUser(User user) {
return userMapper.insertUser(user);
}
}
介面層
- 所做修改與對業務邏輯層的實現類的修改類似
package com.example.controller;
import com.example.Service.UserService;
import com.example.pojo.User;
/**
* 介面層
*/
public class UserController {
//業務邏輯層介面指向業務邏輯層實現類
UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public UserController() {
}
public int insertUser(User user){
return userService.insertUser(user);
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- bean工廠 -->
<beans xmlns="//www.springframework.org/schema/beans"
xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 註冊UserMapper實現類對象-->
<bean id="uMapperImpl" class="com.example.dao.UserMapperImpl">
</bean>
<!-- 註冊UserService實現類對象-->
<bean id="uServiceImpl" class="com.example.Service.impl.UserServiceImpl">
<property name="userMapper" ref="uMapperImpl"/>
</bean>
<!-- 註冊UserController對象-->
<bean id="uController" class="com.example.controller.UserController">
<property name="userService" ref="uServiceImpl"/>
</bean>
</beans>
測試
package com.example.test;
import com.example.controller.UserController;
import com.example.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestInsert {
//測試Spring接管下的簡單三層架構
@Test
public void testInsertUser(){
//創建Spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//取出介面層對象
UserController uController = (UserController) applicationContext.getBean("uController");
//調用介面層對象方法
int num = uController.insertUser(new User("荷包蛋", 20, "黑河"));
if(num == 1){
System.out.println("Spring接管下的簡單三層架構,運行成功!");
}else{
System.out.println("Spring接管下的簡單三層架構,運行失敗!");
}
}
}
輸出結果
用戶: 荷包蛋, 導入成功!
Spring接管下的簡單三層架構,運行成功!
Process finished with exit code 0