您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“MybatisPlus怎么自動生成映射文件”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“MybatisPlus怎么自動生成映射文件”這篇文章吧。
pom.xml添加以下依賴:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> <!-- 高版本mybatis plus將這個包摘出來了,所以需要添加此依賴,低版本不需要 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> </dependency>
package com.yss.datamiddle.common; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.querys.DB2Query; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * @description: 數據庫生成JAVA類 * @create: 2020-07-20 14:00:00 * @update: 2020-07-20 14:00:00 */ public class MybatisPlusUtils { /** * mysql自動代碼生成類 * * @param includeTables * @param excludeTables */ public static void generateMysql(String[] includeTables, String[] excludeTables, Boolean db1) { AutoGenerator autoGenerator = new AutoGenerator(); /** * 數據庫配置 */ buildMysqlDb(autoGenerator); StrategyConfig strategyConfig = new StrategyConfig(); // 駝峰命名 strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); strategyConfig.setNaming(NamingStrategy.underline_to_camel); strategyConfig.setInclude(includeTables); strategyConfig.setExclude(excludeTables); strategyConfig.setTablePrefix(""); autoGenerator.setStrategy(strategyConfig); /** * 全局配置 */ GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setAuthor("Han LiDong"); globalConfig.setDateType(DateType.ONLY_DATE); globalConfig.setActiveRecord(true); globalConfig.setIdType(IdType.INPUT); globalConfig.setOutputDir("C:/D/ORM"); //文件生成路徑 globalConfig.setMapperName("%sMapper"); globalConfig.setServiceName("%sService"); globalConfig.setServiceImplName("%sServiceImpl"); globalConfig.setXmlName("%sMapper"); globalConfig.setSwagger2(true); globalConfig.setFileOverride(true); globalConfig.setBaseColumnList(false); globalConfig.setBaseResultMap(false); globalConfig.setEnableCache(false); autoGenerator.setGlobalConfig(globalConfig); /** * 包名配置 */ PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent(""); packageConfig.setEntity("com.yss.datamiddle.entity"); packageConfig.setMapper("com.yss.datamiddle.dao"); packageConfig.setXml("mapper"); packageConfig.setService("com.yss.datamiddle.service"); packageConfig.setServiceImpl("com.yss.datamiddle.impl"); packageConfig.setController("com.yss.datamiddle.controller"); autoGenerator.setPackageInfo(packageConfig); // 采用默認模板,自定義模板參考@see com.baomidou.mybatisplus.generator.config.TemplateConfig // TemplateConfig templateConfig = new TemplateConfig(); autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine()); autoGenerator.execute(); } /** * oracle自動代碼生成類 * @param includeTables * @param excludeTables */ public static void generateOracle(String[] includeTables, String[] excludeTables, Boolean db1) { AutoGenerator autoGenerator = new AutoGenerator(); buildOracleDb(autoGenerator); StrategyConfig strategyConfig = new StrategyConfig(); // 駝峰命名 strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); strategyConfig.setNaming(NamingStrategy.underline_to_camel); strategyConfig.setInclude(includeTables); strategyConfig.setExclude(excludeTables); strategyConfig.setTablePrefix(""); autoGenerator.setStrategy(strategyConfig); /** * 全局配置 */ GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setAuthor("Han LiDong"); globalConfig.setDateType(DateType.ONLY_DATE); globalConfig.setActiveRecord(true); globalConfig.setIdType(IdType.AUTO); //這里用oracle的序列和觸發器實現主鍵自增 globalConfig.setOutputDir("C:/ORM"); //文件生成路徑 globalConfig.setMapperName("%sMapper"); globalConfig.setServiceName("%sService"); globalConfig.setServiceImplName("%sServiceImpl"); globalConfig.setXmlName("%sMapper"); globalConfig.setSwagger2(true); globalConfig.setFileOverride(true); globalConfig.setBaseColumnList(false); globalConfig.setBaseResultMap(false); globalConfig.setEnableCache(false); autoGenerator.setGlobalConfig(globalConfig); /** * 包名配置 */ PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent(""); packageConfig.setEntity("com.yss.datamiddle.entity"); packageConfig.setMapper("com.yss.datamiddle.dao"); packageConfig.setXml("mapper"); packageConfig.setService("com.yss.datamiddle.service"); packageConfig.setServiceImpl("com.yss.datamiddle.impl"); packageConfig.setController("com.yss.datamiddle.controller"); autoGenerator.setPackageInfo(packageConfig); // 采用默認模板,自定義模板參考@see com.baomidou.mybatisplus.generator.config.TemplateConfig // TemplateConfig templateConfig = new TemplateConfig(); autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine()); autoGenerator.execute(); } private static void buildOracleDb(AutoGenerator autoGenerator) { DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbQuery(new MyDbQuery()); //自定義dbquery不然querydb.dbtype找不到 dataSourceConfig.setDbType(DbType.ORACLE); dataSourceConfig.setDriverName("oracle.jdbc.driver.OracleDriver"); dataSourceConfig.setUsername("xxxxx"); dataSourceConfig.setPassword("xxxxx"); dataSourceConfig.setUrl("jdbc:oracle:thin:@192.168.xxx.xxx/orcl"); //dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"); autoGenerator.setDataSource(dataSourceConfig); } private static void buildMysqlDb(AutoGenerator autoGenerator) { DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL); dataSourceConfig.setDriverName("com.mysql.jdbc.Driver"); dataSourceConfig.setUsername("root"); dataSourceConfig.setPassword("123456"); dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"); autoGenerator.setDataSource(dataSourceConfig); } public static void main(String[] args) { //String[] includeTables = {"hr_kpi_dept_person","sys_user"}; String[] includeTables = {"ETL_MIDDLE_STATE"}; //generateMysql(includeTables,null,false); //mysql映射文件生成 generateOracle(includeTables,null,false); //oracle映射文件生成 } }
右鍵 Run 運行之后就會生成一堆映射文件,將之copy到自己的項目中就可以了
注意: 生成的dao層文件需要自己添加@Mapper注解
在Mybatis的配置文件中添加settings屬性的autoMappingBehavior
<settings> <setting name="autoMappingBehavior" value="NONE"/> </settings>
autoMappingBehavior有三個屬性(默認是PARTIAL)
NONE
:取消自動映射
PARTIAL
:只會自動映射,沒有定義嵌套結果集映射的結果集
FULL
:會自動映射任意復雜的結果集(無論是否嵌套)
自動映射的時候sql語句的結果集字段是不區分大小寫的,所以映射的pojo成員變量也不需要區分大小寫,都可以映射到。
如果數據庫符合命名規范,即每個單詞之間用下劃線連接,pojo類符合駝峰式命名,就可以設置mapUnderscoreToCamelCase為true,這樣就可以自動映射。
不用再給select語句的結果集字段起別名來對應pojo類的成員
一個配置完整的 settings 元素的示例如下:
<settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25"/> <setting name="defaultFetchSize" value="100"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="mapUnderscoreToCamelCase" value="false"/> <setting name="localCacheScope" value="SESSION"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> </settings>
以上是“MybatisPlus怎么自動生成映射文件”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。