常规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 {
@RequestMapping("/listEmp")
public Result list(){
//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("就业指导");
}
});
//3. 响应数据
return Result.success(empList);
}
}
分层
controller负责接受和响应
service负责逻辑处理
dao负责获取数据,数据访问
![](https://i-blog.csdnimg.cn/blog_migrate/ff1a6ee4fd71d1db1f5e8a10661ae79e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0aeff684b43e232d9370c5abdfab6a10.png)
ioc容器控制反转和依赖注入
controller中
service中
dao中
作业
1.常规controller实现
poet.xml
<?xml version="1.0" encoding="UTF-8"?>
<poets>
<poet>
<index>1</index>
<author>陶渊明</author>
<gender>1</gender>
<dynasty>东晋末至南朝宋初期</dynasty>
<title>诗人和辞赋家</title>
<style>古今隐逸诗人之宗</style>
</poet>
<poet>
<index>2</index>
<author>王维</author>
<gender>1</gender>
<dynasty>唐代</dynasty>
<title>诗佛</title>
<style>空灵、寂静</style>
</poet>
<poet>
<index>3</index>
<author>李白</author>
<gender>1</gender>
<dynasty>唐代</dynasty>
<title>诗仙</title>
<style>豪放飘逸的诗风和丰富的想象力</style>
</poet>
<poet>
<index>4</index>
<author>李商隐</author>
<gender>2</gender>
<dynasty>唐代</dynasty>
<title>诗坛鬼才</title>
<style>无</style>
</poet>
<poet>
<index>5</index>
<author>李清照</author>
<gender>2</gender>
<dynasty>宋代</dynasty>
<title>女词人</title>
<style>婉约风格</style>
</poet>
<poet>
<index>6</index>
<author>杜甫</author>
<gender>1</gender>
<dynasty>唐代</dynasty>
<title>诗圣</title>
<style>反映社会现实和人民疾苦</style>
</poet>
<poet>
<index>7</index>
<author>苏轼</author>
<gender>1</gender>
<dynasty>北宋</dynasty>
<title>文学家、书画家,诗神</title>
<style>清新豪健的诗风和独特的艺术表现力</style>
</poet>
</poets>
poet.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>诗人信息</title>
</head>
<link rel="stylesheet" href="element-ui/index.css">
<script src="./js/vue.js"></script>
<script src="./element-ui/index.js"></script>
<script src="./js/axios-0.18.0.js"></script>
<body>
<h1 align="center">员工信息列表展示</h1>
<div id="app">
<el-table :data="tableData" style="width: 100%" stripe border >
<el-table-column prop="index" label="编号" align="center" ></el-table-column>
<el-table-column prop="author" label="姓名" align="center" ></el-table-column>
<el-table-column prop="gender" label="性别" align="center" ></el-table-column>
<el-table-column prop="dynasty" label="朝代" align="center" ></el-table-column>
<el-table-column prop="title" label="头衔" align="center" ></el-table-column>
<el-table-column prop="style" label="风格" align="center" width="400px" ></el-table-column>
</el-table>
</div>
</body>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
</style>
<script>
new Vue({
el: "#app",
data() {
return {
tableData: []
}
},
mounted(){
axios.get('/listPoet').then(res=>{
if(res.data.code){
this.tableData = res.data.data;
}
});
},
methods: {
}
});
</script>
</html>
XmlParserUtilsA
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
public class XmlParserUtilsA {
public static <T> List<T> parse(String file , Class<T> targetClass) {
ArrayList<T> list = new ArrayList<T>(); //封装解析出来的数据
try {
//1.获取一个解析器对象
SAXReader saxReader = new SAXReader();
//2.利用解析器把xml文件加载到内存中,并返回一个文档对象
Document document = saxReader.read(new File(file));
//3.获取到根标签
Element rootElement = document.getRootElement();
//4.通过根标签来获取 user 标签
List<Element> elements = rootElement.elements("poet");
//5.遍历集合,得到每一个 user 标签
for (Element element : elements) {
String index = element.element("index").getText();
//获取 name 属性
String author = element.element("author").getText();
//获取 age 属性
String gender = element.element("gender").getText();
//获取 image 属性
String dynasty = element.element("dynasty").getText();
//获取 gender 属性
String title = element.element("title").getText();
//获取 job 属性
String style= element.element("style").getText();
//组装数据
Constructor<T> constructor = targetClass.getDeclaredConstructor(String.class, String.class, String.class, String.class, String.class, String.class);
constructor.setAccessible(true);
T object = constructor.newInstance(index,author, gender, dynasty, title, style);
list.add(object);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
实体 Poet
package com.itheima.pojo;
public class Poet {
private String index;
private String author;
private String gender;
private String dynasty;
private String title;
private String style;
public Poet(){
}
public Poet(String index, String author, String gender, String dynasty, String title, String style) {
this.index = index;
this.author = author;
this.gender = gender;
this.dynasty = dynasty;
this.title = title;
this.style = style;
}
@Override
public String toString() {
return "Poet{" +
"index=" + index +
", author='" + author + '\'' +
", gender='" + gender + '\'' +
", dynasty='" + dynasty + '\'' +
", title='" + title + '\'' +
", style='" + style + '\'' +
'}';
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDynasty() {
return dynasty;
}
public void setDynasty(String dynasty) {
this.dynasty = dynasty;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
}
PoetContrroller
package com.itheima.controller;
import com.itheima.pojo.Poet;
import com.itheima.pojo.Result;
import com.itheima.utils.XmlParserUtils;
import com.itheima.utils.XmlParserUtilsA;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Objects;
@RestController
public class PoetController {
@RequestMapping("/listPoet")
public Result list() {
//1. 加载并解析poet.xml
String file = this.getClass().getClassLoader().getResource("poet.xml").getFile();
System.out.println(file);
List<Poet> poetList = XmlParserUtilsA.parse(file, Poet.class);
//2. 对数据进行转换处理 - gender
poetList.stream().forEach(poet -> {
//处理 gender 1: 男, 2: 女
String gender = poet.getGender();
if ("1".equals(gender)) {
poet.setGender("男");
} else if ("2".equals(gender)) {
poet.setGender("女");
}
});
//3.返回值
return Result.success(poetList);
}
}
2.分层
PoetController
PoetService
PoetDao
3.控制反转和依赖注入解耦
将dao和service放在容器中,让其类成为bean
在controller和service中分别注入service和dao的依赖
PoetDao
PoetService
PoetController