第四次课后作业

一、使用原始的不分层方法

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="rawTableData" style="width: 100%"  stripe border >
        <el-table-column type="index" label="编号" align="center" min-width="20%"></el-table-column>
        <el-table-column prop="name" 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 {
                rawTableData: []
            }
        },
        mounted(){
            axios.get('/listPoet').then(res=>{
                if(res.data.code){
                    this.rawTableData = res.data.data;
                }
            });
        },
        methods: {
        }
    });
</script>
</html>

PoetContraller.java文件

@RestController
public class PoetController{
    @RequestMapping("/listPoet")
    public Result list(){
//        1.加载并解析
        String file= this.getClass().getClassLoader().getResource("poet.xml").getFile();
        System.out.println(file);
        List<Poet> poetList = XmlParserUtils2.parse(file, Poet.class);
        //2.对数据进行转换处理
        poetList.stream().forEach(poet -> {
            //对性别处理
            String gender = poet.getGender();
            if ("1".equals(gender)){
                poet.setGender("男");
            }else if ("2".equals(gender)){
                poet.setGender("女");
            }
        });
        return Result.success(poetList);
    }
}
二、MVC分层实现

PoetDaoA.java文件

package com.wust.dao.impl;

import com.wust.dao.PoetDao;
import com.wust.pojo.Poet;
import com.wust.utils.XmlParserUtils2;

import java.util.List;

public class PoetDaoA implements PoetDao {
    @Override
    public List<Poet> listPoet() {
        String file= this.getClass().getClassLoader().getResource("poet.xml").getFile();
        System.out.println(file);
        List<Poet> poetList = XmlParserUtils2.parse(file, Poet.class);
        return poetList;
    }
}

PoetServiceA.java文件

package com.wust.service.impl;

import com.wust.dao.PoetDao;
import com.wust.dao.impl.PoetDaoA;
import com.wust.pojo.Poet;
import com.wust.service.PoetService;

import java.util.List;

public class PoetServiceA implements PoetService {

    private PoetDao poetDao = new PoetDaoA();

    @Override
    public List<Poet> listPoet() {

        //1.调用Dao,获取数据并赋值给poetList
        List<Poet> poetList = poetDao.listPoet();
        //2.对数据进行转换处理
        poetList.stream().forEach(poet -> {
            //对性别处理
            String gender = poet.getGender();
            if ("1".equals(gender)){
                poet.setGender("男");
            }else if ("2".equals(gender)){
                poet.setGender("女");
            }
        });
        return poetList;
    }
}

PoetContraller.java文件

package com.wust.controller;

import com.wust.pojo.Poet;
import com.wust.pojo.Result;
import com.wust.service.PoetService;
import com.wust.service.impl.PoetServiceA;
import com.wust.utils.XmlParserUtils2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PoetController2 {

    private PoetService poetService =new PoetServiceA();
    @RequestMapping("/listPoet")
    public Result list(){
        //调用Service获取数据
        List<Poet> poetList = poetService.listPoet();
        return Result.success(poetList);
    }
}

三、控制反转与依赖注入方法

在Controller和Service中加入注解@Autowired,在Dao 和 Service中加入Componed注解.

运行结果图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值