当前位置: 首页 > news >正文

十六、请求响应-响应:三层架构-分层解耦

三层架构

三层架构
三层架构

分层解耦介绍

分层解耦介绍
分层解耦介绍

控制反转与依赖注入

分层解耦介绍

声明bean的注解

Bean的声明

声明bean的注解总结

声明bean的注解总结

Bean组件扫描

Bean组件扫描

Bean注入

Bean注入

Bean注入总结

Bean注入总结

代码:

EmpController类(Controller层响应请求处理层)

package com.itheima.Controller;import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.service.impl.EmpServiceA;
import com.itheima.utils.XmlParserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class EmpController {@Autowired//运行时,IOC容器会提供该类型的bean对象,并赋值给该变量-依赖注入private EmpService empService;@RequestMapping("/listEmp")public Result list(){//1.调用service,获取数据List<Emp> empList = empService.listEmp();//3.响应数据return Result.success(empList);}
}

EmpDao接口(获取员工列表数据)

package com.itheima.dao;import com.itheima.pojo.Emp;import java.util.List;public interface EmpDao {//获取员工列表数据public List<Emp> empList();
}

EmpDaoA类(Dao层加载并解析emp.xml,实现数据访问)

package com.itheima.dao.impl;import com.itheima.dao.EmpDao;
import com.itheima.pojo.Emp;
import com.itheima.utils.XmlParserUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;import java.util.List;
//@Component//将当前类交给IOC容器管理,成为IOC容器中的bean
@Repository//将当前类交给IOC容器管理,成为IOC容器中的bean
public class EmpDaoA implements EmpDao {@Overridepublic List<Emp> empList() {//1.加载并解析emp.xmlString file = this.getClass().getClassLoader().getResource("emp.xml").getFile();System.out.println(file);List<Emp> empList = XmlParserUtils.parse(file, Emp.class);return empList;}
}

EmpService接口(获取员工列表)

package com.itheima.service;import com.itheima.pojo.Emp;import java.util.List;public interface EmpService {//获取员工列表public List<Emp> listEmp();
}

EmpServiceA类(Service层实现EmpService接口,调用dao,获取数据,并对数据进行处理)

package com.itheima.service.impl;import com.itheima.dao.EmpDao;
import com.itheima.dao.impl.EmpDaoA;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;import java.util.List;
//@Component//将当前类交给IOC容器管理,成为IOC容器中的bean
@Service//将当前类交给IOC容器管理,成为IOC容器中的bean
public class EmpServiceA implements EmpService {@Autowired//运行时,IOC容器会提供该类型的bean对象,并赋值给该变量-依赖注入private EmpDao empdao;@Overridepublic List<Emp> listEmp() {//1.调用dao,获取数据List<Emp> empList = empdao.empList();// 2.对数据进行转换处理-gender,jobempList.stream().forEach(emp -> {//处理gender 1:男 2:女String gender = emp.getGender();if("1".equals(gender)){emp.setGender("男");} else if ("2".equals(gender)) {emp.setGender("女");}});//处理job-1:讲师,2:班主任,3:就业指导empList.stream().forEach(emp -> {//job-1:讲师,2:班主任,3:就业指导String job = emp.getJob();if("1".equals(job)){emp.setJob("讲师");} else if ("2".equals(job)) {emp.setJob("班主任");}else if ("3".equals(job)) {emp.setJob("就业指导");}});return empList;}
}

SpringbootWebQuickstartApplication(util层启动类–启动springboot工程)

package com.itheima;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//启动类--启动springboot工程
@SpringBootApplication//默认扫描当前包及其子包
public class SpringbootWebQuickstartApplication {public static void main(String[] args) {SpringApplication.run(SpringbootWebQuickstartApplication.class, args);}}

结果

结果

http://www.dtcms.com/a/316843.html

相关文章:

  • 信息安全的概述
  • RabbitMQ延时队列的两种实现方式
  • C++算法竞赛篇(九)字符数组题型讲解
  • 坚鹏:AI智能体软件是知行学成为AI智能体创新应用引领者的抓手
  • uvm-register-backdoor-access
  • SpringBoot AI心理学训练实战
  • 更改CodeBuddy的默认terminal为Git Bash
  • 随机森林算法详解:从集成学习原理到代码实现
  • Java技术栈/面试题合集(11)-设计模式篇
  • java web 未完成项目,本来想做个超市管理系统,前端技术还没学。前端是个简单的html。后端接口比较完善。
  • MySQL内外连接详解
  • 学习笔记-相似度匹配改进2
  • 机器学习——随机森林
  • Python高级编程与实践:Python高级数据结构与编程技巧
  • 【C++】Stack and Queue and Functor
  • C++二级考试核心知识点【内附操作题真题及解析】
  • Juc高级篇:可见性,有序性,cas,不可变,设计模式
  • SpringMVC(一)
  • Design Compiler:布图规划探索(ICC)
  • 《失落王国》v1.2.8中文版,单人或联机冒险的低多边形迷宫寻宝游戏
  • Modbus tcp 批量写线圈状态
  • centos7上如何安装Mysql5.5数据库?
  • 跨域场景下的Iframe事件监听
  • 【机器学习深度学习】模型量化
  • OSPF作业
  • Linux 基础
  • vue3 计算方式
  • GPS信号捕获尝试(上)
  • 【android bluetooth 协议分析 01】【HCI 层介绍 30】【hci_event和le_meta_event如何上报到btu层】
  • 【三个数公因数】2022-10-7