2, configure a number of data sources (H2 database). 3, configure a number of data sources (Mysql database). 【 attach source 】Spring Boot actual combat series – four, log management 【 attach source 】Spring Boot actual combat series – five, transaction abstraction 【 attach source 】Spring Boot actual combat series – six, currency storage database processing 【 attach source 】Spring Mybatis Generator automatic code generation

Introduction of depend on

  • mybatis-spring-boot-starter
  • mybatis-generator-core
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>
Copy the code

Introduce the MVN plug-in

  • Packaging plug-in
  • Code generation plug-in
<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.3.7</version>
            <configuration>
                <configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
                <overwrite>true</overwrite>
                <includeCompileDependencies>true</includeCompileDependencies>
            </configuration>
        </plugin>
    </plugins>
</build>
Copy the code

Write the code to generate the configuration file mybatis-generator-config.xml

Reference: juejin.cn/post/684490…

<? 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>
    <properties resource="application.properties"/>


    <classPathEntry
            location="C: \ Users \ XiaoMi \. M2 \ repository/mysql/mysql - connector - Java \ 8.0.22 \ mysql connector - Java - 8.0.22. Jar" />
    <context id="context1" targetRuntime="MyBatis3Simple">
        <commentGenerator>
            <! -- Do not want to generate comments -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>


        <jdbcConnection driverClass="${spring.datasource.driverClassName}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
        </jdbcConnection>


        <javaModelGenerator targetPackage="com.qiuyi.springdemo.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>


        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>


        <javaClientGenerator type="XMLMAPPER" targetPackage="com.qiuyi.springdemo.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>


        <! -- List of tables to inspect -->
        <table schema="" tableName="t_coffee" domainObjectName="Coffee" />
    </context>
</generatorConfigura
Copy the code

The results

Double-click mybatis-generator:generate on the right to automatically generate three folders:

  • dao
  • model
  • mapper

Source code download address

Github.com/qiulanzhu/S…