SpringBoot整合MyBatis-Plus(基础整合、SQL执行分析、分页插件)

前期准备

首先创建一个web项目,并整合lombok。可以参考上一篇文章SpringBoot快速构建web项目-多模块项目

1.基础整合

1.1添加依赖

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.10</version>
    </dependency>
    <!--mysql-connector-java-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--jdbc-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

1.2改yml

spring:
# DataSource Config
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    url: jdbc:mysql://localhost:3306/accountbook?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

mybatis-plus:
  configuration:
    # 是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射。
    map-underscore-to-camel-case: false
  mapper-locations: classpath:/mapper/*.xml

1.3添加配置类

@Configuration
@MapperScan("com.fgrapp.learn.dao") //dao层接口所在路径
public class MybatisPlusConfig {
   
}

1.4 编写业务类

1.4.1 建表语句

CREATE TABLE `salesbill` (
  `billId` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
  `date` date NOT NULL COMMENT '日期',
  `customerID` int(11) NOT NULL COMMENT '销售客户',
  `categoryID` 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于您的问题,我可以帮您解答。针对springboot整合mybatis-plus实现多表分页查询实现,可以按照以下步骤进行: 1.在pom.xml文件中添加Mybatis-Plus和Pagehelper的依赖,如下: ``` <!-- Mybatis-Plus依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <!-- Pagehelper依赖 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> ``` 2.在Mybatis-Plus的配置文件中,指定分页插件。如下: ``` @Configuration @MapperScan("com.example.mapper") public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } ``` 3.编写Mapper和对应的Mapper.xml文件,进行多表联合查询,并在Mapper接口方法上添加分页参数。如下: ``` //在Mapper接口方法上添加分页参数 public interface UserMapper extends BaseMapper<User> { List<User> selectUserPage(Page<User> page); } <!-- 在Mapper.xml中编写多表联合查询SQL语句 --> <select id="selectUserPage" resultMap="BaseResultMap"> select u.*, r.role_name from user u left join user_role ur on u.id = ur.user_id left join role r on ur.role_id = r.id <where> <if test="username != null and username != ''"> and u.username like concat('%',#{username},'%') </if> </where> </select> ``` 4.在Controller层中,接受分页参数并返回分页结果。如下: ``` @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public Page<User> selectUserPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, String username) { Page<User> p = new Page<>(page, size); p = userMapper.selectUserPage(p, username); return p; } } ``` 以上就是整合Mybatis-Plus和Pagehelper实现多表分页查询的具体步骤,希望能对您有所帮助!如果您有其他问题,欢迎继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值