Springboot整合MybatisPlus及分页功能

1 引入pom

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>2.7.14</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.29</version>
</dependency>

2 编写配置类MybatisPlusConfig

java 复制代码
@Configuration
public class MybatisPlusConfig {
    /**
     * 分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

3 业务实现

3.1 Controller层

java 复制代码
    @Autowired
    IComponentService componentService;

    @ApiOperation(value = "查询基础组件列表", notes = "根据条件查询基础组件列表")
    @GetMapping("/list")
    public ResponseMessage<IPage<Component>> queryList(@RequestBody QueryEntity<ComponentDTO> queryEntity){
        IPage<Component> page = new Page<>(queryEntity.getPageNum(), queryEntity.getPageSize());
        LambdaQueryWrapper<Component> query = new LambdaQueryWrapper<>();
//        query.eq(Component::getType, queryEntity.getQuery().getType());
        return ResponseMessage.success("查询成功",componentService.queryList(page,query));
    }

3.2 service层

java 复制代码
IPage<Component> queryList(IPage<Component> page, LambdaQueryWrapper<Component> queryWrapper);

3.3 service实现层

java 复制代码
	@Override
    public IPage<Component> queryList(IPage<Component> page, LambdaQueryWrapper<Component> queryWrapper) {

        return componentMapper.selectPage(page,queryWrapper);
    }

3.4 dao层

java 复制代码
@Mapper
public interface ComponentMapper extends BaseMapper<Component> {

}

4 yaml配置

yaml 复制代码
server:
  #运行端口号
  port: 8090
  servlet:
    context-path: /hom

spring:
#  mvc:
#    pathmatch:
#      matching-strategy: ANT_PATH_MATCHER
  #数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/hom?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    username: root
    password: marlon

#mybatis-plus配置
mybatis-plus:
  configuration:
    #sql日志打印
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #开启驼峰命名匹配
    map-underscore-to-camel-case: true
    #mapper文件
  mapper-locations: classpath:mapper/**/*.xml,classpath:mapper/*.xml
  #数据库实体类的包全路径,方便在mapper.xml中不许使用实体类的全路径,写类名就行(不区分大小写)
  type-aliases-package: cn.com.marlon.hom.entity
  global-config:
    db-config:
      #逻辑删除
      logic-delete-value: 0
      logic-not-delete-value: 1
      logic-delete-field: deleted

5 postman测试

相关推荐
dreamxian4 小时前
苍穹外卖day11
java·spring boot·后端·spring·mybatis
怒放吧德德9 小时前
Spring Boot实战:InfluxDB 2.x简单教程
java·spring boot·后端
indexsunny9 小时前
互联网大厂Java面试实战:核心技术与业务场景深度解析
java·spring boot·hibernate·security·microservices·interview
希望永不加班11 小时前
SpringBoot 内置服务器(Tomcat/Jetty/Undertow)切换
服务器·spring boot·后端·tomcat·jetty
yymboss12 小时前
【JavaEE】Spring Boot 项目创建
java·spring boot·java-ee
sxhcwgcy13 小时前
快速在本地运行SpringBoot项目的流程介绍
java·spring boot·后端
cjy00011113 小时前
SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)
spring boot·tomcat·mybatis
毕设源码-郭学长13 小时前
【开题答辩全过程】以 基于Spring Boot“活力青春”健身房管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
毕设源码-钟学长13 小时前
【开题答辩全过程】以 基于SpringBoot的校园快递APP系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
LSL666_14 小时前
IService——查询(下)
java·开发语言·数据库·mybatisplus·iservice