亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

打開pom.xml文件,將最新的mybatis相關(guā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的代碼自動生成器 -->
    <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>

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

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

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

public class CodeGenerator {

  /**
   * <p>
   * 讀取控制臺內(nèi)容
   * </p>
   */
  public static String scanner(String tip) {
    Scanner scanner = new Scanner(System.in);
    StringBuilder help = new StringBuilder();
    help.append("請輸入" + tip + ":");
    System.out.println(help.toString());
    if (scanner.hasNext()) {
      String ipt = scanner.next();
      if (StringUtils.isNotEmpty(ipt)) {
        return ipt;
      }
    }
    throw new MybatisPlusException("請輸入正確的" + 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); 實體屬性 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();
    //這里有個模塊名的配置,可以注釋掉不用。
//    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<>();
    // 自定義配置會被優(yōu)先輸出
    focList.add(new FileOutConfig(templatePath) {
      @Override
      public String outputFile(TableInfo tableInfo) {
        // 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱會跟著發(fā)生變化?。?
        return projectPath + "/src/main/resources/mapper/"
//            + + pc.getModuleName() + 如果放開上面的模塊名,這里就有一個模塊名了
            + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
      }
    });
    /*
    cfg.setFileCreate(new IFileCreate() {
      @Override
      public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
        // 判斷自定義文件夾是否需要創(chuàng)建
        checkDir("調(diào)用默認(rèn)方法創(chuàng)建的目錄");
        return false;
      }
    });
    */
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);

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

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

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

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    //數(shù)據(jù)庫表映射到實體的明明策略
    strategy.setNaming(NamingStrategy.underline_to_camel);
    //數(shù)據(jù)庫表字段映射到實體的命名策略, 未指定按照 naming 執(zhí)行
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    //自定義繼承的Entity類全稱,帶包名
//    strategy.setSuperEntityClass("***");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    //自定義繼承的Controller類全稱,帶包名
//    strategy.setSuperControllerClass("***");
    strategy.setInclude(scanner("表名,多個英文逗號分割").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里面添加對應(yīng)的方法啟動就可以正常進行訪問了。

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

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

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

}

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

相關(guān)文章

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

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

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

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

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

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

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

    java LeetCode題解不同路徑

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

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

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

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

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

    一篇文章教帶你了解Java Spring之自動裝配

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

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

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

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

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

    如何使用IntelliJ IDEA的HTTP Client進行接口驗證

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

最新評論