业务背景

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

  1. Spring 05: 用DI(依赖注入)优化Spring接管下的三层项目架构

    背景 用注解改造前面Spring博客集里(指 Spring 02)Spring接管下的三层项目架构 对前面Spring博客集里(指 Spring 04)@Controller + @Service + ...

  2. Spring Boot -- Idea搭建下搭建web项目

    最近公司准备使用Spring Boot框架,让小瑾先来学习一下,为了表示小瑾的办事效率,小瑾直接先学习用Idea搭建一个Spring Boot项目,哈哈哈,坐等领导夸. 废话不多说了,先来总结一下用I ...

  3. IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架

    项目创建 本项目使用的是IDEA 2016创建. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId, ...

  4. spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途

    Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...

  5. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】02、创建新的SpringBoot项目

    1.创建项目 得到项目架构 2.测试项目Web功能 默认端口为8080,运行后,输入localhost:8080/index即可访问到网页 到这里,项目构建成功!

  6. Spring Boot 学习(一) 快速搭建SpringBoot 项目

    快速搭建一个 Spring Boot 项目 部分参考于<深入实践Spring Boot>.<Spring实战 第四版>与程序猿DD的有关博客. 参考(嘟嘟独立博客):http: ...

  7. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  8. 在Spring tools suite中使用git 共享项目

    我们都在eclipse 和 myeclipse中使用过cvs 和 svn 版本控制工具进行团队开发,今天我学习了另外一种版本控制工具git,下面我演示如何在Spring tools suite中使用g ...

  9. freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建

    今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...

随机推荐

  1. python封装发送邮件类

    import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart i ...

  2. C++调用C#的动态库dll

    以往我们经常是需要使用C#来调用C++的dll,这通过PInvoke就能实现.现在在实际的项目过程中,有时会遇到在C++的项目中调用某个C#的dll来完成特定的某个功能,我们都知道,Native C+ ...

  3. VSCode进一步深入了解学习

    紧接上一章节趁热打铁吧,未关注博主的记得关注哦! VSCode设置 (1)关闭预览模式 我们在 VScode 上打开一个新文件的话会覆盖掉以前的文件,这是因为 VSCode 默认开启了预览模式,预览模 ...

  4. anaconda遇到:Solving environment: failed with initial frozen solve. Retrying with flexible solve.问题

    Solving environment: failed with initial frozen solve. Retrying with flexible solve. 遇到上述问题: 解决方案: # ...

  5. 4. Docker自定义镜像

    下面制作镜像: 此时,验证一下: 以上验证都是成功的,到此就可以把刚才建立并经过刚才运行并验证的镜像包通过各种方式传递给其他人来部署使用了,并且环境肯定是可你统一的.

  6. @vue/cli3+配置build命令构建测试包&正式包

    上一篇博客介绍了vue-cli2.x配置build命令构建测试包和正式包,但现在前端开发vue项目大多数使用新版@vue/cli脚手架搭建vue项目(vue create project-name) ...

  7. JuiceFS V1.0 RC1 发布,大幅优化 dump/load 命令性能, 深度用户不容错过

    各位社区的伙伴, JuiceFS v1.0 RC1 今天正式发布了!这个版本中,最值得关注的是对元数据迁移备份工具 dump/load 的优化. 这个优化需求来自于某个社区重度用户,这个用户在将亿级数 ...

  8. 《ECMAScript 6 入门》【二、变量的解构赋值】(持续更新中……)

    前言: 让我们看下es6的新语法解构,跟模式匹配类似.一.数组的解构赋值 举个例子给多个变量赋值的写法: var a =1;var b =2;var c =3; 需要写多个变量特别麻烦,我们先使用以前 ...

  9. gitlab备份迁移与升级

    升级计划: https://docs.gitlab.com/ee/update/index.html#upgrade-paths 1. 安装gitlab(和源版本必须保持一致) wget https: ...

  10. 论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》

    论文信息 论文标题:Attributed Graph Clustering via Adaptive Graph Convolution论文作者:Xiaotong Zhang, Han Liu, Qi ...