springboot整合mybatis-plus实现多表分页查询

springboot整合mybatis-plus实现多表分页查询

1.新建一个springboot工程
2.需要导入mybatis和mybatis-plus的依赖文件

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>
         <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

3.application.yml配置文件

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC
    username: root
    password: 数据库密码
mybatis:
  mapper-locations: classpath*:mapper/*.xml

mybatis-plus:
  mapper-locations: classpath:/mapper/*Mapper.xml
logging:
  level:
    com.tuanzi.*: debug

4.首先我们需要写一个类来配置分页插件

省略import
@EnableTransactionManagement
@Configuration
@MapperScan("com.tuanzi.*.mapper*")
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }
}

5.controller类

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 多表关联,分页查询(1对1)
     * @param page
     * @return
     */
    @RequestMapping("/findAll")
    public Result<IPage<User>> findAll(@RequestBody Page<User> page){

         return userService.pages(page);

    }

    /**
     * 多表关联,分页查询(1对多)
     * @param page
     * @return
     */
    @RequestMapping("/selectAll")
    public Result<IPage<User>> selectAll(@RequestBody Page<User> page){

        return userService.pageList(page);

    }

}

6.service类

public interface UserService extends IService<User> {

    Result<IPage<User>> pages(Page<User> page);

    Result<IPage<User>> pageList(Page<User> page);
}

7.service实现类

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public Result<IPage<User>> pages(Page<User> page) {
        IPage<User> userIPage = userMapper.Pages(page);
        return Result.getSuccess("分页查询成功",userIPage);
    }

    @Override
    public Result<IPage<User>> pageList(Page<User> page) {
        IPage<User> userIPage = userMapper.pageList(page);
        return Result.getSuccess("分页查询成功",userIPage);
    }
}

8.mapper接口

注意!!: 如果入参是有多个,需要加注解指定参数名才能在xml中取值
page 为分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位(你可以继承Page实现自己的分页对象)

@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {

    IPage<User> Pages(@Param("page") Page<User> page);

    IPage<User> pageList(@Param("page") Page<User> page);
}

9.xml文件

一对一关联

 <!--  一对一 通用查询映射结果 -->
    <resultMap id="BaseResultMap1" type="com.tuanzi.user.entity.User">
        <result column="id" property="id" />
        <result column="name" property="name" />
        <result column="age" property="age" />
        <result column="email" property="email" />
        <!--assocication    一对一关联查询
               可以指定联合的JavaBean对象
               property="work"指定哪个属性是联合的对象
               javaType:指定这个属性对象的类型
           -->
        <association property="work" javaType="com.tuanzi.user.entity.Work">
            <result column="id" property="id" />
            <result column="position" property="position" />
            <result column="user_id" property="userId" />
        </association>
    </resultMap>

一对多关联

<!-- 一对多  通用查询映射结果 -->
    <resultMap id="BaseResultMap2" type="com.tuanzi.user.entity.User">
        <result column="id" property="id" />
        <result column="name" property="name" />
        <result column="age" property="age" />
        <result column="email" property="email" />
        <!--
		collection定义关联结合类型的属性的封装规则
		property="workList"指定哪个属性是联合的对象
		ofType:指定集合里面元素的类型
		-->
       <collection property="workList" ofType="com.tuanzi.user.entity.Work">
           <result column="id" property="id" />
           <result column="position" property="position" />
           <result column="user_id" property="userId" />
       </collection>
    </resultMap>

SQL语句:

<select id="Pages" resultMap="BaseResultMap1">
        select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id
    </select>
    <select id="pageList" resultMap="BaseResultMap2">
        select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id
    </select>

10.这样就基本完成了!我这里省略了实体类
我们运行一下,用postman测试一下结果
这里我们需要传2个参数,当然我们也可以不用传,因为mybatis-plus有默认值
来看下mybatis-plus的page源码
在这里插入图片描述
效果图:
在这里插入图片描述
在这里插入图片描述

OK

最后附赠源码地址:demo

  • 3
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
您好!关于您的问题,我可以帮您解答。针对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实现多表分页查询的具体步骤,希望能对您有所帮助!如果您有其他问题,欢迎继续提问。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值