springboot整合mybatis-plus多条件模糊查询及分页

本文展示了如何使用 MyBatis Plus 的 PageHelper 插件进行分页和模糊查询。通过 `QueryWrapper` 创建查询条件,结合 `PageHelper.startPage()` 进行分页设置,然后调用 `service` 方法获取数据并返回 `PageInfo` 对象,实现了多条件的灵活查询功能。

简单德导包省略
分页运用的pagehelper插件。完全不用写sql语句

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wei.pojo.File;
import com.wei.service.FileService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;

/**
 * @author weijq
 * @since 2021-03-29
 */
@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;
    /**
     * 模糊查询加分页
     * @param pageNum
     * @param pageSize
     * @param fileName
     * @return
     */
    @GetMapping("likeFind")
    public PageInfo likeFind(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                             @RequestParam(value = "pageSize",required = false,defaultValue = "2")Integer pageSize,
                             String fileName){
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();

        queryWrapper.like("file_name",fileName);
        System.out.println(queryWrapper);
        PageHelper.startPage(pageNum,pageSize);
        List<File> files = fileService.likeFind(queryWrapper);
        System.out.println(files);
        PageInfo<File> pageInfo = new PageInfo<>(files);
        return pageInfo;
    }
    /**
     * 实现多条件模糊查询
     * @param pageNum
     * @param pageSize
     * @param fileName 模糊条件
     * @param fileZhaiyao 模糊条件
     * @return
     */
    @RequestMapping("selectMapsByPage")
    public PageInfo selectMaps(@RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
                               @RequestParam(value = "pageSize",defaultValue = "2",required = false) Integer pageSize,
            String fileName,String fileZhaiyao){

        QueryWrapper<File> queryWrapper = new QueryWrapper<>();
        queryWrapper = new QueryWrapper();
        if (""!=fileName&&fileName!=null){
            queryWrapper.like("file_name",fileName);
        }
        if (""!=fileZhaiyao&&fileZhaiyao!=null){
            queryWrapper.like("file_zhaiyao",fileZhaiyao);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> list = fileService.selectMaps(queryWrapper);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}

好的,下面是 Spring Boot 整合 MyBatis-Plus 进行分页查询的示例代码: 首先,需要在 pom.xml 中添加 MyBatis-Plus 和 MySQL 的依赖: ```xml <dependencies> <!-- MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 然后,需要在 application.properties 中配置 MySQL 数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 接下来,创建一个实体类和对应的 Mapper 接口: ```java @Data public class User { private Long id; private String name; private Integer age; private String email; } @Mapper public interface UserMapper extends BaseMapper<User> { IPage<User> selectUserPage(Page<User> page, @Param("name") String name); } ``` 其中,User 类是实体类,UserMapper 是 Mapper 接口,继承了 MyBatis-Plus 的 BaseMapper 接口。 注意,在 UserMapper 接口中,我们定义了一个名为 selectUserPage 的方法,它接受一个 Page 对象和一个 name 参数,返回一个 IPage 对象。这个方法将在 Controller 中调用,用于分页查询用户信息。 最后,编写 Controller 类,通过调用 UserMapper 中定义的 selectUserPage 方法来进行分页查询: ```java @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public IPage<User> getUserList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) String name) { Page<User> page = new Page<>(pageNum, pageSize); return userMapper.selectUserPage(page, name); } } ``` 在 getUserList 方法中,我们首先创建了一个 Page 对象,表示要查询的分页信息。然后,调用 userMapper.selectUserPage 方法来进行分页查询,并将结果返回给前端。 最后,这里提供一个 Mapper.xml 文件的示例,用于实现分页查询: ```xml <select id="selectUserPage" resultMap="BaseResultMap"> select * from user <if test="name != null"> where name like concat('%', #{name}, '%') </if> <if test="orderBy != null"> order by ${orderBy} </if> </select> ``` 其中,selectUserPage 方法的实现与上面的示例代码类似,这里不再赘述。注意,这里使用了 MyBatis 的动态 SQL 功能,根据传入的参数来动态生成 SQL 语句。具体来说,如果传入了 name 参数,则会在 SQL 语句中添加一个 where 子句,用于按照用户名进行模糊查询;如果传入了 orderBy 参数,则会在 SQL 语句中添加一个 order by 子句,用于按照指定字段排序。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值