千家信息网

Springboot+Mybatis怎么实现分页加条件查询功能

发表于:2025-02-11 作者:千家信息网编辑
千家信息网最后更新 2025年02月11日,本篇内容介绍了"Springboot+Mybatis怎么实现分页加条件查询功能"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家
千家信息网最后更新 2025年02月11日Springboot+Mybatis怎么实现分页加条件查询功能

本篇内容介绍了"Springboot+Mybatis怎么实现分页加条件查询功能"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

User.xml

    -- 这里的id为函数名        update user                                    username=#{username},                                        nickname=#{nickname},                                        email=#{email},                                        phone=#{phone},                                        address=#{address}                                        id = #{id}                                               1=1                            and username like concat("%",#{username},"%")                                        and email like concat("%",#{email},"%")                                        and address like concat("%",#{address},"%")                        

UserMapper.java

package com.shelbourne.schooldelivery.mapper; import com.shelbourne.schooldelivery.entity.User;import org.apache.ibatis.annotations.*; import java.util.List; @Mapperpublic interface UserMapper {     //查询所有用户    @Select("select * from user")    //mybatis提供注解,注意SQL语句后不能加分号    List findAll();     //新增用户    @Insert("insert into user(username,password,nickname,email,phone,address)" +            "values(#{username},#{password},#{nickname},#{email},#{phone},#{address})")    public Integer insert(User user);     //通过注解(静态)和xml里面(动态)两种方式编写SQL语句    int update(User user);     //删除单个用户    @Delete("delete from user where id=#{id}")    Integer deleteById(@Param("id") Integer id);//最后加上@Param参数,参数名和上面的#{}里面的一样     //查询记录条数    @Select("select count(*) from user")    Integer selectTotal();     //编写动态SQL实现分页查询+条件查询    //查询满足条件的某一页用户    List selectPageWithParam(Integer startIdx, Integer size, String username, String email, String address);     //查询满足条件的所有用户数    int selectTotalWithParam(String username, String email, String address);}

UserController.java

package com.shelbourne.schooldelivery.controller; import com.shelbourne.schooldelivery.entity.User;import com.shelbourne.schooldelivery.mapper.UserMapper;import com.shelbourne.schooldelivery.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*; import java.util.HashMap;import java.util.List;import java.util.Map; @RequestMapping("/user")  //统一给接口加前缀,postman后台接口localhost:9090/user@RestControllerpublic class UserController {     @Autowired  //注入其他类的注解    private UserMapper userMapper;     @Autowired    private UserService userService;     //查询所有用户    @GetMapping    public List findAll(String username) {        return userMapper.findAll();    }     //通过POST请求进行新增和更新操作    @PostMapping    public Integer save(@RequestBody User user) {//一定要加上RequestBody,可以把前端传回的JSON对象转换为Java对象        return userService.save(user);    }     //删除请求接口    @DeleteMapping("/{id}")    public Integer delete(@PathVariable Integer id) {//这里的"id"必须和DeleteMapping里面的名字一样        return userMapper.deleteById(id);    }     @GetMapping("/page")    public Map findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String username,                                        @RequestParam String email, @RequestParam String address) {        int startIdx = (pageNum - 1) * pageSize, size = pageSize;        List data = userMapper.selectPageWithParam(startIdx, size, username, email, address);//获取一页的数据        int total = userMapper.selectTotalWithParam(username, email, address);//查询总条数        Map res = new HashMap<>();        res.put("data", data);//表格数据        res.put("total", total);//分页使用        return res;    }}

Home.vue中:

"Springboot+Mybatis怎么实现分页加条件查询功能"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0