您的位置:首页 > 移动开发

SpringBoot集成MyBatis、PageHelper和通用Mapper

2017-12-28 21:42 846 查看
之前一直用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
注解放在一起,否则会出错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: