基于Java代碼配置MyBatis Generator
使用MyBatis Generator生成器時(shí),有時(shí)候沒辦法使用xml形式的配置文件,比如將Maven項(xiàng)目設(shè)置成pom打包方式(<packaging>pom</packaging>)!由于Maven的工作機(jī)制對(duì)于打包方式為pom的項(xiàng)目是不會(huì)輸出jar包或war包和resources內(nèi)容,所以放在resources目錄下或放在源碼目錄下的xml文件就沒法讀取了,就算你在pom.xml文件中明確有如下配置也沒有用的:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
這個(gè)時(shí)候就會(huì)用到純Java代碼的MyBatis Generator配置,直接貼配置代碼:
import org.mybatis.generator.config.*;
/**
* 基于Java代碼的MBG配置
* Maven打包方式為POM的項(xiàng)目或模塊(<packaging>pom</packaging>),resources目錄的內(nèi)容不會(huì)輸出到類路徑下,所以可以選擇直接使用Java代碼配置!
*
* @author 707669522@qq.com
* @since 2020-06-13
*/
public class GeneratorConfig {
public static Configuration getGeneratorConfig() {
Context context = new Context(ModelType.CONDITIONAL);
context.setId("simple");
context.setTargetRuntime("MyBatis3Simple");
/*添加屬性*/
context.addProperty("javaFileEncoding", "UTF-8");
/*插件配置,這個(gè)是我自己的插件,沒有自定義插件的同學(xué)可以不配這一節(jié),刪除即可*/
PluginConfiguration pluginConfig = new PluginConfiguration();
pluginConfig.setConfigurationType("com.xgclassroom.generator.GeneratorPlugin");
context.addPluginConfiguration(pluginConfig);
/*注釋生成器配置*/
CommentGeneratorConfiguration commentGeneratorConfig = new CommentGeneratorConfiguration();
commentGeneratorConfig.addProperty("suppressAllComments", "true");
context.setCommentGeneratorConfiguration(commentGeneratorConfig);
/*JDBC連接信息配置*/
JDBCConnectionConfiguration jdbcConnectionConfig = new JDBCConnectionConfiguration();
jdbcConnectionConfig.setDriverClass("com.mysql.cj.jdbc.Driver");
//注意代碼配置中JDBC連接字符串中的參數(shù)分隔符不需要再像xml配置文件中那樣使用轉(zhuǎn)義符
jdbcConnectionConfig.setConnectionURL("jdbc:mysql://localhost:3306/permission_center?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false");
jdbcConnectionConfig.setUserId("xurm");
jdbcConnectionConfig.setPassword("1qaz@WSX");
jdbcConnectionConfig.addProperty("nullCatalogMeansCurrent", "true");//MySQL無(wú)法識(shí)別table標(biāo)簽中schema類的配置,所以在URL上指明目標(biāo)數(shù)據(jù)庫(kù),并追加nullCatalogMeansCurrent屬性為true
jdbcConnectionConfig.addProperty("remarksReporting", "true");//針對(duì)oracle數(shù)據(jù)庫(kù)無(wú)法讀取表和字段備注
jdbcConnectionConfig.addProperty("useInformationSchema", "true");//針對(duì)mysql數(shù)據(jù)庫(kù)無(wú)法讀取表和字段備注
context.setJdbcConnectionConfiguration(jdbcConnectionConfig);
/*Model生成器配置*/
JavaModelGeneratorConfiguration javaModelGeneratorConfig = new JavaModelGeneratorConfiguration();
javaModelGeneratorConfig.setTargetProject("permission/src/main/java");//目標(biāo)項(xiàng)目(源碼主路徑)
javaModelGeneratorConfig.setTargetPackage("com.xgclassroom.model");//目標(biāo)包(Model類文件存放包)
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfig);
/*SqlMapper生成器配置(*Mapper.xml類文件),要javaClient生成器類型配合*/
SqlMapGeneratorConfiguration sqlMapGeneratorConfig = new SqlMapGeneratorConfiguration();
sqlMapGeneratorConfig.setTargetProject("permission/src/main/java");//目標(biāo)項(xiàng)目(源碼主路徑)
sqlMapGeneratorConfig.setTargetPackage("com.xgclassroom.mapper");//目標(biāo)包(*Mapper.xml類文件存放包)
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfig);
/*JavaClient生成器配置(*Mapper.java類文件)*/
JavaClientGeneratorConfiguration javaClientGeneratorConfig = new JavaClientGeneratorConfiguration();
javaClientGeneratorConfig.setConfigurationType("XMLMAPPER");//JavaClient生成器類型(主要有ANNOTATEDMAPPER、MIXEDMAPPER、XMLMAPPER,要Context的TargetRuntime配合)
javaClientGeneratorConfig.setTargetProject("permission/src/main/java");//目標(biāo)項(xiàng)目(源碼主路徑)
javaClientGeneratorConfig.setTargetPackage("com.xgclassroom.mapper");//目標(biāo)包(*Mapper.java類文件存放包)
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfig);
/*表生成配置*/
TableConfiguration tableConfig = new TableConfiguration(context);
tableConfig.setTableName("%");
GeneratedKey generatedKey = new GeneratedKey("id", "JDBC", true, null);//設(shè)置主鍵列和生成方式
tableConfig.setGeneratedKey(generatedKey);
context.addTableConfiguration(tableConfig);
Configuration config = new Configuration();
config.addContext(context);
return config;
}
}
然后就是把MyBatis Generator調(diào)用過(guò)程中原本讀取xml配置文件的地方換掉就可以了:
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.util.ArrayList;
import java.util.List;
/**
* MyBatisGenerator代碼生成器Java調(diào)用程序
*
* @author 707669522@qq.com
* @since 2020-06-13
*/
public class GeneratorRunner {
public static void main(String[] args) {
try {
List<String> warnings = new ArrayList<String>();
Configuration config;
//使用xml配置文件的方式
/*File configFile = new File(GeneratorRunner.class.getClassLoader().getResource("generatorConfig.xml").getPath());
ConfigurationParser cp = new ConfigurationParser(warnings);
config = cp.parseConfiguration(configFile);*/
//使用純Java代碼配置的方式
config = GeneratorConfig.getGeneratorConfig();
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
最后把xml形式的配置也貼上,說(shuō)不定能幫到某些同學(xué):
<?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>
<context id="simple" targetRuntime="MyBatis3Simple">
<property name="javaFileEncoding" value="UTF-8"/>
<plugin type="com.xgclassroom.generator.GeneratorPlugin"></plugin>
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/customer_center?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false"
userId="xurm" password="1qaz@WSX">
<!--MySQL無(wú)法識(shí)別table標(biāo)簽中schema類的配置,所以在URL上指明目標(biāo)數(shù)據(jù)庫(kù),并追加nullCatalogMeansCurrent屬性為true-->
<property name="nullCatalogMeansCurrent" value="true"></property>
<!-- /*針對(duì)oracle數(shù)據(jù)庫(kù)無(wú)法讀取表和字段備注*/ -->
<property name="remarksReporting" value="true"></property>
<!-- /*針對(duì)mysql數(shù)據(jù)庫(kù)無(wú)法讀取表和字段備注*/ -->
<property name="useInformationSchema" value="true"></property>
</jdbcConnection>
<javaModelGenerator targetPackage="com.xgclassroom.model" targetProject="customer/src/main/java"/>
<sqlMapGenerator targetPackage="com.xgclassroom.mapper" targetProject="customer/src/main/java"></sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.xgclassroom.mapper"
targetProject="customer/src/main/java"/>
<!--對(duì)于MySQL不要加schema和catalog,會(huì)生成{catalog}..{table}的SQL語(yǔ)句,表名填%是表示生成目標(biāo)庫(kù)的所有表-->
<table tableName="%">
<!--指定生成主鍵列相關(guān)的設(shè)置-->
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot2.x配置多數(shù)據(jù)源方式
這篇文章主要介紹了SpringBoot2.x配置多數(shù)據(jù)源方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
java 實(shí)現(xiàn)將一個(gè)string保存到txt文檔中
今天小編就為大家分享一篇java 實(shí)現(xiàn)將一個(gè)string保存到txt文檔中的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法
這篇文章主要介紹了SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
Java中ScheduledExecutorService介紹和使用案例(推薦)
ScheduledExecutorService是Java并發(fā)包中的接口,用于安排任務(wù)在給定延遲后運(yùn)行或定期執(zhí)行,它繼承自ExecutorService,具有線程池特性,可復(fù)用線程,提高效率,本文主要介紹java中的ScheduledExecutorService介紹和使用案例,感興趣的朋友一起看看吧2024-10-10
老生常談Java中List與ArrayList的區(qū)別
大家都知道List是接口,ArrayList是List接口的一個(gè)實(shí)現(xiàn)類,接下來(lái)通過(guò)本文給大家介紹Java中List與ArrayList的區(qū)別,需要的朋友可以參考下2022-08-08
SpringBoot在啟動(dòng)類main方法中調(diào)用service層方法報(bào)“空指針異?!暗慕鉀Q辦法
這篇文章主要介紹了SpringBoot在啟動(dòng)類main方法中調(diào)用service層方法報(bào)“空指針異常“的解決辦法,大多數(shù)情況下,我們使用Springboot是創(chuàng)建一個(gè)maven項(xiàng)目,然后通過(guò)controller層的接口調(diào)用,但也有特殊情況,文章介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06

