需要配置三处。
1.pom.xml本次配置需要有
1.Springboot启动类
2.mybatis连接
3.mysql连接
4.swagger–用于后端数据展示接口
5.配置application.yml切换
<?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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--本项目信息-->
<groupId>com.piesat</groupId>
<artifactId>springboot6</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot6</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!--swagger-->
<swagger.version>1.8.6</swagger.version>
<springfox.version>2.6.1</springfox.version>
<!--swagger-->
</properties>
<dependencies>
<!--data-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--启动类-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
<version>5.3.6</version>
</dependency>
<dependency>
<artifactId>spring-web</artifactId>
<groupId>org.springframework</groupId>
<version>5.3.6</version>
</dependency>
<!--controller生成接口文档-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<!--controller生成接口文档-->
<!--yml切换 4.需要添加yaml的依赖,不然无法启动idea启动(还有另一种办法是下载idea支持的插件,这边不推荐)-->
<!-- <dependency>-->
<!-- <groupId>org.yaml</groupId>-->
<!-- <artifactId>snakeyaml</artifactId>-->
<!-- </dependency>-->
<!--Log4J2-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--maven-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--mybatis-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generatorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<!--yml切换3.添加解析插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>@</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
<!--yml切换-2.开启过滤,用指定的参数替换directory下的文件中的参数,这是在< build >下的-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<!--开启过滤,用指定的参数替换directory下的文件中的参数-->
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<!-- yml切换-1.qi多环境配置方案 -->
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev2</id>
<properties>
<profileactive>dev2</profileactive>
</properties>
<!-- 默认开启这个配置 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<profileactive>dev</profileactive>
</properties>
</profile>
</profiles>
</project>
2.application.xml配置
2.1.application.yml
spring:
profiles:
active: dev2
2.2.application-dev.yml
server:
port: 8088
servlet:
context-path: /dev
spring:
datasource:
url: jdbc:mysql://localhost:3306/bookdemo?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: tiger
driver-class-name: com.mysql.jdbc.Driver
mybatis:
#驼峰命名
map-underscore-to-camel-case: true
#配置xml位置
mapper-locations: classpath:mapping/*.xml
#标注实体类位置
type-aliases-package: com.example.springboot6.bean
3.generatorMapper.xml配置
自动生成mybatis和model代码
<?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>
<!--//驱动包存放的位子-->
<classPathEntry location="D:\software\Idea\jarPackage\mysql-connector-java-8.0.15\mysql-connector-java-8.0.15.jar" />
<context id="context" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/bookdemo"
userId="root" password="tiger" >
</jdbcConnection>
<javaModelGenerator targetPackage="com.example.springboot6.bean" targetProject="src/main/java">
<!--//实体存放的位子-->
<property name="enableSubPackages" value="true"></property>
<property name = "trimStrings" value="false"></property>
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.example.springboot6.mapper" targetProject="src/main/java" >
<!--//实体存放的位子-->
<property name="enableSubPackages" value="false"></property>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.springboot6.mapper" targetProject="src/main/java" >
<!--//实体存放的位子-->
<property name="enableSubPackages" value="false"></property>
</javaClientGenerator>
<!--//表名和实体名-->
<table tableName="book" domainObjectName="Book"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="metadata" domainObjectName="Metadata"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="type" domainObjectName="Type"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
完毕。