基于Java代碼配置MyBatis Generator
使用MyBatis Generator生成器時(shí),有時(shí)候沒辦法使用xml形式的配置文件,比如將Maven項(xiàng)目設(shè)置成pom打包方式(<packaging>pom</packaging>)!由于Maven的工作機(jī)制對于打包方式為pom的項(xiàng)目是不會輸出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í)候就會用到純Java代碼的MyBatis Generator配置,直接貼配置代碼:
import org.mybatis.generator.config.*; /** * 基于Java代碼的MBG配置 * Maven打包方式為POM的項(xiàng)目或模塊(<packaging>pom</packaging>),resources目錄的內(nèi)容不會輸出到類路徑下,所以可以選擇直接使用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無法識別table標(biāo)簽中schema類的配置,所以在URL上指明目標(biāo)數(shù)據(jù)庫,并追加nullCatalogMeansCurrent屬性為true jdbcConnectionConfig.addProperty("remarksReporting", "true");//針對oracle數(shù)據(jù)庫無法讀取表和字段備注 jdbcConnectionConfig.addProperty("useInformationSchema", "true");//針對mysql數(shù)據(jù)庫無法讀取表和字段備注 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)用過程中原本讀取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形式的配置也貼上,說不定能幫到某些同學(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無法識別table標(biāo)簽中schema類的配置,所以在URL上指明目標(biāo)數(shù)據(jù)庫,并追加nullCatalogMeansCurrent屬性為true--> <property name="nullCatalogMeansCurrent" value="true"></property> <!-- /*針對oracle數(shù)據(jù)庫無法讀取表和字段備注*/ --> <property name="remarksReporting" value="true"></property> <!-- /*針對mysql數(shù)據(jù)庫無法讀取表和字段備注*/ --> <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"/> <!--對于MySQL不要加schema和catalog,會生成{catalog}..{table}的SQL語句,表名填%是表示生成目標(biāo)庫的所有表--> <table tableName="%"> <!--指定生成主鍵列相關(guān)的設(shè)置--> <generatedKey column="id" sqlStatement="JDBC"/> </table> </context> </generatorConfiguration>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
log4j.properties 配置(實(shí)例講解)
下面小編就為大家?guī)硪黄猯og4j.properties 配置(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Dubbo?LoadBalance基于權(quán)重的隨機(jī)負(fù)載均衡算法提高服務(wù)性能
這篇文章主要為大家介紹了Dubbo?LoadBalance基于權(quán)重的隨機(jī)負(fù)載均衡算法提高服務(wù)性能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-10-10java實(shí)現(xiàn)文件切片上傳百度云+斷點(diǎn)續(xù)傳的方法
文件續(xù)傳在很多地方都可以用的到,本文主要介紹了java實(shí)現(xiàn)文件切片上傳百度云+斷點(diǎn)續(xù)傳的方法,?文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12MyBatis全局映射文件實(shí)現(xiàn)原理解析
這篇文章主要介紹了MyBatis全局映射文件實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Java?多線程并發(fā)編程提高數(shù)據(jù)處理效率的詳細(xì)過程
這篇文章主要介紹了Java?多線程并發(fā)編程提高數(shù)據(jù)處理效率,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Java優(yōu)化重復(fù)冗余代碼的8種方式總結(jié)
日常開發(fā)中,我們經(jīng)常會遇到一些重復(fù)代碼,最近小編優(yōu)化了一些系統(tǒng)中的重復(fù)代碼,用了好幾種的方式,感覺挺有用的,所以本文給大家講講優(yōu)化重復(fù)代碼的幾種方式2023-08-08Xml中使用foreach遍歷對象實(shí)現(xiàn)代碼
這篇文章主要介紹了Xml中使用foreach遍歷對象實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12