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

相关推荐
十年Java程序媛11 小时前
Java 接口和抽象类对比|Java8 新特性,抛弃老旧八股,正确选型
java·spring boot·后端
for_ever_love__12 小时前
MySQL 高频面试题 30 问:索引、事务、锁、日志、主从与分库分表(MySQL 8.0 版)
java·数据库·spring boot·mysql·spring cloud·面试·总结
SL_staff13 小时前
设备越多系统越崩?根源在三层抽象失效
spring boot·物联网·github
威哥爱编程13 小时前
【AI全栈12-01】Spring Boot 为何是 AI 全栈后端优选
java·spring boot·后端
威哥爱编程13 小时前
【AI全栈12-03】Spring Boot 用多模型路由把智能客服成本降下来
java·spring boot·后端
威哥爱编程13 小时前
【AI全栈12-02】Spring Boot 跑通第一个 AI 对话接口:HR 政策问答机器人实战
java·spring boot·后端
m0_5873830014 小时前
深圳 24 小时自助健身房系统软件开发实战指南与案例解析
java·spring boot·小程序·架构·需求分析
企业数字化笔记15 小时前
AI工具参数很多怎么办?预设、表单校验、危险参数与配置审计
java·spring boot·python·音视频
huaweichenai16 小时前
spring boot对接阿里短信
java·spring boot
马剑威(威哥爱编程)16 小时前
【AI全栈后端12-08】Spring Boot 用 MCP 统一接入内部系统:让 AI 接一次,全公司复用
java·人工智能·spring boot