MyBatis自动生成工具

自动生成工具是一个为MyBatis提供接口生成的工具。可以自动导入MyBatis的映射信息。

自动生成,支持3种方式:

  1. 自主研发(支持MySqlPostgreSql数据库)
  2. 通用mapper(支持mybatis的所有数据库)
  3. mybatis官方方式

引入自动生成工具

<dependency>
   <groupId>com.wueasy</groupId>
   <artifactId>wueasy-auto</artifactId>
   <version>最新版本</version>
   <scope>test</scope>
</dependency>

自动生成处理

自主研发生成工具

目前自主研发的生成mybatis信息,只支持MySqlPostgreSql数据库,生成的接口更全面。

数据库连接配置

修改数据库连接配置的信息

wueasy:
  auto:
    type: 2 #类型,1 通用mapper,2 原始方式,默认1
  data:
    datasource :
      demo :
        mybatis:
          basePackage: com.wueasy.auto.mapper
          mapperLocations: classpath*:mybatis/wueasy/auto/*.xml
        connection:
          url : jdbc:mysql://localhost:3306/easy_data?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8
          username : easyadmin
          password : easyadmin
          #连接池配置
          initialSize : 10  #初始化大小
          minIdle : 10  #最小连接
          maxActive : 20  #最大连接
          maxWait : 60000  #配置获取连接等待超时的时间
          timeBetweenEvictionRunsMillis : 60000  #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
          minEvictableIdleTimeMillis : 300000  #配置一个连接在池中最小生存的时间,单位是毫秒
          maxEvictableIdleTimeMillis : 600000  #配置一个连接在池中最大生存的时间,单位是毫秒
          validationQuery : SELECT 1 
          testWhileIdle : true
          testOnBorrow : false
          testOnReturn : false
          keepAlive : true

执行类

执行后,将会按照配置生成接口文件。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=WueasyApplicationAuto.class)
public class ApplicationTests {

    @Autowired
    private AutoCodeService autoCodeService;

    /**
     * @author: fallsea
     */
    @Test
    public void testObj() {
        String tableName = "sys_log"; //表名称
        String entityPackage = "com.wueasy.admin.entity"; //实体包bean路径
        String mapperPackage = "com.wueasy.admin.mapper"; //mapper包路径
        String mapperXmlPackage = "mybatis.mapper.wueasy.sys"; //mapper配置文件包路径
        autoCodeService.auto(tableName, entityPackage, mapperPackage, mapperXmlPackage);
    }

}

通用mapper生成工具

通用Mapper代码生成器,参数文档地址:https://github.com/abel533/Mapper/wiki/4.1.mappergenerator

配置信息

新增一个配置文件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 id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="javaFileEncoding" value="UTF-8"/>
        <property name="useMapperCommentGenerator" value="true"/>

        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
            <property name="caseSensitive" value="true"/>
            <property name="forceAnnotation" value="false"/>
            <property name="generateColumnConsts" value="false"/>
            <property name="generateDefaultInstanceMethod" value="false"/>
            <property name="lombok" value="Data"/>
        </plugin>

        <!--通用代码生成器插件-->
        <!--mapper接口-->
        <plugin type="tk.mybatis.mapper.generator.TemplateFilePlugin">
            <property name="targetProject" value="src/test/java"/>
            <property name="targetPackage" value="test.mapper"/>
            <property name="templatePath" value="generator/mapper.ftl"/>
            <property name="mapperSuffix" value="Mapper"/>
            <property name="fileName" value="${tableClass.shortClassName}${mapperSuffix}.java"/>
        </plugin>
        <!--mapper.xml-->
        <plugin type="tk.mybatis.mapper.generator.TemplateFilePlugin">
            <property name="targetProject" value="src/test/resources"/>
            <property name="targetPackage" value="test.mappers"/>
            <property name="mapperPackage" value="test.mapper"/>
            <property name="templatePath" value="generator/mapperXml-wueasy.ftl"/>
            <property name="mapperSuffix" value="Mapper"/>
            <property name="fileName" value="${tableClass.shortClassName}${mapperSuffix}.xml"/>
        </plugin>


        <!-- 数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="easyadmin"
                        password="easyadmin">
        </jdbcConnection>
<!--         <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" -->
<!--                         connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" -->
<!--                         userId="easyadmin" -->
<!--                         password="easyadmin"> -->
<!--             针对oracle数据库增加注释 -->
<!--             <property name="remarksReporting" value="true"></property> -->
<!--         </jdbcConnection> -->
        <javaTypeResolver type="com.wueasy.auto.resolver.MyJavaTypeResolverDefaultImpl"/>

        <!--模型配置-->
        <javaModelGenerator targetPackage="test.model" targetProject="src/test/java"/>


        <!-- 表配置 -->
        <table tableName="sys_user">
            <generatedKey column="id" sqlStatement="JDBC"/>
        </table>
    </context>
</generatorConfiguration>

执行类

执行后,将会按照配置生成接口文件。

public class Generator {


    public static InputStream getResourceAsStream(String path){
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
    }

    public static void main(String[] args) throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(getResourceAsStream("generatorConfig.xml"));
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        for (String warning : warnings) {
            System.out.println(warning);
        }
    }
}

版权信息

3.4.1开始支持自动生成自定义版权信息

版权声明内容

新建文件license.txt,如下内容:

wueasy - A Java Distributed Rapid Development Platform.
Copyright (C) 2017-2020 wueasy.com , All Rights Reserved.

Without permission, no one is allowed to use 
(including but not limited to: copying, disseminating,
 displaying, mirroring, uploading, downloading in illegal ways);

Otherwise, legal liability will be investigated according to law.

自主研发生成工具配置

在yml文件中,增加自动生成配置,配置文件路径。

wueasy: 
  auto: 
    copyrightPath: E:\\license.txt #版权文件路径

通用mapper生成工具配置

generatorConfig.xml配置中,增加copyrightPath配置

<?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 id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="copyrightPath" value="E:\\license.txt"/>
    </context>
</generatorConfiguration>
Copyright © wueasy.com 2017-2020 all right reserved,powered by Gitbook未经允许,禁止以任何形式传播 修订时间: 2020-06-30

results matching ""

    No results matching ""