MybatisGenerator生成mybatis程式碼
阿新 • • 發佈:2018-11-13
專案地址
專案地址如下
可以將專案下載到本地,作為你自己私有的工具哦
https://gitee.com/yinkgh/MybatisGenerator
如何生成專案
步驟
#MybatisGenerator專案逆向生成帶有資料庫中文註釋的dao,model,xml檔案。 具體用法如下: 1.修改配置檔案generatorConfig.xml 修改如下:
<commentGenerator>
<!-- 不生成註解資訊 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="org.postgresql.Driver"
connectionURL="jdbc:postgresql://10.168.xx.xx:5432/crm_cms" userId="crm"
password="crm" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.lashou.cms.domain"
targetProject="service-cms" />
<sqlMapGenerator targetPackage="com.lashou.cms.mapper"
targetProject="service-cms" />
<javaClientGenerator targetPackage="com.lashou.cms.mapper"
targetProject="service-cms" type="XMLMAPPER" />
<!-- 修改對應的資料庫表 -->
<table schema="" tableName="comment_deal" domainObjectName="CommentDeal" />
</context>
2.執行程式碼:
執行如下main方法即可生成對應的實體類,xml對映檔案,以及dao層程式碼。
package org.mybatis.generator;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class GeneratorSqlmap {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置檔案
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}