本身寫的代碼生成器,保證好用!代碼整理中,後面放代碼,可先加羣下載jar使用:QQ:461964997java
優勢:mysql
1,語法簡單,即freeMarker語法。git
2,配置,操做簡單,maven命令生成sql
3,模板暴露,可擴展自定義模板數據庫
使用步驟:mybatis
項目中增長mbg/templates等,以下圖所示:app
提示:文件夾的位置能夠本身適狀況調整位置,只要generatorConfig.xml對應的配置路徑對便可。maven
其中url
1,tramp-generator-config_1_0.dtd文件是對generatorConfig.xml配置文件的規範約束。spa
2,*.ftl文件就是咱們想生成的模板文件,使用了freeMarker,因此只要瞭解freeMarker即可以隨意自定義模板。插件內置了屬性,可直接使用,具體屬性下面說。
3,generatorConfig.xml是插件的核心配置文件,具體描述看下面代碼:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//W3C//DTD HTML 4.01//en" "https://ad_earn/dtd/raw/master/tramp-generator-config_1_0.dtd"> <generatorConfiguration> <tables> <!--數據庫表,name:數據庫中的表名, prefix:表前綴,聲明後不會出如今文件名中 module:模塊 overwrite:是否覆蓋代碼,默認false--> <table name="ceb_notice" module="basic" prefix="ceb_" overwrite="true"/> </tables> <!-- 須要執行的組--> <templates path="mbg/templates" targetGroupName="mybatisBase"> <group name="mybatisBase"> <!--模板,name:模板名稱(生成的文件名稱規則:名稱=類名+模板名稱,再去除 .ftl後綴) tootPath:根目錄(如:resources,java),path:模板生成的文件所要存放的路徑,其中${module}的值爲table標籤module屬性對應的值--> <template name="BaseMapper.xml.ftl" rootPath="resources" path="mybatis.base" overwrite="true"/> <template name="#.java.ftl" rootPath="java" path="com.tramp.${module}.entity" overwrite="true"/> <template name="Field.java.ftl" rootPath="java" path="com.tramp.frame.server.base.field" overwrite="true"/> <template name="BaseDao.java.ftl" rootPath="java" path="com.tramp.frame.server.base.dao" overwrite="true"/> <template name="Enum.java.ftl" rootPath="java" path="com.tramp.${module}.enums" overwrite="true"/> <template name="Mapper.xml.ftl" rootPath="resources" path="mybatis"/> <template name="Service.java.ftl" rootPath="java" path="com.tramp.${module}.service"/> <template name="Dao.java.ftl" rootPath="java" path="com.tramp.${module}.dao"/> </group> <group name="tablesToDoc"> <template name="CiTables.doc.ftl" absolutePath="D:\"/> </group> </templates> <types> <type jdbcType="SMALLINT" javaType="Integer"></type> </types> <info author="autoCoder" remark="autoCoder生成"/> <!--目前只支持mysql--> <dataSource dialect="mysql" url="jdbc:mysql://" db="tramp-fan" user="" password="" driverClassName="com.mysql.jdbc.Driver"/> </generatorConfiguration>
<plugin> <groupId>com.tramp</groupId> <artifactId>autoCoder-maven-plugin</artifactId> <version>1.0.6</version> <configuration> <configPath>${basedir}/mbg/generatorConfig.xml</configPath> </configuration> </plugin>
執行maven命令:
你們能夠根據本身項目狀況配置對應模板,幾分鐘就能夠配置好啦,不懂freeMarker的花幾分鐘時間看看,基本夠用了。下面貼出插件內置的數據,能夠直接拿來用。
以user_addr數據庫表爲例:有link_mobile屬性,類型VARCHAR,註釋爲:聯繫電話
屬性名 | 屬性值 | |||||||||||||||||||||
className | UserAddr | |||||||||||||||||||||
variableName | userAddr | |||||||||||||||||||||
tableName | user_addr | |||||||||||||||||||||
requestBasePath | /user/addr(配置文件配置) | |||||||||||||||||||||
methods | 方法集合(配置文件配置) | |||||||||||||||||||||
author | 做者(配置文件配置) | |||||||||||||||||||||
remark | 配置文件配置 | |||||||||||||||||||||
table |
|
下面貼出模板代碼片斷供參考:
<resultMap id="baseResultMap" type="com.tramp.basic.entity.${className}"> <id column="id" jdbcType="VARCHAR" property="id" /> <#list table.columnList as field> <#if field.name != 'id'> <result column="${field.name}" property="${field.variableName}" jdbcType="${(field.type=='DATETIME')?string('TIMESTAMP',field.type)}"/> </#if> </#list> </resultMap> <sql id="baseColumnList"> <#list table.columnList as field>${field.name}<#if field_has_next>,</#if></#list> </sql> <select id="get" parameterType="java.lang.String" resultMap="baseResultMap"> select <include refid="baseColumnList"/> from ${table.name} where id = ${'#'}{id,jdbcType=VARCHAR} </select> <delete id="delete" parameterType="java.util.Collection"> delete from ${table.name} where 1=1 and id in <foreach close=")" collection="list" index="index" item="item" open="(" separator=","> ${'#'}{item,jdbcType=VARCHAR} </foreach> </delete>
git地址:https://gitee.com/ad_earn/tramp-generator。須要使用的,能夠加qq羣交流,下載,QQ:461964997