javaEEday05-分层解耦-三层架构
This commit is contained in:
@@ -2,6 +2,8 @@ package com.inmind.controller;
|
|||||||
|
|
||||||
import com.inmind.pojo.Emp;
|
import com.inmind.pojo.Emp;
|
||||||
import com.inmind.pojo.Result;
|
import com.inmind.pojo.Result;
|
||||||
|
import com.inmind.service.EmpService;
|
||||||
|
import com.inmind.service.impl.EmpServiceA;
|
||||||
import com.inmind.utils.XmlParserUtils;
|
import com.inmind.utils.XmlParserUtils;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -11,39 +13,15 @@ import java.util.List;
|
|||||||
@RestController
|
@RestController
|
||||||
public class EmpController {
|
public class EmpController {
|
||||||
|
|
||||||
|
//定义业务层对象
|
||||||
|
private EmpService empService = new EmpServiceA();//多态
|
||||||
|
|
||||||
//定义一个获取员工列表的接口
|
//定义一个获取员工列表的接口
|
||||||
@RequestMapping("/listEmp")
|
@RequestMapping("/listEmp")
|
||||||
public Result listEmp(){
|
public Result listEmp(){
|
||||||
//1.加载并解析emp.xml
|
//1.调用业务层,获取数据
|
||||||
//String file = "D:\\workspace_idea\\inmind_web_project260428\\springboot-web-req-resp\\src\\main\\resources\\emp.xml";
|
List<Emp> empList = empService.listEmp();
|
||||||
String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
|
//2.响应数据
|
||||||
List<Emp> emps = XmlParserUtils.parse(file, Emp.class);
|
return Result.success(empList);
|
||||||
//2.对数据进行处理
|
|
||||||
emps.stream().forEach((emp->{
|
|
||||||
String gender = emp.getGender();
|
|
||||||
//处理:1 - 男 2- 女
|
|
||||||
if ("1".equals(gender)) {
|
|
||||||
emp.setGender("男");
|
|
||||||
} else {
|
|
||||||
emp.setGender("女");
|
|
||||||
}
|
|
||||||
|
|
||||||
//处理职位:1:讲师 2:班主任 3:就业指导
|
|
||||||
String job = emp.getJob();
|
|
||||||
switch (job) {
|
|
||||||
case "1":
|
|
||||||
emp.setJob("讲师");
|
|
||||||
break;
|
|
||||||
case "2":
|
|
||||||
emp.setJob("班主任");
|
|
||||||
break;
|
|
||||||
case "3":
|
|
||||||
emp.setJob("就业指导");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
//3.响应数据
|
|
||||||
return Result.success(emps);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.inmind.dao;
|
||||||
|
|
||||||
|
import com.inmind.pojo.Emp;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface EmpDao {
|
||||||
|
//获取员工列表数据
|
||||||
|
List<Emp> listEmp();
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.inmind.dao.impl;
|
||||||
|
|
||||||
|
import com.inmind.dao.EmpDao;
|
||||||
|
import com.inmind.pojo.Emp;
|
||||||
|
import com.inmind.utils.XmlParserUtils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class EmpDaoA implements EmpDao {
|
||||||
|
@Override
|
||||||
|
public List<Emp> listEmp() {
|
||||||
|
//1.加载并解析emp.xml
|
||||||
|
//String file = "D:\\workspace_idea\\inmind_web_project260428\\springboot-web-req-resp\\src\\main\\resources\\emp.xml";
|
||||||
|
String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
|
||||||
|
List<Emp> emps = XmlParserUtils.parse(file, Emp.class);
|
||||||
|
return emps;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.service;
|
||||||
|
|
||||||
|
import com.inmind.pojo.Emp;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
public interface EmpService {
|
||||||
|
//处理员工列表的业务逻辑
|
||||||
|
List<Emp> listEmp();
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.inmind.service.impl;
|
||||||
|
|
||||||
|
import com.inmind.dao.EmpDao;
|
||||||
|
import com.inmind.dao.impl.EmpDaoA;
|
||||||
|
import com.inmind.pojo.Emp;
|
||||||
|
import com.inmind.service.EmpService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class EmpServiceA implements EmpService {
|
||||||
|
//获取dao层的对象,获取数据
|
||||||
|
private EmpDao empDao = new EmpDaoA();
|
||||||
|
@Override
|
||||||
|
public List<Emp> listEmp() {
|
||||||
|
//通过dao层获取数据
|
||||||
|
List<Emp> empList = empDao.listEmp();
|
||||||
|
|
||||||
|
//对数据进行业务处理
|
||||||
|
//2.对数据进行处理
|
||||||
|
empList.stream().forEach((emp->{
|
||||||
|
String gender = emp.getGender();
|
||||||
|
//处理:1 - 男 2- 女
|
||||||
|
if ("1".equals(gender)) {
|
||||||
|
emp.setGender("男");
|
||||||
|
} else {
|
||||||
|
emp.setGender("女");
|
||||||
|
}
|
||||||
|
|
||||||
|
//处理职位:1:讲师 2:班主任 3:就业指导
|
||||||
|
String job = emp.getJob();
|
||||||
|
switch (job) {
|
||||||
|
case "1":
|
||||||
|
emp.setJob("讲师");
|
||||||
|
break;
|
||||||
|
case "2":
|
||||||
|
emp.setJob("班主任");
|
||||||
|
break;
|
||||||
|
case "3":
|
||||||
|
emp.setJob("就业指导");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
return empList;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user