1.准备工作
1.在pom.xml文件中引入dom4j的依赖,用于解析XML文件。
<!-- 解析XML -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<!-- 注意:是放在<dependencies></dependencies>标签内 -->
2.引入解析XML的工具类PoetXMLParserUtils、对应的实体类Poet、XML文件poet.xml
PoetXMLParserUtils.java
package com.zy.utils;
import org.dom4j.Document;
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 PoetXmlParserUtils {
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 标签
//注意这里是writer,不是 poet
List<Element> elements = rootElement.elements("writer");
//5.遍历集合,得到每一个 user 标签
for (Element element : elements) {
//获取 author 属性
String number = element.element("number").getText();
//获取 author 属性
String name = element.element("author").getText();
//获取 gender 属性
String gender = element.element("gender").getText();
//获取 dynasty 属性
String dynasty = element.element("dynasty").getText();
//获取 title 属性
String title = element.element("title").getText();
//获取 style 属性
String style = element.element("style").getText();
//组装数据
Constructor<T> constructor = targetClass.getDeclaredConstructor(Integer.class, String.class, String.class, String.class, String.class, String.class);
constructor.setAccessible(true);
T object = constructor.newInstance(Integer.parseInt(number), name, gender, dynasty, title, style);
list.add(object);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
Poet.java
package com.zy.pojo;
public class Poet {
private Integer number;
private String name;
private String gender;
private String dynasty;
private String title;
private String style;
public Poet(){
}
public Poet(Integer number, String name, String gender, String dynasty, String title, String style){
this.number = number;
this.name = name;
this.gender = gender;
this.dynasty = dynasty;
this.title = title;
this.style = style;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
@Override
public String toString() {
return "Poet{" +
"number=" + number +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", dynasty='" + dynasty + '\'' +
", title='" + title + '\'' +
", style='" + style + '\'' +
'}';
}
}
Poet.xml
<?xml version="1.0" encoding="UTF-8" ?>
<writers>
<writer>
<number>1</number>
<author>陶渊明</author>
<!-- 1:男 2:女 -->
<gender>1</gender>
<dynasty>东晋末至南朝宋初期</dynasty>
<title>诗人和辞赋家</title>
<style>古今隐逸诗人之宗</style>
</writer>
<writer>
<number>2</number>
<author>王维</author>
<gender>1</gender>
<dynasty>唐代</dynasty>
<title>诗佛</title>
<style>空灵、寂静</style>
</writer>
<writer>
<number>3</number>
<author>李白</author>
<gender>1</gender>
<dynasty>唐代</dynasty>
<title>诗仙</title>
<style>豪放飘逸的诗风和丰富的想象力</style>
</writer>
<writer>
<number>4</number>
<author>李商隐</author>
<gender>1</gender>
<dynasty>唐代</dynasty>
<title>诗坛鬼才</title>
<style>无</style>
</writer>
<writer>
<number>5</number>
<author>李清照</author>
<gender>2</gender>
<dynasty>宋代</dynasty>
<title>女词人</title>
<style>婉约风格</style>
</writer>
<writer>
<number>6</number>
<author>杜甫</author>
<gender>1</gender>
<dynasty>唐代</dynasty>
<title>诗圣</title>
<style>反映社会现实和人民疾苦</style>
</writer>
<writer>
<number>7</number>
<author>苏轼</author>
<gender>1</gender>
<dynasty>北宋</dynasty>
<title>文学家、书画家,诗神</title>
<style>清新豪健的诗风和独特的艺术表现力</style>
</writer>
</writers>
3.引入静态页面文件,放在resources下的static目录下
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="number" label="编号" align="center" min-width="10%"></el-table-column>
<el-table-column prop="name" label="姓名" align="center" min-width="15%"></el-table-column>
<el-table-column prop="gender" label="性别" align="center" min-width="10%"></el-table-column>
<el-table-column prop="dynasty" label="朝代" align="center" min-width="20%"></el-table-column>
<el-table-column prop="title" label="头衔" align="center" min-width="20%"></el-table-column>
<el-table-column prop="style" label="风格" align="center" min-width="30%"></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>
</html>
2.三种方法
1.常规方法 controlller控制器不分层
package com.zy.controller;
import com.zy.pojo.Poet;
import com.zy.pojo.Result;
import com.zy.utils.PoetXmlParserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@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;
poetList = PoetXmlParserUtils.parse(file, Poet.class);
//2. 对数据进行转换处理
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.按照MVC的分层方式实现,常规java代码方式
三层架构:
controller:接收请求、响应数据 。控制层,接收前端发送的请求,对请求进行处理,并响应数据。
service:逻辑处理。业务逻辑层,处理具体的业务逻辑。
dao:数据访问。数据访问层(Data Acess Object)(持久层),负责数据访问操作,包括数据 的增、删、改、查。
package com.wxw.dao;
import com.wxw.pojo.Poet;
import java.util.List;
public interface PoetDao {
//获取诗人列表数据
public List<Poet> listPoet();
}
package com.wxw.dao.impl;
import com.wxw.dao.PoetDao;
import com.wxw.pojo.Poet;
import com.wxw.utils.PoetXmlParserUtils;
import java.util.List;
public class PoetDaoA implements PoetDao {
@Override
public List<Poet> listPoet() {
//1. 加载并解析poet.xml
String file = this.getClass().getClassLoader().getResource("poet.xml").getFile();
System.out.println(file);
List<Poet> poetList = PoetXmlParserUtils.parse(file, Poet.class);
return poetList;
}
}
package com.wxw.service;
import com.wxw.pojo.Poet;
import java.util.List;
public interface PoetService {
//获取诗人列表
public List<Poet> listPoet();
}
package com.wxw.service.impl;
import com.wxw.dao.PoetDao;
import com.wxw.dao.impl.PoetDaoA;
import com.wxw.pojo.Poet;
import com.wxw.service.PoetService;
import java.util.List;
public class PoetServiceA implements PoetService {
private PoetDao poetDao = new PoetDaoA();
@Override
public List<Poet> listPoet() {
//1. 调用dao,获取数据
List<Poet> poetList = poetDao.listPoet();
//2. 对数据进行转换处理
poetList.stream().forEach(poet -> {
//处理 gender 1:男 2:女
String gender = poet.getGender();
if("1".equals(gender)){
poet.setGender("男");
}else if("2".equals(gender)){
poet.setGender("女");
}
});
return poetList;
}
}
package com.wxw.controller;
import com.wxw.pojo.Poet;
import com.wxw.pojo.Result;
import com.wxw.service.EmpService;
import com.wxw.service.PoetService;
import com.wxw.service.impl.PoetServiceA;
import com.wxw.utils.PoetXmlParserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class PoetController {
private PoetService poetService = new PoetServiceA();
@RequestMapping("/listPoet")
public Result list(){
//1.调用Service,获取数据
List<Poet> poetList = poetService.listPoet();
//3. 响应数据
return Result.success(poetList);
}
}
3.采用控制反转和依赖注入的MVC方式实现。
概念:
内聚:软件中各个功能模块内部的功能联系。
耦合:衡量软件中各个层/模块之间的依赖、关联的程度。
软件设计原则:高内聚低耦合
控制反转:Inversion Of Control,简称IOC。对象的创建控制权由程序自身转移到外部(容器),这种思想称为控制反转。
依赖注入:Dependency Injection,简称DI。容器为应用程序提供运行时,所依赖的资源,称之为依赖注入。
Bean对象:IOC容器中创建、管理的对象,称之为bean。