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测试

相关推荐
gelald9 小时前
SpringBoot - Actuator与监控
java·spring boot·后端
我登哥MVP10 小时前
【Spring6笔记】 - 11 - JDBCTemplate
java·数据库·spring boot·mysql·spring
希望永不加班10 小时前
SpringBoot 自定义 Starter:从零开发一个私有 Starter
java·spring boot·后端·spring·mybatis
悟空码字11 小时前
别再System.out了!这份SpringBoot日志优雅指南,让你告别日志混乱
java·spring boot·后端
一 乐11 小时前
工会管理|基于springboot + vue工会管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·工会管理系统
ffqws_11 小时前
Spring Boot:用JWT令牌和拦截器实现登录认证(含测试过程和关键注解讲解)
java·spring boot·后端
yxl_num12 小时前
Docker 完整部署一个包含 Spring Boot(依赖 JDK)、MySQL、Redis、Nginx 的整套服务
java·spring boot·docker
一只幸运猫.12 小时前
用户58856854055的头像[特殊字符]Spring Boot 多模块项目中 Parent / BOM / Starter 的正确分工
java·spring boot·后端
程序员阿明13 小时前
spring boot3识别PDF图纸
java·spring boot·后端·pdf
阿虎儿15 小时前
Spring Boot 4常用依赖包解析与场景搭配
spring boot