MyBatis逆向工程及整合SpringBoot

1.逆向工程(Mybatis Generator)
MyBatis GeneratorXML Configuration File Reference(官方配置文件见下图);详细说明:https://www.jianshu.com/p/e09d2370b796
MyBatis逆向工程及整合SpringBoot_第1张图片
项目结构
MyBatis逆向工程及整合SpringBoot_第2张图片
这是我的generatorConfig.xml配置

<?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>
    <!--
    context:生成一组对象的环境
        1.id:必选,上下文id,用于在生成错误时提示
    targetRuntime:
        1,MyBatis3:默认的值,生成基于MyBatis3.x以上版本的内容,包括XXXBySample;
        2,MyBatis3Simple:类似MyBatis3,只是不生成XXXBySample;
    -->
    <context id="DB2Tables" targetRuntime="MyBatis3Simple">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:-->
            <property name="suppressAllComments" value="true" />
            <!--去除时间标志-->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/liuzm?serverTimezone=UTC"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <javaTypeResolver >
            <!--  true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型  -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--  生成的javabean对象 存放的包 和 存放的项目  -->
        <javaModelGenerator targetPackage="com.liuzm.bean" 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>

        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.liuzm.mapper"  targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table  tableName="user" domainObjectName="User" > </table>

    </context>
</generatorConfiguration>

2.整合SpringBoot加入依赖

 <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

3.运行测试

package com.liuzm.test;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MybatisGenerator {

    public static void main(String[] args) throws Exception {
    
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("src/main/resources/generator/generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }

}

运行之后代码就生成了,项目结构见下图
MyBatis逆向工程及整合SpringBoot_第3张图片
亲测通过;注意如果driverClass="com.mysql.jdbc.Driver"运行时会显示已过时;
MyBatis逆向工程及整合SpringBoot_第4张图片

你可能感兴趣的:(MyBatis逆向工程及整合SpringBoot)