javaEEday05-分层解耦-IOC&DI-入门
This commit is contained in:
@@ -5,6 +5,7 @@ import com.inmind.pojo.Result;
|
||||
import com.inmind.service.EmpService;
|
||||
import com.inmind.service.impl.EmpServiceA;
|
||||
import com.inmind.utils.XmlParserUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -14,7 +15,8 @@ import java.util.List;
|
||||
public class EmpController {
|
||||
|
||||
//定义业务层对象
|
||||
private EmpService empService = new EmpServiceA();//多态
|
||||
@Autowired //运行时,IOC容器会提供该类型的bean对象,并赋值给该变量---依赖注入
|
||||
private EmpService empService;//多态
|
||||
|
||||
//定义一个获取员工列表的接口
|
||||
@RequestMapping("/listEmp")
|
||||
|
||||
@@ -3,9 +3,11 @@ package com.inmind.dao.impl;
|
||||
import com.inmind.dao.EmpDao;
|
||||
import com.inmind.pojo.Emp;
|
||||
import com.inmind.utils.XmlParserUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component //将当前类交给IOC容器管理,成为IOC容器的bean -- 控制反转
|
||||
public class EmpDaoA implements EmpDao {
|
||||
@Override
|
||||
public List<Emp> listEmp() {
|
||||
|
||||
@@ -4,12 +4,15 @@ import com.inmind.dao.EmpDao;
|
||||
import com.inmind.dao.impl.EmpDaoA;
|
||||
import com.inmind.pojo.Emp;
|
||||
import com.inmind.service.EmpService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EmpServiceA implements EmpService {
|
||||
//获取dao层的对象,获取数据
|
||||
private EmpDao empDao = new EmpDaoA();
|
||||
@Autowired
|
||||
private EmpDao empDao;
|
||||
@Override
|
||||
public List<Emp> listEmp() {
|
||||
//通过dao层获取数据
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.inmind.service.impl;
|
||||
|
||||
import com.inmind.dao.EmpDao;
|
||||
import com.inmind.pojo.Emp;
|
||||
import com.inmind.service.EmpService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class EmpServiceB implements EmpService {
|
||||
//获取dao层的对象,获取数据
|
||||
@Autowired
|
||||
private EmpDao empDao;
|
||||
@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