av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

基于Java代碼配置MyBatis Generator

瀏覽:84日期:2022-08-30 18:32:58

使用MyBatis Generator生成器時,有時候沒辦法使用xml形式的配置文件,比如將Maven項目設(shè)置成pom打包方式(<packaging>pom</packaging>)!由于Maven的工作機制對于打包方式為pom的項目是不會輸出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>

這個時候就會用到純Java代碼的MyBatis Generator配置,直接貼配置代碼:

import org.mybatis.generator.config.*;/** * 基于Java代碼的MBG配置 * Maven打包方式為POM的項目或模塊(<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'); /*插件配置,這個是我自己的插件,沒有自定義插件的同學(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)項目(源碼主路徑) 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)項目(源碼主路徑) 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)項目(源碼主路徑) 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 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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 一级毛片免费完整视频 | 色婷婷精品 | 亚洲一在线| 精品欧美 | 激情91 | 亚洲精品乱码久久久久久蜜桃91 | 免费日韩网站 | 另类一区 | 一级a性色生活片久久毛片波多野 | 黄色毛片免费视频 | 久久人人爽人人爽人人片av免费 | www.久 | 欧美成人精品一区二区男人看 | 欧美日韩高清一区 | 久久大全 | 伊人操 | 精品美女 | 日韩精品在线视频免费观看 | 久久久久久久久久一区 | 欧美日韩在线一区二区 | 国产精品久久久久久久久久久久久 | 在线成人免费视频 | 午夜一区二区三区视频 | 艹逼网 | 亚洲一区二区不卡在线观看 | 久久中文字幕一区 | 一区二区三区在线电影 | 国产在线一区二区 | 成人在线观看欧美 | 国产在线网站 | 亚洲免费在线观看视频 | 久久久久久国产精品久久 | 欧美日韩国产在线观看 | 成人在线免费电影 | 在线播放一区二区三区 | 国产视频三级 | 欧美在线国产精品 | 精品视频一区二区三区四区 | 亚洲国产精品一区二区第一页 | 日韩1区 | 欧美乱操 |