这篇开始,开始进行 SpringBoot 框架功能的具体实现,本篇是 SpringBoot 整合 MyBatis 搭建基本骨架; 项目源码实现后分支地址:https://toscode.gitee.com/li_ziheng/lizhengi-samples/tree/feature%2Fspring-boot-1.0.0/ ~ 本篇内容包括:项目介绍与条件准备、项目初始搭建与构造、效果验证
├── controller --- 控制层(将请求通过 url 匹配,分配到不同的接收器/方法进行处理,然后返回结果)
├── service --- 服务层接口
└── impl --- 服务层实现
├── mapper --- 数据访问层,与数据库交互为 service 提供接口
├── entity --- 实体对象
├── dto --- 持久层需要的实体对象(用于服务层与持久层之间的数据传输对象)
└── vo --- 视图层需要的实体对象(用于服务层与视图层之间的数据传输对象)
└── Application.java --- 入口启动类
# MySQL 中创建相关表
# 人物表
CREATE TABLE `character` (
`character_id` int NOT NULL AUTO_INCREMENT,
`character_name` varchar(36) NOT NULL COMMENT '人物名称',
`character_profile` varchar(255) NOT NULL COMMENT '人物描述',
`character_appearance_date` int DEFAULT NULL COMMENT '人物登场年份 负数表示公元前',
PRIMARY KEY (`character_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci;
# 事件表
CREATE TABLE `event` (
`event_id` int NOT NULL AUTO_INCREMENT,
`event_name` varchar(128) NOT NULL COMMENT '事件名称',
`event_type` varchar(36) NOT NULL COMMENT '事件类型',
`event_is_accurate` tinyint(1) DEFAULT NULL COMMENT '事件事件为精确 0大约/1精确',
`event_is_bc` tinyint(1) DEFAULT NULL COMMENT '事件发生在公元前',
`event_year` varchar(36) DEFAULT NULL COMMENT '事件发生年份',
`event_month` varchar(36) DEFAULT NULL COMMENT '事件发生月份',
`event_day` varchar(36) DEFAULT NULL COMMENT '事件发生日',
`event_location` varchar(128) DEFAULT NULL COMMENT '事件发生地点',
`event_description` text COMMENT '事件描述',
PRIMARY KEY (`event_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci;
# 人物表与事件表的关联关系表(多对多)
CREATE TABLE `event_character` (
`id` int NOT NULL AUTO_INCREMENT,
`character_id` int NOT NULL,
`event_id` int NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci
#MySQL 中构建数据
# 构造数据
INSERT INTO lizhengi.event (event_name, event_type, event_is_accurate, event_is_bc, event_year, event_month, event_day, event_location, event_description) VALUES ('秦始皇统一六国', '历史事件', 1, 1, '221', null, null, '咸阳', '秦始皇统一六国')
INSERT INTO lizhengi.event (event_name, event_type, event_is_accurate, event_is_bc, event_year, event_month, event_day, event_location, event_description) VALUES ('秦始皇焚书坑儒', '历史事件', 1, 1, '212', null, null, '咸阳', '秦始皇焚书坑儒')
INSERT INTO lizhengi.event (event_name, event_type, event_is_accurate, event_is_bc, event_year, event_month, event_day, event_location, event_description) VALUES ('秦朝灭亡', '王朝迭代', 1, 1 '207', null, null, '咸阳', '秦朝灭亡')
INSERT INTO lizhengi.event (event_name, event_type, event_is_accurate, event_is_bc, event_year, event_month, event_day, event_location, event_description) VALUES ('汉朝建立', '王朝迭代', 1, 1, '202', null, null, '洛阳', '汉朝建立')
# 构造数据
INSERT INTO lizhengi.`character` (character_name, character_profile, character_appearance_date) VALUES ('嬴政', '中国第一位皇帝', -259)
INSERT INTO lizhengi.`character` (character_name, character_profile, character_appearance_date) VALUES ('子婴', '秦朝最后一位皇帝', -210)
INSERT INTO lizhengi.`character` (character_name, character_profile, character_appearance_date) VALUES ('项羽', '西楚霸王', -232)
INSERT INTO lizhengi.`character` (character_name, character_profile, character_appearance_date) VALUES ('刘邦', '汉高祖,汉朝开国皇帝', -256)
# 构造数据
lizhengi.event_character (character_id, event_id) VALUES (1, 1)
lizhengi.event_character (character_id, event_id) VALUES (1, 2)
lizhengi.event_character (character_id, event_id) VALUES (2, 3)
lizhengi.event_character (character_id, event_id) VALUES (3, 3)
lizhengi.event_character (character_id, event_id) VALUES (4, 4)
# 使用 Idea 初始化一个 SpringBoot 项目:
# 添加项目 maven 依赖,pom.xml 文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 核心的配置 <parent> 节点 -->
<parent>
<groupId>org.springframework.boot</groupId>
<!-- 它继承自 spring-boot-dependencies,保存了基本的依赖信息(来自spring-boot-dependencies)、项目的编码格式、JDK 的版本、执行打包操作的配置、自动化的资源过滤等信息 -->
<artifactId>spring-boot-starter-parent</artifactId>
<!-- 在这里指定了 spring boot 的版本 -->
<version>2.7.2</version>
<!-- 指定查找该父项目pom.xml的(相对)路径,默认顺序:relativePath > 本地仓库 > 远程仓库 -->
<relativePath/> <!-- lookup parent from repository -->
<!-- 不用parent:可以自行定义 dependencyManagement 节点,然后在里边定义好版本号,再接下来在引用依赖时也就不用写版本号了 -->
</parent>
<groupId>com.lizhengi</groupId>
<artifactId>lizhengi-sample-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lizhengi-sample-spring-boot</name>
<description>lizhengi-sample-spring-boot</description>
<!-- 可以自定义属性值,这些值可以通过 ${ } 用到pom文件内的任何地方,一般用来管理版本 -->
<properties>
<!-- spring-boot-starter-parent 中定义过了,这里为了标识出来 -->
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 支持全栈式 web 开发,包括 Tomcat 和 spring-webmvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 用于整合 JUnit 及相关测试环境 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- 测试范围:scope 为 test 表示依赖项目仅仅参与测试相关的工作,包括测试代码的编译,只能在 src 下的 test 文件夹下面才可以使用 -->
<scope>test</scope>
</dependency>
<!-- 链接 SpringBoot 和 MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- MySQL 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!-- 集成 Druid 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.15</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
添加 yaml 配置信息,application.yml 内容如下:
# 配置服务器信息
server:
port: 8080
spring:
# mysql 数据库相关配置
datasource:
url: jdbc:mysql://localhost:3306/lizhengi?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
# Event-事件 持久层实体对象实现
package com.lizhengi.entity.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @author lizhengi
* @version 1.0.0
* @description Event-事件 持久层实体对象实现
* @date 2022-12-07 3:59 下午
**/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EventDTO {
/**
* 事件ID
*/
private Integer eventId;
/**
* 事件名称
*/
private String eventName;
/**
* 事件类型
*/
private String eventType;
/**
* 事件发生是否为精确时间 0大约/1精确
*/
private Boolean eventIsAccurate;
/**
* 事件发生在公元前 0公元前/公元后
*/
private Boolean eventIsBc;
/**
* 事件发生年份
*/
private String eventYear;
/**
* 事件发生月份
*/
private String eventMonth;
/**
* 事件发生日
*/
private String eventDay;
/**
* 事件地点
*/
private String eventLocation;
/**
* 事件人物
*
* @see CharacterDTO
*/
private List<CharacterDTO> eventCharacters;
/**
* 事件描述
*/
private String eventDescription;
}
# Character-人物 持久层实体对象实现
package com.lizhengi.entity.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author lizhengi
* @version 1.0.0
* @description Character-人物 持久层实体对象实现
* @date 2022-12-07 4:40 下午
**/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CharacterDTO {
/**
* 人物ID
*/
private Integer characterId;
/**
* 人物名称
*/
private String characterName;
/**
* 人物介绍
*/
private String characterProfile;
/**
* 人物登场时间(负数表示公元前)
*/
private Integer characterAppearanceDate;
}
# Event-事件 数据访问层 Mapper 接口
package com.lizhengi.mapper;
import com.lizhengi.entity.dto.EventDTO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author lizhengi
* @version 1.0.0
* @description Event-事件 数据访问层Mapper
* @date 2022-12-07 5:44 下午
**/
@Mapper
public interface EventMapper {
/**
* 全量获取 EventDto 信息
*
* @return List<EventDTO>
*/
List<EventDTO> getEventDtoList();
}
# EventMapper.xml
<?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">
<!-- namespace:改mapper.xml映射文件的唯一标识并且必须和数据处理层的接口的路径相同-->
<mapper namespace="com.lizhengi.mapper.EventMapper">
<resultMap type="com.lizhengi.entity.dto.EventDTO" id="eventDto">
<result column="event_id" property="eventId"/>
<result column="event_name" property="eventName"/>
<result column="event_type" property="eventType"/>
<result column="event_is_accurate" property="eventIsAccurate"/>
<result column="event_is_bc" property="eventIsBc"/>
<result column="event_year" property="eventYear"/>
<result column="event_month" property="eventMonth"/>
<result column="event_day" property="eventDay"/>
<result column="event_location" property="eventLocation"/>
<result column="event_description" property="eventDescription"/>
<!-- 一对一联合查询用 association[类型属性:javaType] , 一对多联合查询用 collection[类型属性:ofType] -->
<collection ofType="com.lizhengi.entity.dto.CharacterDTO" property="eventCharacters">
<result column="character_id" property="characterId"/>
<result column="character_name" property="characterName"/>
<result column="character_profile" property="characterProfile"/>
<result column="character_appearance_date" property="characterAppearanceDate"/>
</collection>
</resultMap>
<select id="getEventDtoList" resultMap="eventDto">
select event.*,`character`.*
from event
left join event_character ec on event.event_id = ec.event_id
left join `character` on ec.character_id = `character`.character_id
</select>
</mapper>
# EventService
/*
* Meituan.com Inc.
* Copyright (c) 2010-2022 All Rights Reserved.
*/
package com.lizhengi.service;
import com.lizhengi.entity.dto.EventDTO;
import java.util.List;
/**
* @author lizhengi
* @version 1.0.0
* @description Event-事件 Service
* @date 2022-12-07 8:11 下午
**/
public interface EventService {
/**
* 全量获取 EventDto 信息
*
* @return List<EventDTO>
*/
List<EventDTO> getEventDtoList();
}
# EventServiceImpl
package com.lizhengi.service.impl;
import com.lizhengi.entity.dto.EventDTO;
import com.lizhengi.mapper.EventMapper;
import com.lizhengi.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author lizhengi
* @version 1.0.0
* @description Event-事件 ServiceImpl
* @date 2022-12-07 8:14 下午
**/
@Service
public class EventServiceImpl implements EventService {
EventMapper mapper;
@Autowired
public void setMapper(EventMapper mapper) {
this.mapper = mapper;
}
/**
* 全量获取 EventDto 信息
*
* @return List<EventDTO>
*/
@Override
public List<EventDTO> getEventDtoList(){
return mapper.getEventDtoList();
}
}
# EventController
package com.lizhengi.controller;
import com.lizhengi.entity.dto.EventDTO;
import com.lizhengi.service.impl.EventServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author liziheng
* @version 1.0.0
* @description Event-事件 Controller
* @date 2022-12-07 8:17 下午
**/
@RestController
@RequestMapping("/api/lizhengi/event")
public class EventController {
EventServiceImpl eventService;
@Autowired
public void setEventService(EventServiceImpl eventService) {
this.eventService = eventService;
}
@RequestMapping(path = {"/list"}, method = RequestMethod.GET)
public List<EventDTO> getEventDtoList(){
return eventService.getEventDtoList();
}
}
使用 Postman 请求 ‘http://localhost:8080/api/lizhengi/event/list’ 接口,验证效果: