十六、请求响应-响应:三层架构-分层解耦
三层架构
分层解耦介绍
控制反转与依赖注入
声明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);}}