1. 程式人生 > >SpringBoot整合MyBatis、PageHelper和通用Mapper

SpringBoot整合MyBatis、PageHelper和通用Mapper

之前一直用SSM框架,今天嘗試了一下將MyBatis、PageHelper和通用Mapper進行整合,所以將整合過程記錄作為後續檢視之用。
Mybatis-PageHelper的說明介紹可以點選這裡,一些配置引數與使用介紹可以點選這裡檢視,我在整合這些外掛的時候是參考這篇文章。首先需要在maven中新增相關外掛依賴:

<!--mybatis-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId
>
<version>1.3.1</version> </dependency> <!--mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.1.4</version> </dependency> <!--pagehelper--> <dependency
>
<groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.1</version> </dependency>

然後需要在application.yml檔案中新增自定義配置,原文的配置如下:

mybatis:
    type-aliases-package: tk.mybatis.springboot.model
    mapper-locations: classpath:mapper/*.xml

mapper:
    mappers:
        - tk.mybatis.springboot.util.MyMapper
    not
-empty: false identity: MYSQL pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql

我們可以按需配置自己需要的部分,到這裡外掛基本已經整合完成了。我沒可以利用通用Mapper資料庫操作所需要的MyBatisMapper,關於Mapper的使用可以點選這裡,一開始我在新建了一個Mapper之後,發現在啟動日誌中提示沒有找到相關的物件,後來發現需要使用@MapperScan註解,這樣才可以掃描到MyBatis的Mapper,有一點需要注意的是@MapperScan註解要和@SpringBootApplication註解放在一起,否則會出錯。