首页 > 其他分享 >Spring 02: Spring接管下的三层项目架构

Spring 02: Spring接管下的三层项目架构

时间:2022-08-19 21:12:37浏览次数:62  
标签:02 架构 Spring public User import com example

业务背景

  • 需求:使用三层架构开发,将用户信息导入到数据库中
  • 目标:初步熟悉三层架构开发
  • 核心操作:开发两套项目,对比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

标签:02,架构,Spring,public,User,import,com,example
From: https://www.cnblogs.com/nefu-wangxun/p/16603316.html

相关文章

  • 【2022-08-19】mysql基础知识(六)
    mysql基础知识(六)mysql之视图view什么是视图?视图就是通过查询得到的一张虚拟表,然后保存下来,下次直接进行使用即可。即:将SQL语句的查询结果当做虚拟表保存起来,以后可......
  • 20220819总结
    这次考试太烂了,又没考过Diavolo。T1简单的入门题,先热身。#include<iostream>#defineintlonglong#defineN5001usingnamespacestd;intn,ans;doublea[N]......
  • SpringBoot中调用Kafka
    Kafka实战——在SpringBoot中的应用官网文档链接1.pom引用 <dependency><groupId>org.springframework.kafka</groupId><artifactId>spri......
  • 2022-08-19 田龙跃 JDBC知识
    JAVA链接数据库步骤1.加载驱动2.建立链接3.获取statement语句对象执行sql4.处理结果集5.关闭连接加载驱动Class.forName(驱动名称)建立连接connection=DriverMa......
  • 2022-8-19第一组孙乃宇JDBC学习2
    JDBC的学习Statement的不足:大量的字符串拼接,代码可读性降低。sql注入SQL注入:BUG通过字符串的拼接,可以得到一个恒等的sql语句,可以跳过某些判断。如login("zxcvzx......
  • 2022-08-19 第四组 王佳齐 学习笔记
    思维导图学习笔记PreparedStatement:预编译(预加载)接口2.事务处理可以用来维护数据的完整性。保证sql语句要么全执行,要么全部不执行。1.通过conn获取的对象2.是Stateme......
  • 2022-08-19 记录一下 奥睿科 2.5/3.5英寸双盘位USB3.0硬盘底座 使用感受
    什么?电脑识别不了硬盘???我把京东客服给骂了,再到我写这个随笔的时候,有点心疼那个京东客服。为了扩容,昨天入手了希捷的2t机械家用盘,以及这次的主角奥睿科硬盘底座,简称硬盘盒......
  • 2022-08-19 田龙跃 JAVAWEB项目(小论坛)
    JAVA小项目E-R图个人理解:E-R图中每个实体到我们对java中就是一个domin类,字段就是属性流程图注册流程图登录流程图JSTL标签jstl表达式:表达式的用法也是见名知义(和......
  • 2022暑假集训总结
    2022暑假集训总结收获做了██道题跟着多校联训学的时候,主要收获是学会了一些基本的暴力算法和一些以前不知道的算法概念后来高烧休息了几天回来以后主要的收获是考试......
  • Spring Security登录的流程
    SpringSecurity登录的流程1、UsernamePasswordAuthenticationFilter这过滤器开始attemptAuthentication方法请求的request中的参数setDetails(request,authReque......