国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

基于Java代碼配置MyBatis Generator

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

使用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)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: av人摸人人人澡人人超碰 | 成人久久18免费软件 | 亚洲欧美一区二区三区在线 | 国产精品视频永久免费播放 | a级精品九九九大片免费看 a级毛片免费观看网站 | 久草在线看 | 亚洲欧美日本人成在线观看 | 国产欧美久久精品 | 99精品久久久久久久 | 日本高清无吗免费播放 | 欧美一级高清在线观看 | 麻豆一级片 | 亚洲欧美国产一区二区三区 | 最新亚洲国产有精品 | 手机看片午夜 | 久久网站在线观看 | 欧美一级成人毛片影院 | 国产精品私人玩物在线观看 | 色噜噜国产精品视频一区二区 | 亚洲精品专区一区二区三区 | 日本a级毛片视频播放 | 亚洲精品一区二区三区美女 | 国产精品亚洲一区二区在线观看 | 毛片免费高清免费 | 成年人免费观看的视频 | 久久午夜网 | 国产一区二区三区四区五区 | 一区二区三区在线观看视频 | 欧美日韩永久久一区二区三区 | 黄色免费三级 | 极品精品国产超清自在线观看 | 亚洲欧美国产一区二区三区 | 久久精品免费观看国产软件 | 在线永久免费观看黄网站 | 亚洲精品资源网在线观看 | 曰本aaaaa毛片午夜网站 | 免费成人高清 | 成人免费毛片网站 | 男女国产视频 | 在线亚洲精品国产成人二区 | 一本色道久久综合亚洲精品高清 |