Spring Boot集成mybatis-plus和pageHelper

版本號

框架 版本
spring boot 2.2.1.RELEASE
mybatis 1.2.3
mybatis-plus 2.0.1
pagehelper 1.2.3

引入maven依賴

引入相關的依賴

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.2.0</version>
</dependency>

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.3</version>
</dependency>

添加配置

配置application.yml

mybatis-plus:
  mapper-locations: classpath*:mapper/**.xml
  #實體掃描,多個package用逗號或者分號分隔
  #  typeAliasesPackage: com.pm.facade.*.entity
  #  typeEnumsPackage: com.baomidou.springboot.db.entity.enums
  global-config:
    db-config:
      #主鍵類型  0:"數據庫ID自增", 1:"用戶輸入ID",2:"全局唯一ID (數字類型唯一ID)", 3:"全局唯一ID UUID";
      id-type: AUTO
      #字段策略 0:"忽略判斷",1:"非 NULL 判斷"),2:"非空判斷"
      field-strategy: not_empty
      #駝峯下劃線轉換
      column-underline: true
      #數據庫大寫下劃線轉換
      #capital-mode: true
      #邏輯刪除配置
      logic-delete-value: Y
      logic-not-delete-value: N
      db-type: mysql
      #刷新mapper 調試神器
    refresh: true
      #自定義填充策略接口實現
      #meta-object-handler: com.baomidou.springboot.xxx
    #自定義SQL注入器
  #sql-injector: com.baomidou.springboot.xxx
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false

pagehelper:
  reasonable: true # 禁用合理化時
  support-methods-arguments: true
  params: count=countSql
  row-bounds-with-count: true
  helper-dialect: mysql

啓動類上配置

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//掃包地址
@MapperScan(basePackages = "com.puhua.platform.dao")
@SpringBootApplication
public class PlatformApplication {

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

}

建議

使用mybais-plus建議使用mybatis-plus-generator逆向工程生成對應的mapper和實體類

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章