To add a database to a Java project

Keywords: Database MySQL Mybatis Spring

1. Add code to create database (similar to SQL statement)

Add init schema.sql database syntax creation file to test resource file

2. Adding database in POM file depends on MySQL connector Java and mybatis spring boot starter

<!--Add database dependency-->
        <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. Add Spring configuration database link address in properties

#Add configuration database connection address
spring.datasource.url=jdbc:mysql://localhost:3306/toutiao?useUnicode=true&amp;\
  characterEncoding=utf8&amp;\
  useSSl=false
spring.datasource.username=root
spring.datasource.password=huali
mybatis.config-location=classpath:mybatis-config.xml

4. Add the configuration file mybatis-config.xml to the Java resource file

<?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. After that, the database may report errors due to version problems

Solution: add database version

Error prompt: mon Jan 08 20:25:44 CST 2018 warn: establishing SSL connection without server's identity verification is not recommended. Recording 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.

Warning: it's useless to change & amp to &, according to the online modification of sql configuration.
In the pom file, change the database configuration to:

It turned out to be:
<!--Add database dependency-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

Now it's:
<!--Add database dependency-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>5.0.8</version>
        </dependency>

Posted by sergio-pro on Thu, 30 Apr 2020 10:00:21 -0700