1.打开idea编译器,新建一个项目
2.选择Spring Initializr 勾选Default,完成之后点击【Next】
3.创建项目的文件目录结构以及选择jdk版本信息,设置完成后点击【Next】
4.选择项目所需要的依赖,在Web里面勾选Spring Web
根据自己需要添加数据库和其他依赖
5.修改项目名称,项目存放路径,点击【finish】
6.创建完成之后需要保持网络良好,因为需要加载刚才所选择的依赖(下载依赖可能需要很长时间,一定要在网络良好的情况下载,并且耐心等待) pom.xml文件是用来添加依赖的地方
7.完善springboot+mybatis文件结构 在所示目录下创建文件夹
8.创建配置文件并进行配置
创建这两个yml文件 Application-.yml文件中所需配置代码:
server:
port: 9900
spring:
#配置
datasource:
druid:
tdw:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.177:3306/tdw_test?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
username: root
password: zdzc@2017
schedules: 0/10 * * * * ?
我这边数据源采用配置类,方便以后可能有多数据源(如有多个,可以用多个这样的配置类)
注解@MapperScan是用来扫描dao层mapper类
需要配置对应的model类和xml文件放置位置
@ConfigurationProperties("spring.datasource.druid.tdw") 配置对应的数据源
private static final String MAPPER_LOCATIONS = "classpath*:com/zenchn/demo/dao/mapper/*.xml";(注意:因为我的xml文件不在resourse目录里,所以要加classpath后面要加*号
在pom文件中加入
<build>
<!--配置Maven 对resource文件 过滤 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
可以找到对应的xml文件,不然启动会报错,如果不想加,可以将xml配置文件放到resourse目录)
private static final String TYPE_ALIASES_PACKAGE ="com.zenchn.demo.model";
在启动类上添加这三个注解
@EnableTransactionManagement 开启事务支持
@SpringBootApplication
@EnableScheduling 开启定时任务注解支持
springBoot中yml配置文件如果要支持中文注释,必须转化为utf-8格式,不然启动会报错
我的poml依赖
<?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.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zenchn</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
</dependencies>
<build>
<!--配置Maven 对resource文件 过滤 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<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>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>