mybatisMBG

1.创建空的maven项目,导如下依赖

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>

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

2.创建配置文件

约束文档

<?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">
<?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>
    <!--id:任意名,targetRuntime:固定写法Mybatis3-->
    <context id="testTables" targetRuntime="Mybatis3">

        <!--数据库连接信息-->
        <jdbcConnection
                driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://192.168.180.170:3306/test"
                userId="root"
                password="root"
        ></jdbcConnection>

        <!--实体类配置信息-->
        <javaModelGenerator
                targetPackage="com.liqiliang.entity"
                targetProject="./src/main/java"></javaModelGenerator>
        
        <sqlMapGenerator
                targetPackage="com.liqiliang.mapper"
                targetProject="./src/main/java"></sqlMapGenerator>
        
        <javaClientGenerator
                type="XMLMAPPER"
                targetPackage="com.liqiliang.mapper"
                targetProject="./src/main/java"></javaClientGenerator>

        <table tableName="book" domainObjectName="Book"></table>
    </context>
</generatorConfiguration>

写个启动类

package com.liqiliang.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;

public class Main {

    public static void main(String[] args) {
        ArrayList<String> warings = new ArrayList<String>();
        boolean overwrite = true;
        String genCig = "/generatorConfig.xml";

        File configFile = new File(Main.class.getResource(genCig).getFile());

        ConfigurationParser configurationParser = new ConfigurationParser(warings);

        Configuration configuration = null;

        DefaultShellCallback callback = new DefaultShellCallback(overwrite);

        MyBatisGenerator myBatisGenerator = null;

        try {
            configuration = configurationParser.parseConfiguration(configFile);

            myBatisGenerator = new MyBatisGenerator(configuration,callback,warings);

            myBatisGenerator.generate(null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

然后OK了

posted @ 2021-02-22 23:49  阿亮在努力  阅读(73)  评论(0编辑  收藏  举报