spring boot 集成 postgresql mybatis-plus swagger pagehelper
为了在Spring Boot项目中集成PostgreSQL、MyBatis-Plus、Swagger和PageHelper,你需要按照以下步骤操作:
- 在
pom.xml
中添加相关依赖:
<!-- Spring Boot Starter for PostgreSQL -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version> <!-- 替换为实际版本号 -->
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.x.x</version> <!-- 替换为实际版本号 -->
</dependency>
<!-- PageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.x.x</version> <!-- 替换为实际版本号 -->
</dependency>
- 配置数据源和MyBatis-Plus:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/your_database
username: your_username
password: your_password
driver-class-name: org.postgresql.Driver
mybatis-plus:
mapper-locations: classpath:/mappers/**/*.xml
type-aliases-package: com.yourpackage.model
- 配置Swagger:
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
- 使用PageHelper进行分页:
@Autowired
private PageHelper pageHelper;
public IPage<YourEntity> selectPage(int pageNum, int pageSize) {
Page<YourEntity> page = pageHelper.startPage(pageNum, pageSize);
List<YourEntity> list = yourMapper.selectList(null);
return page.setRecords(list);
}
确保你的Mapper接口继承了\`BaseMapper<You
评论已关闭