欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合MybatisPlus的簡(jiǎn)單教程實(shí)現(xiàn)(簡(jiǎn)單整合)

 更新時(shí)間:2019年05月15日 14:50:03   作者:周兆東  
這篇文章主要介紹了SpringBoot整合MybatisPlus的簡(jiǎn)單教程實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

最近在研究springboot,順便就會(huì)看看數(shù)據(jù)庫(kù)連接這一塊的知識(shí) ,所以當(dāng)我發(fā)現(xiàn)有通用Mapper和MybatisPlus這兩款網(wǎng)絡(luò)上比較火的簡(jiǎn)化mybatis開發(fā)的優(yōu)秀軟件之后。就都想試一下,看看哪一款比較適合自己。

先創(chuàng)建一個(gè)springboot的項(xiàng)目,可以參考我之前的文章Spring Boot 的簡(jiǎn)單教程(一) Spring Boot 項(xiàng)目的創(chuàng)建

創(chuàng)建好springboot之后就需要整合mybatis和mybatis-plus了。

打開pom.xml文件,將最新的mybatis相關(guān)的包都引用進(jìn)來。

    <!-- 這是mysql的依賴 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!-- 這是lombok的依賴 -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <!-- 這是mybatis-plus依賴 -->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.1.1</version>
    </dependency>
    <!-- 這是mybatis-plus的代碼自動(dòng)生成器 -->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-generator</artifactId>
      <version>3.1.1</version>
    </dependency>
    <!-- 這是模板引擎依賴 -->
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.28</version>
    </dependency>

需要對(duì)application.yml進(jìn)行相關(guān)的配置。

  #端口號(hào)
  server:
   port: 8088
  #數(shù)據(jù)庫(kù)的配置信息
  spring:
   datasource:
    url: jdbc:mysql://localhost:3306/*** #自己的數(shù)據(jù)庫(kù)名稱
    username: root
    password: 123456
  mybatis:
   #開啟駝峰命名法
   configuration:
    map-underscore-to-camel-case: true
  mybatis-plus:
   # xml地址
   mapper-locations: classpath:mapper/*Mapper.xml
   # 實(shí)體掃描,多個(gè)package用逗號(hào)或者分號(hào)分隔
   type-aliases-package: ***  #自己的實(shí)體類地址
   configuration:
    # 這個(gè)配置會(huì)將執(zhí)行的sql打印出來,在開發(fā)或測(cè)試的時(shí)候可以用
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

自動(dòng)生成模塊的方法,在相應(yīng)的位置上添加上自己的一些包名就可以運(yùn)行生成相應(yīng)的Entity、Mapper、Mapper XML、Service、Controller 等各個(gè)模塊的代碼。

public class CodeGenerator {

  /**
   * <p>
   * 讀取控制臺(tái)內(nèi)容
   * </p>
   */
  public static String scanner(String tip) {
    Scanner scanner = new Scanner(System.in);
    StringBuilder help = new StringBuilder();
    help.append("請(qǐng)輸入" + tip + ":");
    System.out.println(help.toString());
    if (scanner.hasNext()) {
      String ipt = scanner.next();
      if (StringUtils.isNotEmpty(ipt)) {
        return ipt;
      }
    }
    throw new MybatisPlusException("請(qǐng)輸入正確的" + tip + "!");
  }

  public static void main(String[] args) {
    // 代碼生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("jobob");
    gc.setOpen(false);
    // gc.setSwagger2(true); 實(shí)體屬性 Swagger2 注解
    mpg.setGlobalConfig(gc);

    // 數(shù)據(jù)源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/***?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("***");
    mpg.setDataSource(dsc);

    // 包配置
    PackageConfig pc = new PackageConfig();
    //這里有個(gè)模塊名的配置,可以注釋掉不用。
//    pc.setModuleName(scanner("模塊名"));
    pc.setParent("com.zhouxiaoxi.www");
    mpg.setPackageInfo(pc);

    // 自定義配置
    InjectionConfig cfg = new InjectionConfig() {
      @Override
      public void initMap() {
        // to do nothing
      }
    };

    // 如果模板引擎是 freemarker
    String templatePath = "/templates/mapper.xml.ftl";
    // 如果模板引擎是 velocity
//     String templatePath = "/templates/mapper.xml.vm";

    // 自定義輸出配置
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定義配置會(huì)被優(yōu)先輸出
    focList.add(new FileOutConfig(templatePath) {
      @Override
      public String outputFile(TableInfo tableInfo) {
        // 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱會(huì)跟著發(fā)生變化?。?
        return projectPath + "/src/main/resources/mapper/"
//            + + pc.getModuleName() + 如果放開上面的模塊名,這里就有一個(gè)模塊名了
            + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
      }
    });
    /*
    cfg.setFileCreate(new IFileCreate() {
      @Override
      public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
        // 判斷自定義文件夾是否需要?jiǎng)?chuàng)建
        checkDir("調(diào)用默認(rèn)方法創(chuàng)建的目錄");
        return false;
      }
    });
    */
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);

    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();

    // 配置自定義輸出模板
    //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會(huì)根據(jù)使用的模板引擎自動(dòng)識(shí)別
    // templateConfig.setEntity("templates/entity2.java");
    // templateConfig.setService();
    // templateConfig.setController();

    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    //數(shù)據(jù)庫(kù)表映射到實(shí)體的明明策略
    strategy.setNaming(NamingStrategy.underline_to_camel);
    //數(shù)據(jù)庫(kù)表字段映射到實(shí)體的命名策略, 未指定按照 naming 執(zhí)行
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    //自定義繼承的Entity類全稱,帶包名
//    strategy.setSuperEntityClass("***");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    //自定義繼承的Controller類全稱,帶包名
//    strategy.setSuperControllerClass("***");
    strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(","));
    //自定義基礎(chǔ)的Entity類,公共字段(可添加更多)
