新聞中心
這篇文章主要介紹了Springboot整合MybatisPlus的實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
錫山網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,錫山網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為錫山上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營(yíng)銷網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的錫山做網(wǎng)站的公司定做!
1、pom文件
<?xml version="1.0" encoding="UTF-8"?>4.0.0 com.cun plus 0.0.1-SNAPSHOT jar plus Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.14.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web MySQL mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus-boot-starter 2.3 org.freemarker freemarker 2.3.28 com.alibaba druid-spring-boot-starter 1.1.10 org.springframework.boot spring-boot-maven-plugin
2、創(chuàng)建CodeGenerator.java
package com.cun.plus; import com.baomidou.mybatisplus.enums.IdType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.rules.DbType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; public class CodeGenerator { public static void main(String[] args) { //1. 全局配置 GlobalConfig config = new GlobalConfig(); config.setActiveRecord(false) // 是否支持AR模式 .setAuthor("len") // 作者 .setOutputDir(".\\src\\main\\java") // 生成路徑 .setFileOverride(true) // 文件覆蓋 .setIdType(IdType.AUTO) // 主鍵策略 .setServiceName("I%sService") // 設(shè)置生成的service接口的名字的首字母是否為I // IUserService .setBaseResultMap(true) .setBaseColumnList(true); //2. 數(shù)據(jù)源配置 DataSourceConfig dsConfig = new DataSourceConfig(); dsConfig.setDbType(DbType.MYSQL) // 設(shè)置數(shù)據(jù)庫(kù)類型 .setDriverName("com.mysql.jdbc.Driver") .setUrl("jdbc:mysql://localhost:3306/mydatab?useSSL=true&verifyServerCertificate=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8") .setUsername("root") .setPassword("lqq74561"); //3. 策略配置 //配置要生成的表的表名 String[] tableName = {"system_power_type","system_admin","company","power_api","power_action","power_action_api","power_action_group","power_admin_group","power_group"}; StrategyConfig stConfig = new StrategyConfig(); stConfig.setCapitalMode(true) //全局大寫命名 .setDbColumnUnderline(true) // 指定表名 字段名是否使用下劃線 .setNaming(NamingStrategy.underline_to_camel) // 數(shù)據(jù)庫(kù)表映射到實(shí)體的命名策略 .setTablePrefix("tb_") .setInclude(tableName); // 生成的表 //4. 包名策略配置 PackageConfig pkConfig = new PackageConfig(); pkConfig.setParent("com.cun.plus") .setMapper("mapper") .setService("service") .setController("controller") .setEntity("entity") .setXml("mapper"); //5. 整合配置 AutoGenerator ag = new AutoGenerator(); ag.setGlobalConfig(config) .setDataSource(dsConfig) .setStrategy(stConfig) .setPackageInfo(pkConfig); //6. 執(zhí)行 ag.setTemplateEngine(new FreemarkerTemplateEngine()); ag.execute(); } }
3、在application.yml中配置mybatis-plus
#mybatis-plus mybatis-plus: #xml mapper-locations: classpath:/mapper/*Mapper.xml #bean typeAliasesPackage: com.cun.plus.entity global-config: # 3:"UUID"; id-type: 3 field-strategy: 2 db-column-underline: true key-generator: com.baomidou.mybatisplus.incrementer.OracleKeyGenerator logic-delete-value: 1 logic-not-delete-value: 0 sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector configuration: map-underscore-to-camel-case: true cache-enabled: false #JdbcTypeForNull jdbc-type-for-null: 'null'
4、創(chuàng)建MybatisPlusConfig.java文件
package com.cun.plus.conf; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("com.cun.plus.mapper") public class MybatisPlusConfig { }
其他
Wrapper:MP內(nèi)置條件封裝器。
Sql分析器:(MybatisPlusConfig.java中)
/** * SQL執(zhí)行效率插件 */ @Bean @Profile({"dev","test"})// 設(shè)置 dev test 環(huán)境開啟 public PerformanceInterceptor performanceInterceptor() { return new PerformanceInterceptor(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章標(biāo)題:Springboot整合MybatisPlus的實(shí)現(xiàn)過程解析
文章出自:http://fisionsoft.com.cn/article/pjjicj.html