Spring Boot整合Mybatis+PageHelper分页插件
Pom.xml
基础的依赖我就不再这里给大家一个展示出来了,把用到的依赖给大家展示出来
<!-- Mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- 分页依赖 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<build>
<!-- 扫描mapper包 -->
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>false</filtering>
<includes>
<include>**/mapper/*.xml</include>
</includes>
</resource>
</resources>
</build>
application.yml
配置文件这里用的是.yml文件
server:
port: 8899
# database
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/test?useUnicode=true&&characterEncoding=utf-8&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
# pagehelper
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countsql
# mapper
mybatis:
# 扫描mapper
mapper-locations: classpath:com/XXX/*/mapper/*.xml
# 打印Sql语句
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 驼峰命名法与数据库字段有下划线的对应
map-underscore-to-camel-case: true
启动入口
@SpringBootApplication
@MapperScan("com.XXX.dao")
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
加入了@MapperScan注解 用来扫描dao层接口 不加入的话则会找不到dao层里面的接口
项目的目录结构
现在采用的把Mapper层放到了java目录里面,没有放到resources目录下
下面将通过一个案例展示分页
实体类层:
dao层:
service层:
serviceImpl层:
controller层:
查询结果:
路径:http://localhost:8899/user/findUser
后台打印的sql语句: