Spring 02: xml的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

项目结构

image

实体类

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

测试分析

  • 测试执行流程示意图

image

测试分析
层级变化:界面层 --> 业务逻辑层 --> 数据访问层 --> 业务逻辑层 --> 界面层
对象访问的变化:界面层对象 --> 业务逻辑层接口指向业务逻辑层实现类 --> 数据访问层接口指向数据访问层实现类 --> 数据访问层实现类完成对数据的操作
方法调用变化:界面层对象的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="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>

测试

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
posted @ 2022-08-19 21:06  nefu_wangxun  阅读(410)  评论(0编辑  收藏  举报