码迷,mamicode.com
首页 > 其他好文 > 详细

mybatisMBG

时间:2021-02-23 14:26:33      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:jdbc   resource   数据库连接   api   sqlmap   get   try   img   encoding   

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了

mybatisMBG

标签:jdbc   resource   数据库连接   api   sqlmap   get   try   img   encoding   

原文地址:https://www.cnblogs.com/liqiliang1437/p/14433706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!