Web后端开发-分层解耦
文章目录
- 三层架构
- 分层解耦
- IOC&DI入门
- IOC详解
- DI详解


三层架构
基于上次请求响应的代码,将EmpController做了修改,分层处理
package com.itheima.dao;import com.itheima.pojo.Emp;import java.util.List;public interface EmpDao {public List<Emp> listEmp();
}
package com.itheima.dao.impl;import com.itheima.dao.EmpDao;
import com.itheima.pojo.Emp;
import com.itheima.utils.XmlParserUtils;import java.util.List;public class EmpDaoA implements EmpDao {@Overridepublic List<Emp> listEmp() {//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;}
}
package com.itheima.service;import com.itheima.pojo.Emp;import java.util.List;public interface EmpService {public List<Emp> listEmp();
}
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 java.util.List;public class EmpServiceA implements EmpService {private EmpDao empDao = new EmpDaoA();@Overridepublic List<Emp> listEmp() {//1.调用dao,获取数据List<Emp> empList = empDao.listEmp();//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: 就业指导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;}
}
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class EmpController {private EmpService empService = new EmpServiceA();@RequestMapping("/listEmp")public Result list(){//1.调用service,获取数据List<Emp> empList = empService.listEmp();//3. 响应数据return Result.success(empList);}}
1. 加载并解析emp.xml
//String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
// System.out.println(file);
//List<Emp> empList = XmlParserUtils.parse(file, Emp.class);
//
2. 对数据进行转换处理 - gender, job
// empList.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: 就业指导
//String job = emp.getJob();
// if("1".equals(job)){
// emp.setJob("讲师");
// }else if("2".equals(job)){
// emp.setJob("班主任");
// }else if("3".equals(job)){
// emp.setJob("就业指导");
// }
// });
分层解耦
IOC&DI入门
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);}}
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 java.util.List;@Component //将当前类交给IOC容器管理,成为IOC容器中的bean
public class EmpServiceA implements EmpService {@Autowired//运行时,IOC容器会提供该类型的bean对象,并赋值给该变量 - 依赖注入private EmpDao empDao;@Overridepublic List<Emp> listEmp() {//1.调用dao,获取数据List<Emp> empList = empDao.listEmp();//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: 就业指导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;}
}
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 java.util.List;@Component //将当前类交给IOC容器管理,成为IOC容器中的bean
public class EmpDaoA implements EmpDao {@Overridepublic List<Emp> listEmp() {//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;}
}
若想将实现类从EmpServiceA转为EmpServiceB,只需把A的@Component注释掉,然后给B加上@Component注解即可
package com.itheima.service.impl;import com.itheima.dao.EmpDao;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.List;@Component //将当前类交给IOC容器管理,成为IOC容器中的bean
public class EmpServiceB implements EmpService {@Autowired//运行时,IOC容器会提供该类型的bean对象,并赋值给该变量 - 依赖注入private EmpDao empDao;@Overridepublic List<Emp> listEmp() {//1.调用dao,获取数据List<Emp> empList = empDao.listEmp();//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: 就业指导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;}
}
IOC详解
控制器 指的是 Controller
DI详解