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.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class EmpController { private EmpService empService=new EmpServiceA();http://localhost:8080/listEmp @RequestMapping("/listEmp") public Result list(){ List<Emp> empList=empService.listEmp(); return Result.success(empList);
service层
interface接口:
package com.itheima.service; import com.itheima.pojo.Emp; import java.util.List; public interface EmpService { public List<Emp> listEmp(); }
class:
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(); @Override public List<Emp> listEmp() { List<Emp> empList=empDao.listEmp(); //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("就业指导"); } }); return empList; } }
DAO层
interface接口
package com.itheima.dao; import com.itheima.pojo.Emp; import java.util.List; public interface EmpDao { public List<Emp> listEmp(); }
class:
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 { @Override public List<Emp> listEmp() { //1. 加载并解析emp.xml String file = this.getClass().getClassLoader().getResource("emp.xml").getFile(); System.out.println(file); List<Emp> empList = XmlParserUtils.parse(file, Emp.class); return empList; } }