1、常规方式,controlller控制器不分层
poetController
package com.example.demos.web.Controller;
import com.example.demos.web.pojo.Peot;
import com.example.demos.web.utils.XmlParser1Utils;
import com.example.demos.web.pojo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class PoetController {
@RequestMapping("/poetlist1")
public Result listpeot(){
//1. 加载并解析emp.xml
String file = this.getClass().getClassLoader().getResource("poet.xml").getFile();
System.out.println(file);
List<Peot> peotList;
peotList= XmlParser1Utils.parse(file, Peot.class);
peotList.stream().forEach(peot -> {
//处理 gender 1: 男, 2: 女
String gender = peot.getGender();
if("1".equals(gender)){
peot.setGender("男");
}else if("2".equals(gender)){
peot.setGender("女");
}
});
//3. 响应数据
return Result.success(peotList);
}
}
2、按照MVC的分层方式实现,常规java代码方式
PoetController
package com.example.demos.web.Controller;
import com.example.demos.web.pojo.Peot;
import com.example.demos.web.service.PoetService;
import com.example.demos.web.service.impl.PoetServiceA;
import com.example.demos.web.utils.XmlParser1Utils;
import com.example.demos.web.pojo.Result;
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("/poetlist1")
public Result listpeot(){
//1、调用service,获取数据
List<Peot> peotList=poetService.listPoet();
//3. 响应数据
return Result.success(peotList);
}
}
dao 接口
package com.example.demos.web.dao;
import com.example.demos.web.pojo.Peot;
import java.util.List;
public interface Poetdao {
//获取诗人信息
public List<Peot> listPoet();
}
Poetdao
package com.example.demos.web.dao.impl;
import com.example.demos.web.dao.Poetdao;
import com.example.demos.web.pojo.Peot;
import com.example.demos.web.utils.XmlParser1Utils;
import java.util.List;
public class PoetDaoA implements Poetdao {
@Override
public List<Peot> listPoet(){
//1. 加载并解析emp.xml
String file = this.getClass().getClassLoader().getResource("poet.xml").getFile();
System.out.println(file);
List<Peot> peotList;
peotList= XmlParser1Utils.parse(file, Peot.class);
return peotList;
}
}
service接口
package com.example.demos.web.service;
import com.example.demos.web.pojo.Peot;
import java.util.List;
public interface PoetService {
//获取诗人列表
public List<Peot> listPoet();
}
PoetService
import com.example.demos.web.dao.Poetdao;
import com.example.demos.web.dao.impl.PoetDaoA;
import com.example.demos.web.pojo.Peot;
import com.example.demos.web.service.PoetService;
import java.util.Arrays;
import java.util.List;
public class PoetServiceA implements PoetService {
private Poetdao poetdao=new PoetDaoA();
@Override
public List<Peot> listPoet(){
//1、调用dao,获取数据
List<Peot> poetList=poetdao.listPoet();
//2、对数据进行数据转换
poetList.stream().forEach(peot -> {
//处理 gender 1: 男, 2: 女
String gender = peot.getGender();
if("1".equals(gender)){
peot.setGender("男");
}else if("2".equals(gender)){
peot.setGender("女");
}
});
return poetList;
}
}
3、采用控制反转和依赖注入的MVC方式实现。
Controller
package com.example.demos.web.Controller;
import com.example.demos.web.pojo.Peot;
import com.example.demos.web.service.PoetService;
import com.example.demos.web.service.impl.PoetServiceA;
import com.example.demos.web.utils.XmlParser1Utils;
import com.example.demos.web.pojo.Result;
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//程序在运行时,IOC容器会提供该类型的bean对象,并赋值给该变量
private PoetService poetService;
@RequestMapping("/poetlist1")
public Result listpeot(){
//1、调用service,获取数据
List<Peot> peotList=poetService.listPoet();
//3. 响应数据
return Result.success(peotList);
}
}