springboot-3.2.1 集成 mybatis、分页插件pagehelper和接口文档swagger
为了在Spring Boot 3.2.1中集成MyBatis、PageHelper和Swagger,你需要按照以下步骤操作:
- 在
pom.xml
中添加依赖:
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.1</version>
</dependency>
<!-- PageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
- 配置
application.properties
或application.yml
文件,添加数据库连接信息和PageHelper的配置:
# 数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# PageHelper 配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
- 创建Mapper接口和对应的XML文件,并使用PageHelper进行分页:
// Mapper接口
public interface YourMapper {
List<YourEntity> selectAll(@Param("param") Map<String, Object> param);
}
// XML映射文件
<mapper namespace="YourMapper">
<select id="selectAll" resultType="YourEntity">
SELECT * FROM your_table
WHERE some_condition = #{param.someCondition}
</select>
</mapper>
// Service中使用PageHelper进行分页
public PageInfo<YourEntity> getPage(int pageNum, int pageSize, Map<String, Object> param) {
PageHelper.startPage(pageNum, pageSize);
List<YourEntity> list = yourMapper.selectAll(param);
return new PageInfo<>(list);
}
- 配置Swagger。创建一个配置类来指定Swagger的相关配置:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
- 在Controller中使用Swagger注解来描述API:
@RestController
@Api(tags = "Your Controlle
评论已关闭