KenhoMBGPlus SpringBoot模板生成工具(一)-构建项目

15 篇文章 0 订阅
6 篇文章 0 订阅

1.引言

在日常工作中我们发现每个模块开发它的基础组成就是查询,新增,修改,查询,删除,对于开发来说这些繁杂却简单的时间消耗是没有任何必要的,如同SpringBoot的出现大大减少了开发中对配置的设置,本工具依据MyBatis Generator为基础,拓展生成基于spring secuirty的controller,server,mapper及POJO。

工具的开发涉及freemark模板,和对MyBatis Generator源码的修改

2.MyBatis Generator

我们先来看看官网对MyBatis Generator的介绍

MyBatis Generator (MBG) is a code generator for MyBatis. It will generate code for all versions of MyBatis. It will introspect a database table (or many tables) and will generate artifacts that can be used to access the table(s). This lessens the initial nuisance of setting up objects and configuration files to interact with database tables。

MyBatis Generator是MyBatis的生成器。他可以生成所有版本MyBatis对应的代码。大大减少了在开发当中对象的构造和配置设置。

2.1 使用

2.1.1 源码下载

下载源码 https://github.com/mybatis/generator

2.1.2 快速启动

通过官网快速指南,设置完对应配置后,执行jar包就行。

我们先看一下官方提供的配置

<!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="simple" targetRuntime="MyBatis3Simple">
    <!--定义数据库相关基本配置-->
    <jdbcConnection driverClass="org.hsqldb.jdbcDriver"
        connectionURL="jdbc:hsqldb:mem:aname" />
    <!--定义生成POJO对象配置-->
    <javaModelGenerator targetPackage="example.model" targetProject="src/main/java"/>
    <!--定义生成mapper对象配置-->
    <sqlMapGenerator targetPackage="example.mapper" targetProject="src/main/resources"/>
    <!--定义查询的表-->
    <table tableName="FooTable" />
  </context>
</generatorConfiguration>

创建好配置之后执行  

java -jar mybatis-generator-core-x.x.x.jar -configfile \temp\generatorConfig.xml -overwrite

3 使用springboot形式进行启动

能使用java -jar即代码中肯定存在一个mian方法,查询文件结构我们可知ShellRunner就是该类,使用代码设置配置,获得一个controller

@RestController
public class AutoCodeController {
    @RequestMapping(value = "/generate", method = RequestMethod.GET)
    public void generate()
    {
        String[] args=new String[3];
        args=new String[3];
        args[0]="-configfile";
        args[1]="/Users/kirra/Desktop/project/KenhoMBGPlus/KenhoMBGPlus/src/main/resources/MysqlConfig.xml";
        args[2]="-overwrite";
        ShellRunner.main(args);
    }
}

使用配置

<generatorConfiguration>

    <!-- 据连接驱动jar地址 -->
    <classPathEntry location="/Users/kirra/Desktop/repo/mysql/mysql-connector-java/5.1.14/mysql-connector-java-5.1.14.jar"/>
    <context id="testTables" targetRuntime="MyBatis3">
        <!--定义数据库相关基本配置-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://172.16.0.11:3306/sys?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8"
                        userId="root"
                        password="123456789">
        </jdbcConnection>
        <!--定义生成POJO对象配置-->
        <javaModelGenerator targetPackage="pojo"
                            targetProject="/Users/kirra/Desktop/project/KenhoMBGPlus/KenhoMBGPlus/coderoot">
        </javaModelGenerator>
        <!--定义生成mapper对象配置-->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="/Users/kirra/Desktop/project/KenhoMBGPlus/KenhoMBGPlus/coderoot">

        </sqlMapGenerator>

        <table tableName="sys_config" domainObjectName="SysConfig"/>

    </context>

</generatorConfiguration>

结果

4.问题

mybatis-generator-config_1_0.dtd和messages.properties需要存在资源文件夹中,不然会造成对应类使用时抛出异常

 

 

后面的博文,我会通过修改源码和追加模板技术,实现springboot模板的生成

github:https://github.com/tale2009/KenhoMBGPlus

参考:http://mybatis.org/generator/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值