這篇文章主要介紹“Springboot整合MybatisPlus的實(shí)現(xiàn)過(guò)程”,在日常操作中,相信很多人在Springboot整合MybatisPlus的實(shí)現(xiàn)過(guò)程問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Springboot整合MybatisPlus的實(shí)現(xiàn)過(guò)程”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)公司專注于企業(yè)網(wǎng)絡(luò)營(yíng)銷推廣、網(wǎng)站重做改版、井研網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為井研等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
1、pom文件
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cun</groupId> <artifactId>plus</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>plus</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.14.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>MySQL</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>2.3</version> </dependency> <!-- 代碼生成器默認(rèn)使用如下模版引擎 --><!-- <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> </dependency>--> <!-- freemarker 模板引擎 --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency> <!--Druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
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-plusmybatis-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)境開(kāi)啟public PerformanceInterceptor performanceInterceptor() { return new PerformanceInterceptor();}
到此,關(guān)于“Springboot整合MybatisPlus的實(shí)現(xiàn)過(guò)程”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
當(dāng)前文章:Springboot整合MybatisPlus的實(shí)現(xiàn)過(guò)程
網(wǎng)頁(yè)網(wǎng)址:http://m.rwnh.cn/article12/phoedc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、服務(wù)器托管、域名注冊(cè)、網(wǎng)站收錄、企業(yè)網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)