1.XmlParserUtils
package com.example.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 XmlParserUtils { 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 ID = element.element("ID").getText(); String author = element.element("author").getText(); String gender = element.element("gender").getText(); String dynasty= element.element("dynasty").getText(); String title = element.element("title").getText(); 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(ID,author,gender,dynasty,title,style); list.add(object); } } catch (Exception e) { e.printStackTrace(); } return list; } }
2.导入poet.xml文件
3.编写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="ID" label="编号" align="center" min-width="20%"></el-table-column> <el-table-column prop="author" label="姓名" align="center" min-width="20%"></el-table-column> <el-table-column prop="gender" label="性别" align="center" min-width="20%"></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="20%"></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('/poet').then(res=>{ if(res.data.code){ this.tableData = res.data.data; } }); }, methods: { } }); </script> </html>
采用三种方式实现
1、常规方式,controlller控制器不分层
①Poet.java代码
package com.example.pojo; public class Poet { private String ID; private String author; private String gender; private String dynasty; private String title; private String style; @Override public String toString() { return "Poet{" + "ID='" + ID + '\'' + ", author='" + author + '\'' + ", gender='" + gender + '\'' + ", dynasty='" + dynasty + '\'' + ", title='" + title + '\'' + ", style='" + style + '\'' + '}'; } public Poet() { } public Poet(String ID, String author, String gender, String dynasty, String title,String style) { this.ID= ID; this.author = author; this.gender = gender; this.dynasty = dynasty; this.title = title; this.style=style; } public String getID() { return ID; } public void setID(String ID) { this.ID= ID; } 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; } }
②PoetController.java
package com.example.controller; import com.example.pojo.Result; import com.example.pojo.Poet; import com.example.utils.XmlParserUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class PoetController { @RequestMapping("/poet") public Result list() { String file=this.getClass().getClassLoader().getResource("poet.xml").getFile(); System.out.println(file); List<Poet> PoetList= XmlParserUtils.parse(file, Poet.class); PoetList.stream().forEach(poet->{ //处理gender:1.男/2.女 String gender=writer.getGender(); if("1".equals(gender)){ poet.setGender("男"); } else if("2".equals(gender)){ port.setGender("女"); } }); //相应数据 return Result.success(poetList); } }
2、按照MVC的分层方式实现,常规java代码方式
三层架构
①controller层
package com.example.controller; import com.example.pojo.Poet; import com.example.pojo.Result; import com.example.service.PoetService; import com.example.service.impl.PoetServiceA; import com.example.utils.XmlParserUtils; 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("/poet") public Result list(){ List<Poet> poetList = poetService.listpoet(); return Result.success(poetList); } }
②service层
package com.example.service.impl; import com.example.dao.PoetDao; import com.example.dao.impl.PoetDaoA; import com.example.pojo.Poet; import com.example.service.PoetService; import java.util.List; public class PoetServiceA implements PoetService { private PoetDao poetDao = new PoetDaoA(); @Override public List<Poet> listpoet() { List<Poet> poetList = poetDao.poetList(); poetList.stream().forEach(poet -> { String gender = poet.getGender(); if ("1".equals(gender)) { poet.setGender("男"); } else if ("2".equals(gender)) { poet.setGender("女"); } }); return poetList; } }
③dao层
package com.example.dao.impl; import com.example.dao.PoetDao; import com.example.pojo.Poet; import com.example.utils.XmlParserUtils; import java.util.List; public class PoetDaoA implements PoetDao { @Override public List<Poet> poetList() { String file =this.getClass().getClassLoader().getResource("poet.xml").getFile(); System.out.println(file); List<Poet> poetList = XmlParserUtils.parse(file, Poet.class); return poetList; } }
3、采用控制反转和依赖注入的MVC方式实现。
①
package com.example.controller; import com.example.pojo.Poet; import com.example.pojo.Result; import com.example.service.PoetService; import com.example.service.impl.PoetServiceA; import com.example.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 PoetController { @Autowired private PoetService poetService; @RequestMapping("/poet") public Result list(){ List<Poet> poetList = poetService.listpoet(); return Result.success(poetList); } }
②
package com.example.dao.impl; import com.example.dao.PoetDao; import com.example.pojo.Poet; import com.example.utils.XmlParserUtils; import org.springframework.stereotype.Component; import java.util.List; @Component public class PoetDaoA implements PoetDao { @Override public List<Poet> poetList() { String file =this.getClass().getClassLoader().getResource("poet.xml").getFile(); System.out.println(file); List<Poet> poetList = XmlParserUtils.parse(file, Poet.class); return poetList; } }
③
package com.example.service.impl; import com.example.dao.PoetDao; import com.example.dao.impl.PoetDaoA; import com.example.pojo.Poet; import com.example.service.PoetService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; @Component public class PoetServiceA implements PoetService { @Autowired private PoetDao poetDao; @Override public List<Poet> listpoet() { List<Poet> poetList = poetDao.poetList(); poetList.stream().forEach(poet -> { String gender = poet.getGender(); if ("1".equals(gender)) { poet.setGender("男"); } else if ("2".equals(gender)) { poet.setGender("女"); } }); return poetList; } }
④
package com.example.service.impl; import com.example.dao.PoetDao; import com.example.pojo.Poet; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; public class PoetServiceB { @Autowired private PoetDao poetDao; public List<Poet> listpoet() { List<Poet> poetList = poetDao.poetList(); poetList.stream().forEach(poet -> { String gender = poet.getGender(); if ("1".equals(gender)) { poet.setGender("男士"); } else if ("2".equals(gender)) { poet.setGender("女士"); } }); return poetList; } }