增加根据velocity模板自动生成generatorConfig.xml文件
This commit is contained in:
parent
7f3e9f16f4
commit
ad328e093c
|
@ -112,7 +112,8 @@ zheng
|
||||||
|
|
||||||
- 创建数据表(建议使用PowerDesigner)
|
- 创建数据表(建议使用PowerDesigner)
|
||||||
|
|
||||||
- 配置对应dao模块的generatorConfig.xml,只需底部追加指定表的代码生成,可生成单表的CRUD功能
|
- 直接运行对应项目dao模块中的generator.main(),可自动生成单表的CRUD功能和对应的model、example、mapper、service代码
|
||||||
|
|
||||||
- 生成的model和example均已实现Serializable接口,支持分布式
|
- 生成的model和example均已实现Serializable接口,支持分布式
|
||||||
- 生成的mapper.xml的selectByExample方法自动包含分页参数offset和limit
|
- 生成的mapper.xml的selectByExample方法自动包含分页参数offset和limit
|
||||||
- 已包含抽象类BaseServiceImpl,只需要继承抽象类并传入泛型参数,即可默认实现mapper接口所有方法,特殊需求直接扩展即可
|
- 已包含抽象类BaseServiceImpl,只需要继承抽象类并传入泛型参数,即可默认实现mapper接口所有方法,特殊需求直接扩展即可
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.zheng.cms.dao;
|
||||||
|
|
||||||
|
import com.zheng.common.util.JdbcUtil;
|
||||||
|
import com.zheng.common.util.PropertiesFileUtil;
|
||||||
|
import com.zheng.common.util.VelocityUtil;
|
||||||
|
import org.apache.velocity.VelocityContext;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代码生成类
|
||||||
|
* Created by ZhangShuzheng on 2017/1/10.
|
||||||
|
*/
|
||||||
|
public class Generator {
|
||||||
|
|
||||||
|
// 根据命名规范,只修改此常量值即可
|
||||||
|
private static String MODULE_PREFIX_NAME = "cms";
|
||||||
|
|
||||||
|
private static String VM_PATH = "zheng-common/src/main/resources/generatorConfig.vm";
|
||||||
|
private static String MODULE_PATH = "zheng-cms/zheng-" + MODULE_PREFIX_NAME + "-dao/src/main/resources/generatorConfig.xml";
|
||||||
|
private static String SQL = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'zheng' AND table_name LIKE '" + MODULE_PREFIX_NAME + "_%';";
|
||||||
|
private static String JDBC_DRIVER = PropertiesFileUtil.getInstance("jdbc").get("jdbc.driver");
|
||||||
|
private static String JDBC_URL = PropertiesFileUtil.getInstance("jdbc").get("jdbc.url");
|
||||||
|
private static String JDBC_USERNAME= PropertiesFileUtil.getInstance("jdbc").get("jdbc.username");
|
||||||
|
private static String JDBC_PASSWORD = PropertiesFileUtil.getInstance("jdbc").get("jdbc.password");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据模板生成generatorConfig.xml文件
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("========== 开始生成代码 ==========");
|
||||||
|
try {
|
||||||
|
VelocityContext context= new VelocityContext();
|
||||||
|
List<Map<String, Object>> tables = new ArrayList<>();
|
||||||
|
Map<String, Object> table = null;
|
||||||
|
|
||||||
|
// 查询定制前缀项目的所有表
|
||||||
|
JdbcUtil jdbcUtil = new JdbcUtil(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD);
|
||||||
|
List<Map> result = jdbcUtil.selectByParams(SQL, null);
|
||||||
|
for (Map map : result) {
|
||||||
|
System.out.println(map.get("TABLE_NAME"));
|
||||||
|
table = new HashMap<>();
|
||||||
|
table.put("table_name", map.get("TABLE_NAME"));
|
||||||
|
// TODO 转驼峰
|
||||||
|
table.put("model_name", map.get("TABLE_NAME"));
|
||||||
|
tables.add(table);
|
||||||
|
}
|
||||||
|
jdbcUtil.release();
|
||||||
|
|
||||||
|
context.put("tables", tables);
|
||||||
|
VelocityUtil.generate(VM_PATH, MODULE_PATH, context);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
System.out.println("========== 结束生成代码 ==========");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -64,28 +64,12 @@
|
||||||
<javaClientGenerator targetPackage="${generator.javaClientGenerator.targetPackage}" targetProject="src/main/java" type="XMLMAPPER" />
|
<javaClientGenerator targetPackage="${generator.javaClientGenerator.targetPackage}" targetProject="src/main/java" type="XMLMAPPER" />
|
||||||
|
|
||||||
<!-- 需要映射的表 -->
|
<!-- 需要映射的表 -->
|
||||||
<table tableName="test_user" domainObjectName="User">
|
<table tableName="cms_article" domainObjectName="cms_article"></table>
|
||||||
<generatedKey column="user_id" sqlStatement="MySql" identity="true" />
|
<table tableName="cms_article_category" domainObjectName="cms_article_category"></table>
|
||||||
</table>
|
<table tableName="cms_article_tag" domainObjectName="cms_article_tag"></table>
|
||||||
<table tableName="test_book" domainObjectName="Book">
|
<table tableName="cms_category" domainObjectName="cms_category"></table>
|
||||||
<generatedKey column="book_id" sqlStatement="MySql" identity="true" />
|
<table tableName="cms_category_tag" domainObjectName="cms_category_tag"></table>
|
||||||
</table>
|
<table tableName="cms_comment" domainObjectName="cms_comment"></table>
|
||||||
<table tableName="cms_article" domainObjectName="CmsArticle">
|
<table tableName="cms_tag" domainObjectName="cms_tag"></table>
|
||||||
<generatedKey column="article_id" sqlStatement="MySql" identity="true" />
|
|
||||||
</table>
|
|
||||||
<table tableName="cms_category" domainObjectName="CmsCategory">
|
|
||||||
<generatedKey column="category_id" sqlStatement="MySql" identity="true" />
|
|
||||||
</table>
|
|
||||||
<table tableName="cms_tag" domainObjectName="CmsTag">
|
|
||||||
<generatedKey column="tag_id" sqlStatement="MySql" identity="true" />
|
|
||||||
</table>
|
|
||||||
<table tableName="cms_article_category" domainObjectName="CmsArticleCategory"></table>
|
|
||||||
<table tableName="cms_article_tag" domainObjectName="CmsArticleTag"></table>
|
|
||||||
<table tableName="cms_category_tag" domainObjectName="CmsCategoryTag"></table>
|
|
||||||
<table tableName="cms_comment" domainObjectName="CmsComment">
|
|
||||||
<generatedKey column="case_id" sqlStatement="MySql" identity="true" />
|
|
||||||
</table>
|
|
||||||
<table tableName="cms_page" domainObjectName="CmsPage"></table>
|
|
||||||
<table tableName="cms_setting" domainObjectName="CmsSetting"></table>
|
|
||||||
</context>
|
</context>
|
||||||
</generatorConfiguration>
|
</generatorConfiguration>
|
|
@ -113,7 +113,7 @@
|
||||||
<artifactId>zkclient</artifactId>
|
<artifactId>zkclient</artifactId>
|
||||||
<version>0.1</version>
|
<version>0.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- 模板引擎 -->
|
<!-- velocity模板引擎 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.velocity</groupId>
|
<groupId>org.apache.velocity</groupId>
|
||||||
<artifactId>velocity</artifactId>
|
<artifactId>velocity</artifactId>
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.zheng.common.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代码生成工具类
|
||||||
|
* Created by ZhangShuzheng on 2017/1/10.
|
||||||
|
*/
|
||||||
|
public class GeneratorUtil {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
<?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>
|
||||||
|
|
||||||
|
<!-- 配置文件 -->
|
||||||
|
<properties resource="jdbc.properties"></properties>
|
||||||
|
|
||||||
|
<!-- mysql驱动包 -->
|
||||||
|
<classPathEntry location="${classPathEntry}" />
|
||||||
|
|
||||||
|
<context id="context" targetRuntime="MyBatis3">
|
||||||
|
|
||||||
|
<property name="javaFileEncoding" value="UTF-8"/>
|
||||||
|
|
||||||
|
<!-- 由于beginningDelimiter和endingDelimiter的默认值为双引号("),在Mysql中不能这么写,所以还要将这两个默认值改为**反单引号(`)**,配置如下: -->
|
||||||
|
<property name="beginningDelimiter" value=""/>
|
||||||
|
<property name="endingDelimiter" value=""/>
|
||||||
|
|
||||||
|
<!-- 为生成的Java模型创建一个toString方法 -->
|
||||||
|
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
|
||||||
|
|
||||||
|
<!-- 为生成的Java模型类添加序列化接口,并生成serialVersionUID字段 -->
|
||||||
|
<plugin type="com.zheng.common.plugin.SerializablePlugin">
|
||||||
|
<property name="suppressJavaInterface" value="false"/>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- 生成一个新的selectByExample方法,这个方法可以接受一个RowBounds参数,主要用来实现分页 -->
|
||||||
|
<plugin type="com.zheng.common.plugin.PaginationPlugin"></plugin>
|
||||||
|
|
||||||
|
<!-- 生成在XML中的<cache>元素 -->
|
||||||
|
<plugin type="org.mybatis.generator.plugins.CachePlugin">
|
||||||
|
<!-- 使用ehcache -->
|
||||||
|
<property name="cache_type" value="org.mybatis.caches.ehcache.LoggingEhcache" />
|
||||||
|
<!-- 内置cache配置 -->
|
||||||
|
<!--
|
||||||
|
<property name="cache_eviction" value="LRU" />
|
||||||
|
<property name="cache_flushInterval" value="60000" />
|
||||||
|
<property name="cache_readOnly" value="true" />
|
||||||
|
<property name="cache_size" value="1024" />
|
||||||
|
-->
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- Java模型生成equals和hashcode方法 -->
|
||||||
|
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
|
||||||
|
|
||||||
|
<!-- 生成的代码去掉注释 -->
|
||||||
|
<commentGenerator>
|
||||||
|
<property name="suppressAllComments" value="true" />
|
||||||
|
</commentGenerator>
|
||||||
|
|
||||||
|
<!-- 数据库连接 -->
|
||||||
|
<jdbcConnection driverClass="${jdbc.driver}"
|
||||||
|
connectionURL="${jdbc.url}"
|
||||||
|
userId="${jdbc.username}"
|
||||||
|
password="${jdbc.password}" />
|
||||||
|
|
||||||
|
<!-- model生成 -->
|
||||||
|
<javaModelGenerator targetPackage="${generator.javaModelGenerator.targetPackage}" targetProject="src/main/java" />
|
||||||
|
|
||||||
|
<!-- MapperXML生成 -->
|
||||||
|
<sqlMapGenerator targetPackage="${generator.sqlMapGenerator.targetPackage}" targetProject="src/main/java" />
|
||||||
|
|
||||||
|
<!-- Mapper接口生成 -->
|
||||||
|
<javaClientGenerator targetPackage="${generator.javaClientGenerator.targetPackage}" targetProject="src/main/java" type="XMLMAPPER" />
|
||||||
|
|
||||||
|
<!-- 需要映射的表 -->
|
||||||
|
#foreach($table in $tables)
|
||||||
|
<table tableName="$!table.table_name" domainObjectName="$!table.model_name"></table>
|
||||||
|
#end
|
||||||
|
</context>
|
||||||
|
</generatorConfiguration>
|
Loading…
Reference in New Issue