//    strategy.setSuperEntityColumns("id");
    //駝峰轉(zhuǎn)連字符
    strategy.setControllerMappingHyphenStyle(true);
    //表前綴
//    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
  }

}

在生成的controller里面添加對(duì)應(yīng)的方法啟動(dòng)就可以正常進(jìn)行訪問了。

當(dāng)然還需要在 Spring Boot 啟動(dòng)類中添加 @MapperScan 注解,掃描 Mapper 文件夾:

@SpringBootApplication
@MapperScan("***.*.mapper") //對(duì)應(yīng)你的mapper存放的地址
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(QuickStartApplication.class, args);
  }

}

 以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot 項(xiàng)目如何在tomcat容器中運(yùn)行的實(shí)現(xiàn)方法

    SpringBoot 項(xiàng)目如何在tomcat容器中運(yùn)行的實(shí)現(xiàn)方法

    這篇文章主要介紹了SpringBoot 項(xiàng)目如何在tomcat容器中運(yùn)行的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • java中實(shí)現(xiàn)excel合并單元格詳細(xì)代碼實(shí)例

    java中實(shí)現(xiàn)excel合并單元格詳細(xì)代碼實(shí)例

    最近的工作中,遇到一個(gè)需求在生成的Excel表格后需要在尾部添加一個(gè)合并的單元格數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于java中實(shí)現(xiàn)excel合并單元格的相關(guān)資料,需要的朋友可以參考下
    2024-06-06
  • IntellJ IDEA神器使用技巧(小結(jié))

    IntellJ IDEA神器使用技巧(小結(jié))

    這篇文章主要介紹了IntellJ IDEA神器使用技巧(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • java LeetCode題解不同路徑

    java LeetCode題解不同路徑

    這篇文章主要為大家介紹了java LeetCode題解不同路徑示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • JavaSwing FlowLayout 流式布局的實(shí)現(xiàn)

    JavaSwing FlowLayout 流式布局的實(shí)現(xiàn)

    這篇文章主要介紹了JavaSwing FlowLayout 流式布局的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • MyEclipse如何取消默認(rèn)工作空間方法示例

    MyEclipse如何取消默認(rèn)工作空間方法示例

    這篇文章主要介紹了MyEclipse如何取消默認(rèn)工作空間,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 一篇文章教帶你了解Java Spring之自動(dòng)裝配

    一篇文章教帶你了解Java Spring之自動(dòng)裝配

    今天小編就為大家分享一篇關(guān)于Spring中的自動(dòng)裝配,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2021-09-09
  • Java實(shí)現(xiàn)批量修改txt文件名稱的方法示例

    Java實(shí)現(xiàn)批量修改txt文件名稱的方法示例

    這篇文章主要介紹了Java實(shí)現(xiàn)批量修改txt文件名稱的方法,結(jié)合實(shí)例形式分析了Java針對(duì)目錄文件遍歷及文件讀寫、屬性操作等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • Spring、SpringMVC和SpringBoot的區(qū)別及說明

    Spring、SpringMVC和SpringBoot的區(qū)別及說明

    這篇文章主要介紹了Spring、SpringMVC和SpringBoot的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2022-10-10
  • 如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證

    如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證

    這篇文章主要介紹了如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證,本文給大家分享最新完美解決方案,感興趣的朋友跟隨小編一起看看吧
    2024-06-06

最新評(píng)論