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="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
Spring 02: Spring接管下的三层项目架构的更多相关文章
- Spring 05: 用DI(依赖注入)优化Spring接管下的三层项目架构
背景 用注解改造前面Spring博客集里(指 Spring 02)Spring接管下的三层项目架构 对前面Spring博客集里(指 Spring 04)@Controller + @Service + ...
- Spring Boot -- Idea搭建下搭建web项目
最近公司准备使用Spring Boot框架,让小瑾先来学习一下,为了表示小瑾的办事效率,小瑾直接先学习用Idea搭建一个Spring Boot项目,哈哈哈,坐等领导夸. 废话不多说了,先来总结一下用I ...
- IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架
项目创建 本项目使用的是IDEA 2016创建. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId, ...
- spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途
Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...
- 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】02、创建新的SpringBoot项目
1.创建项目 得到项目架构 2.测试项目Web功能 默认端口为8080,运行后,输入localhost:8080/index即可访问到网页 到这里,项目构建成功!
- Spring Boot 学习(一) 快速搭建SpringBoot 项目
快速搭建一个 Spring Boot 项目 部分参考于<深入实践Spring Boot>.<Spring实战 第四版>与程序猿DD的有关博客. 参考(嘟嘟独立博客):http: ...
- Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来
转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...
- 在Spring tools suite中使用git 共享项目
我们都在eclipse 和 myeclipse中使用过cvs 和 svn 版本控制工具进行团队开发,今天我学习了另外一种版本控制工具git,下面我演示如何在Spring tools suite中使用g ...
- freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建
今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...
随机推荐
- 拭目以待!JNPF .NET将更新.NET 6技术,同时上线 3.4.1 版本
2022年5月30日,福建引迈即将上线JNPF开发平台的.NET 6版本,在产品性能上做了深度优化,且极大的提升了工作效率,加强了对云服务的改进升级,全面提升用户的使用体验. JNPF是一个以PaaS ...
- 使用PowerShell安装MySQL
更新记录 2022年4月16日:本文迁移自Panda666原博客,原发布时间:2021年7月10日. 2022年4月16日:更新MySQL下载链接. 一.说明与准备工作 根据MySQL官网提供的安装M ...
- Node.js amqplib 连接 Rabbitmq 学习笔记
var amqp = require('amqplib'); connect([url, [socketOptions]]) var amqp = require('amqplib/callback_ ...
- 关于使用koa实现线上 https服务
var https=require("https");//https服务var fs= require("fs");var Koa = require('koa ...
- UiPath存在元素Element Exists的介绍和使用
一.Element Exists的介绍 使您能够验证UI元素是否存在,即使它不可见,输出的是一个布尔值 二.Element Exists在UiPath中的使用 1. 打开设计器,在设计库中新建一个Se ...
- 3行python代码翻译70种语言,这个OCR神奇太赞了
写在前面的一些P话: 今天给大家介绍一个超级简单且强大的OCR文本识别工具:easyocr. 这个模块支持70多种语言的即用型OCR,包括中文,日文,韩文和泰文等.完全满足了大家对于语言的要求,不管你 ...
- SpringBoot项目启动org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException解决方法
将Pom文件中的SpringBoot版本调低即可. 我的是调成了2.5.6
- 当在命令行输入"pip install xxx"
当输入"pip install xxx"时发生了什么 不知道你在下载一些包的时候有没有什么疑惑,输入了"pip install xxx" ,系统是如何找到对应的 ...
- 边缘计算 KubeEdge+EdgeMash
简介 KubeEdge是面向边缘计算场景.专为边云协同设计的业界首个云原生边缘计算框架,在 Kubernetes 原生的容器编排调度能力之上实现了边云之间的应用协同.资源协同.数据协同和设备协同等能力 ...
- vue 调用nginx服务跨越的问题
server { listen 80; server_name api.xxxx.com; add_header Access-Control-Allow-Origin '*' ; proxy_set ...