基于Java代碼配置MyBatis Generator
使用MyBatis Generator生成器時(shí),有時(shí)候沒(méi)辦法使用xml形式的配置文件,比如將Maven項(xiàng)目設(shè)置成pom打包方式(<packaging>pom</packaging>)!由于Maven的工作機(jī)制對(duì)于打包方式為pom的項(xiàng)目是不會(huì)輸出jar包或war包和resources內(nèi)容,所以放在resources目錄下或放在源碼目錄下的xml文件就沒(méi)法讀取了,就算你在pom.xml文件中明確有如下配置也沒(méi)有用的:
<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 [email protected] * @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è)是我自己的插件,沒(méi)有自定義插件的同學(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 [email protected] * @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 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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))2. idea設(shè)置提示不區(qū)分大小寫的方法3. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)4. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容5. css代碼優(yōu)化的12個(gè)技巧6. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法7. 原生JS實(shí)現(xiàn)記憶翻牌游戲8. Django使用HTTP協(xié)議向服務(wù)器傳參方式小結(jié)9. CentOS郵件服務(wù)器搭建系列—— POP / IMAP 服務(wù)器的構(gòu)建( Dovecot )10. django創(chuàng)建css文件夾的具體方法
