Integrating mybatis+generator to automatically generate code

Keywords: Programming Spring Mybatis MySQL log4j

How to integrate Springboot and Mysteries framework, and then use generator to automatically generate mapper, pojo and other files.

1. Introduce the required files into the pom after the new sprongBook project

Database connection driver, mybatis Plus (including mybatis), generator, attention (configuration of generator plug-in in bulider)

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>BootShiroTest</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules><!--Submodule introduction-->
        <module>spring-boot-common</module>
        <module>spring-boot-shiro</module>
    </modules>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>


    <properties>
        <druid.version>1.1.10</druid.version>
        <mysql.version>5.1.38</mysql.version>
        <mssql.version>4.0</mssql.version>
        <oracle.version>11.2.0.3</oracle.version>
        <mybatisplus.version>2.1.9</mybatisplus.version>
        <mybatisplus.spring.boot.version>1.0.5</mybatisplus.spring.boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- mysql drive -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- oracle drive -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>${oracle.version}</version>
        </dependency>
        <!-- mssql drive -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>${mssql.version}</version>
        </dependency>
        <!-- postgresql drive -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!--mybatis plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>${mybatisplus.version}</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>${mybatisplus.spring.boot.version}</version>
        </dependency>


        <!-- log4j. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>

                    <!-- mybatis Automatic Code Generation Tool-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>

    </dependencies>

    <build>
                 <plugins>
                     <plugin>
                         <groupId>org.springframework.boot</groupId>
                         <artifactId>spring-boot-maven-plugin</artifactId>
                     </plugin>
                     <!-- mybatis generator Automatic Generation Code Plug-in -->
                     <plugin>
                         <groupId>org.mybatis.generator</groupId>
                         <artifactId>mybatis-generator-maven-plugin</artifactId>
                         <version>1.3.2</version>
                         <configuration>
                             <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                             <overwrite>true</overwrite>
                             <verbose>true</verbose>
                         </configuration>
                     </plugin>
                 </plugins>
             </build>


</project>

Configure the application.properties file (as required!)

spring.datasource.url= jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.schema=database/import.sql
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5
spring.jpa.database=mysql
mybatis.mapper-locations=classpath:mapping/*.xml


# LOG4J configuration
log4j.rootCategory=INFO,stdout,file
# Log output to file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.file.file=logs/springboot.log 
log4j.appender.file.DatePattern='.'yyyy-MM-dd 
log4j.appender.file.layout=org.apache.log4j.PatternLayout 
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n 

3 Configuration Generator Automatic Generation Code: The location of the file is in the pom.xml file as the location of the plug-in ${basedir}/src/main/resources/generator/generatorConfig.xml

Note that jar paths, package paths, and project path table configurations should not be written incorrectly.

<?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>
    <!-- Database Driver:Select the database driver package on your local hard disk-->
    <classPathEntry  location="G:\JAVA\Repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- Whether to remove automatically generated comments true: Yes, false:no -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--Database Links URL,User name, password -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test_spboot" userId="root" password="123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- Package name and location of the generated model-->
        <javaModelGenerator targetPackage="com.jy.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- Generate the package name and location of the mapping file-->
        <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- generate DAO The package name and location of-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.jy.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- Tables to be generated tableName Is the table name or view name in the database domainObjectName Is the entity class name-->
        <table tableName="resources" domainObjectName="Resources" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="role_resources" domainObjectName="RoleResources" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>


    </context>
</generatorConfiguration>

6. Running after creating database

Code generated

 

Full catalogue of projects:

 

Reference: https://www.cnblogs.com/zslli/p/8779449.html

Posted by fuii_koh on Sun, 31 Mar 2019 14:42:30 -0700