SpringBoot + Mybatis + 达梦数据库 + PageHelper 实现分页
在Spring Boot项目中使用MyBatis和PageHelper插件实现分页功能,首先需要添加相关依赖,并进行配置。
- 添加依赖到
pom.xml
:
<dependencies>
<!-- 添加Spring Boot Starter的MyBatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- 添加达梦数据库JDBC驱动依赖 -->
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver18</artifactId>
<version>8.1.1.114</version>
</dependency>
<!-- 添加PageHelper分页插件依赖 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
</dependencies>
- 配置
application.properties
或application.yml
:
# 配置数据源
spring.datasource.url=jdbc:dm://localhost:5236/DATABASE
spring.datasource.username=USERNAME
spring.datasource.password=PASSWORD
spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
# 配置MyBatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.yourpackage.model
- 在MyBatis的Mapper接口中使用PageHelper进行分页:
public interface YourModelMapper {
List<YourModel> selectByCondition(YourModel condition);
}
- 在Service层中使用PageHelper进行分页:
@Service
public class YourModelService {
@Autowired
private YourModelMapper yourModelMapper;
public PageInfo<YourModel> queryByCondition(int pageNum, int pageSize, YourModel condition) {
PageHelper.startPage(pageNum, pageSize);
List<YourModel> list = yourModelMapper.selectByCondition(condition);
return new PageInfo<>(list);
}
}
- 在Controller中调用Service层的分页方法:
@RestController
@RequestMapping("/yourmodels")
public class YourModelController {
@Autowired
private YourModelService yourModelService;
@GetMapping("/list")
public PageInfo<YourModel> getList(@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
return yourModelService.queryByCondition(pageNum, pageSize, new YourModel());
}
}
以上代码展示了如何在Spring Boot项目中结合MyBatis和PageHelper实现分页功能。在实际使用时,需要根据自己的数据库表结构和查询条件调整Mapper XML文件和对应的Mapper接口。
评论已关闭