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&useSSL=false&useUnicode=true&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

本文介绍了如何使用MyBatisGenerator作为基础,扩展生成适用于SpringSecurity的Controller、Service、Mapper及POJO。通过修改源码和添加Freemarker模板,实现了SpringBoot应用中的代码自动化生成,减少了繁琐的手动配置和开发工作。文章还提供了源码下载、配置文件示例以及使用步骤,为开发者提供了一种提升开发效率的解决方案。
2740

被折叠的 条评论
为什么被折叠?



