之前一直用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
注解放在一起,否则会出错。