深圳幻海软件技术有限公司 欢迎您!

Xml的Spring三层项目架构,你学会了吗?

2023-02-28

业务背景需求:使用三层架构开发,将用户信息导入到数据库中目标:初步熟悉三层架构开发核心操作:开发两套项目,对比Spring接管下的三层项目构建和传统三层项目构建的区别注意:本例中的数据访问层,先不连接数据库,只是进行简单数据模拟非Spring接管下的三层项目构建实体类+各访问层实体类:com.exa

业务背景

  • 需求:使用三层架构开发,将用户信息导入到数据库中
  • 目标:初步熟悉三层架构开发
  • 核心操作:开发两套项目,对比Spring接管下的三层项目构建和传统三层项目构建的区别
  • 注意:本例中的数据访问层,先不连接数据库,只是进行简单数据模拟

非Spring接管下的三层项目构建

实体类 + 各访问层

  • 实体类:com.example.pojoUser 实体类User实体类默认含有:无参构造方法 + 全属性的(有参构造方法 + getter,setter方法 + toString方法)
  • 数据访问层:com.example.daoUserMapper.java(接口)UserMapperImpl.java (实现类)
  • 业务逻辑层:com.example.serviceUserService.java (接口)UserServiceImpl.java (实现类)
  • 界面层:com.example.controllerUserController.java

项目结构

实体类

package com.example.pojo;

public class User {
    private String name;
    private int age;
    private String address;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

数据访问层

  • 接口
package com.example.dao;

import com.example.pojo.User;

/**
 * 数据访问层接口
 */
public interface UserMapper {
    //导入用户信息
    int insertUser(User user);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 实现类
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;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

业务逻辑层

  • 接口
package com.example.Service;

import com.example.pojo.User;

/**
 * 业务逻辑层接口
 */
public interface UserService {
    //导入用户数据的功能
    int insertUser(User user);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 实现类
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);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

界面层

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);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

测试

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框架的简单三层架构,运行失败!");
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

输出结果

用户: 荷包蛋, 导入成功!
Spring接管下的简单三层架构,运行成功!

Process finished with exit code 0
  • 1.
  • 2.
  • 3.
  • 4.

测试分析

  • 测试执行流程示意图

测试分析层级变化:界面层 --> 业务逻辑层 --> 数据访问层 --> 业务逻辑层 --> 界面层对象访问的变化:界面层对象 --> 业务逻辑层接口指向业务逻辑层实现类 --> 数据访问层接口指向数据访问层实现类 --> 数据访问层实现类完成对数据的操作方法调用变化:界面层对象的insertUser(User u) --> 业务逻辑层实现类的insertUser(User u) --> 数据访问层实现类的insertUser(User u)

Spring接管下的三层项目构建

对传统三层项目构建的修改:由上述测试分析中"对象访问的变化可知",需要用到的实现类有:UserController,UserServiceImpl,UserMapperImpl在Spring接管下,需要在bean工厂中,注册上述实体类的对象,将原先需要程序员手动创建管理的对象交给Spring框架去接手管理

业务逻辑层

实现类修改为:

//此时的业务逻辑层实现类:不再手动创建数据访问层的对象,交给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);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

界面层

  • 所做修改与对业务逻辑层的实现类的修改类似:
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);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- bean工厂 -->
<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">

    <!-- 注册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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

测试

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接管下的简单三层架构,运行失败!");
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

输出结果

用户: 荷包蛋, 导入成功!
Spring接管下的简单三层架构,运行成功!

Process finished with exit code 0
  • 1.
  • 2.
  • 3.
  • 4.