使用Springboot + Gradle快速整合Mybatis-Plus


使用Springboot + Gradle快速整合Mybatis-Plus

作者:Stanley 羅昊

轉載請注明出處和署名,謝謝!

MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生。

注:作者使用IDEA編輯器

使用 Spring Initializr 快速初始化一個 Spring Boot 工程

在IDEA編輯左上角Flie ->new - >Project 👇

選擇Spring Initializr -->Next:(下一步)進入下面這個界面👇,Group:填寫你的包路徑 Artifact:項目名

jar包管理工具選擇Gradle Project -->Next(下一步)

選擇Web->Next(下一步)編寫項目文件名以及路徑

點擊Finish(完成)

添加依賴

點擊 build.gradle添加依賴

引入一下依賴:

復制代碼
    dependencies {
        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.springframework.boot:spring-boot-starter-thymeleaf')
        compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version: '1.3.7'
        compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1')
        implementation group: 'com.alibaba', name: 'druid', version: '1.1.12'
        compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.1.2.RELEASE'
        testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.1.2.RELEASE'
        compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.0.7.1'
        testCompile group: 'com.h2database', name: 'h2', version: '1.4.196'
        compile group: 'org.projectlombok', name: 'lombok', version: '1.16.20'
        compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.0.1'
        compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.11'
        compile group: 'org.springframework', name: 'spring-jdbc', version: '4.0.0.RELEASE'
        compile group: 'com.baomidou', name: 'mybatis-plus', version: '3.0.1'
    }
復制代碼

配置resources 文件下的 application.yml

以上有MySQL8.0以上jar包,在編寫jdbc連接池是,多了一個cj

復制代碼
datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name:  com.mysql.cj.jdbc.Driver//8.0以上需要多加一個cj
    username: 用戶名
    password: 密碼
    url: jdbc:mysql://localhost:3306/數據庫?characterEncoding=utf-8&useSSL=false
mybatis-plus:
  mapper-locations: classpath*:mapperConfig/*.xml
復制代碼

編寫代碼生成器CodeGeneration在編譯路徑下

復制代碼
 1 import com.baomidou.mybatisplus.annotation.DbType;
 2 import com.baomidou.mybatisplus.generator.AutoGenerator;
 3 import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
 4 import com.baomidou.mybatisplus.generator.config.GlobalConfig;
 5 import com.baomidou.mybatisplus.generator.config.PackageConfig;
 6 import com.baomidou.mybatisplus.generator.config.StrategyConfig;
 7 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
 8 
 9 /**
10  * @author Stanley 羅昊
11  * @ClassName: CodeGeneration
12  * @Description: 代碼生成器
13  * @date  2019年2月15日 下午8:10:14
14  */
15 public class CodeGeneration {
16 
17     /**
18      * @param args
19      * @Title: main
20      * @Description: 生成
21      */
22     public static void main(String[] args) {
23         AutoGenerator
24                 mpg = new AutoGenerator();
25 
26 
27         // 全局配置
28         GlobalConfig gc = new GlobalConfig();
29         gc.setOutputDir("src\\main\\java");//輸出文件路徑
30         gc.setFileOverride(true);
31         gc.setActiveRecord(false);// 不需要ActiveRecord特性的請改為false
32         gc.setEnableCache(false);// XML 二級緩存
33         gc.setBaseResultMap(true);// XML ResultMap
34         gc.setBaseColumnList(true);// XML columList
35         gc.setAuthor("luohao");// 作者
36 
37         // 自定義文件命名,注意 %s 會自動填充表實體屬性!
38         gc.setControllerName("%sController");
39         gc.setServiceName("I%sService");
40         gc.setServiceImplName("%sServiceImpl");
41         gc.setMapperName("%sMapper");
42         gc.setXmlName("%sMapper");
43         mpg.setGlobalConfig(gc);
44 
45         // 數據源配置
46         DataSourceConfig dsc = new DataSourceConfig();
47         dsc.setDbType(DbType.MYSQL);
48         dsc.setDriverName("com.mysql.cj.jdbc.Driver");
49         dsc.setUsername("root");
50         dsc.setPassword("123");
51         dsc.setUrl("jdbc:mysql://localhost:3306/mybatispuls?characterEncoding=utf-8&useSSL=false");
52         mpg.setDataSource(dsc);
53 
54         // 策略配置
55         StrategyConfig strategy = new StrategyConfig();
56         // strategy.setTablePrefix(new String[] { "sys_" });// 此處可以修改為您的表前綴
57         strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
58         strategy.setInclude(new String[]{"user"}); // 需要生成的表
59 
60         strategy.setSuperServiceClass(null);
61         strategy.setSuperServiceImplClass(null);
62         strategy.setSuperMapperClass(null);
63 
64         mpg.setStrategy(strategy);
65 
66         // 包配置
67         PackageConfig pc = new PackageConfig();
68         pc.setParent("com.lh");
69         pc.setController("controller");
70         pc.setService("service");
71         pc.setServiceImpl("service.impl");
72         pc.setMapper("dao");
73         pc.setEntity("model");
74         pc.setXml("mapper");
75         mpg.setPackageInfo(pc);
76 
77         // 執行生成
78         mpg.execute();
79 
80     }
81 
82 }
復制代碼

編寫完成后執行

 今日感悟:

什么樣的老板絕對不能跟?

總是跟你談前景談情懷,就是不談錢不談調休,

給你畫大餅,用你的“前途”欺騙你,壓榨你。

他們是踩着你往上爬的人,別以為他們會將你的付出記在心里,

他們最擅長的,就是翻臉不認人。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM