Controller控制层
用于处理前端请求并返回结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @RestController @RequestMapping("/task") public class TaskController { @Resource ITaskService taskService;
@RequestMapping(value = "/getTask", method = RequestMethod.GET) @ResponseBody public RestResult getTask( @Parameter(description = "当前页", required = true) long currentPage, @Parameter(description = "页面数量", required = true) long pageCount){ IPage<TaskVo> taskList = taskService.getTasks(currentPage, pageCount); return ; }
@RequestMapping(value = "/newTask", method = RequestMethod.POST) @ResponseBody public RestResult<String> newTask(@RequestBody NewTaskDTO newTaskDTO) { return ; } }
|
Service服务层
提供业务的处理方法
1 2 3
| public interface ITaskService extends IService<Task> { IPage<TaskVo> getTasks(Long currentPage, Long pageCount); }
|
Serviceimpl: 将mapper层与service层整合
1 2 3 4 5 6 7 8 9 10 11
| @Service public class TaskService extends ServiceImpl<TaskMapper, Task> implements ITaskService { @Resource TaskMapper taskMapper;
@Override public IPage<TaskVo> getTasks(Long currentPage, Long pageCount) { Page<TaskVo> page = new Page<>(currentPage, pageCount); return taskMapper.getTask(page); } }
|
Mapper映射层
数据库持久化操作
- 调用xml执行具体方法:
1 2 3
| public interface TaskMapper extends BaseMapper<Task> { IPage<TaskVo> getTask(Page<?> page); }
|
1 2 3 4 5 6 7 8 9 10 11
| <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.demo.mapper.TaskMapper"> <select id="getTask" parameterType="com.demo.domain.Task" resultType="com.demo.vo.TaskVo"> select * from task </select> </mapper>
|
- 直接在mapper中实现:
1 2 3 4 5
| @Mapper public interface TaskMapper { @Select("select * from task") List<TaskVo> getTask(); }
|
- 项目入口添加mapper扫描的位置:
@MapperScan("com.demo.mapper")
Model实体层
实体类与数据库表对应
1 2 3 4 5 6 7 8 9 10 11
| @Entity @Data @Table(name = "task") public class Task { @Id @Column(columnDefinition="varchar(100) comment '任务ID'") private Long taskId; @Column(columnDefinition="varchar(100) comment '任务名'") private String taskName; }
|
Vo视图对象
封装页面展示数据
1 2 3 4 5
| @Data public class TaskVo { private Long taskId; private String taskName; }
|
Dto数据传输对象
封装数据方便传输
1 2 3 4 5 6 7 8
| @Data public class NewTaskDTO { @Schema(description = "任务Id", required = true) private Long taskId;
@Schema(description = "任务名字", required = true) private String taskName; }
|