1.添加创建数据库的代码(类似于SQL语句)
在test-resource文件中添加 init-schema.sql 数据库语法创建文件
2.pom文件中添加数据库依赖mysql-connector-java和mybatis-spring-boot-starter
<!--添加数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
3.在propertes中添加Spring配置数据库链接地址
#增加配置数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/toutiao?useUnicode=true&\
characterEncoding=utf8&\
useSSl=false
spring.datasource.username=root
spring.datasource.password=huali
mybatis.config-location=classpath:mybatis-config.xml
4.在Java-resource文件中添加配置文件 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="true"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="3000"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<!-- Continue going here -->
</configuration>
5.之后数据库可能会因为版本问题报错
解决方案:添加数据库版本
错误提示:Mon Jan 08 20:25:44 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
警告:按照网上修改sql配置 将&改为& ,并没用什么卵用。
在pom文件中将数据库配置改为:
原来是:
<!--添加数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
现在是:
<!--添加数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.0.8</version>
</dependency>