最终目录构造:
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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>study</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mysql 连贯驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!-- 德鲁伊数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mybatis-generator-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<!-- mybatis-generator.xml 门路 -->
<configurationFile>${basedir}/src/main/resources/mybatis-generator.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
主启动类
package com.study;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.study.dao") // 主动注册 dao 类
public class StudyApplication {public static void main(String[] args) {SpringApplication.run(StudyApplication.class, args);
}
}
RequestMapping 与申请参数接管
get
申请用 @RequestParam
,post
申请用 @RequestBody
package com.study.controller;
import java.util.Map;
import com.study.dao.RecordCatalogDOMapper;
import com.study.pojo.RecordCatalogDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.study.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
@RestController
@RequestMapping("stu")
@Slf4j
public class StuController {
@Autowired // 主动拆卸
private RecordCatalogDOMapper recordCatalogDOMapper;
@GetMapping("{stuId}/get")
/*
* @RequestParam 以后后端参数统一能够省略
* api: http://localhost:8081/stu/54/get?name=wang
*/
public JsonResult getStu(@PathVariable("stuId") String id,
@RequestParam String name) {log.info("stuId=" + id); // stuId=54
log.info("name=" + name); // name=wang
RecordCatalogDO recordCatalogDO = recordCatalogDOMapper.selectByPrimaryKey(1L);
return JsonResult.ok(recordCatalogDO);
}
@PostMapping("create")
public JsonResult createStu(@RequestBody Map<String, Object> map) {log.info("map=" + map.toString()); // map={catalog= 吃饭, name=wang}
return JsonResult.ok(map);
}
}
yml 配置
server:
port: 8081
spring:
datasource:
name: backend
url: jdbc:mysql://127.0.0.1:3306/backend # 数据库
username: root
password: 12345678
type: com.alibaba.druid.pool.DruidDataSource # 连接池
driverClassName: com.mysql.cj.jdbc.Driver
mybatis:
mapperLocations: classpath:mapping/* # map 映射文件,位于 resources 的哪个地位
mybatis-generator.xml
提前建好文件夹
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- context 是逆向工程的次要配置信息 -->
<!-- id:起个名字 -->
<!-- targetRuntime:设置生成的文件实用于那个 mybatis 版本 -->
<context id="default" targetRuntime="MyBatis3">
<!--jdbc 的数据库连贯 -->
<!--jdbc:mysql://localhost:3306/ 数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/backend?serverTimezone=UTC"
userId="root"
password="12345678">
</jdbcConnection>
<!-- targetPackage:生成的实体类所在的包门路 -->
<!-- targetProject:生成的实体类所在的硬盘地位 -->
<!-- DataObject 类寄存地位 -->
<javaModelGenerator targetPackage="com.study.pojo"
targetProject="src/main/java">
<!-- 是否容许子包 -->
<property name="enableSubPackages" value="false"/>
<!-- 是否对 modal 增加构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 是否清理从数据库中查问出的字符串左右两边的空白字符 -->
<property name="trimStrings" value="true"/>
<!-- 建设 modal 对象是否不可扭转 即生成的 modal 对象不会有 setter 办法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- targetPackage 和 targetProject:生成的 mapper 文件的包和地位 -->
<sqlMapGenerator targetPackage="mapping"
targetProject="src/main/resources">
<!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- targetPackage 和 targetProject:生成的 interface 文件的包和地位 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.study.dao" targetProject="src/main/java">
<!-- 针对 oracle 数据库的一个配置,是否把 schema 作为字包名 -->
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 生成对应表及类名 -->
<table tableName="record_catalog" domainObjectName="RecordCatalogDO">
</table>
</context>
</generatorConfiguration>