pagehelper分页+tk.mybatis的使用

以下是截取部分

pom.xml

<properties>
    <pagehelper.version>1.2.12</pagehelper.version>
</properties>
<dependencies>
    <dependency>
       <groupId>com.github.pagehelper</groupId>
       <artifactId>pagehelper-spring-boot-starter</artifactId>
       <version>${pagehelper.version}</version>
    </dependency>
    <dependency>
       <groupId>tk.mybatis</groupId>
       <artifactId>mapper-spring-boot-starter</artifactId>
       <version>${tk.mybatis.version}</version>
    </dependency> 
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
    </dependency>
</dependencies>

业务逻辑为

如果收件人、手机号不为null且不为空,则根据收件人姓名、手机号分页查询用户信息,根据不为空的字段作为条件查询用户信息,如果查询条件都为空,则查询所有用户

controller层的入参

package com.wanshun.member.dto.input;

import com.wanshun.common.arg.BaseArgAo;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserInfoQueryInDto implements BaseArgAo {

    /**
     * 每页数据量
     */
    private Integer id;

    /**
     * 收件人姓名
     */
    private String userName;

    /**
     * 收件人手机号
     */
    private String userPhone;

    /**
     * 当前页码
     */
    private Integer currentPage;

    /**
     * 每页数据量
     */
    private Integer pageSize;
}

UserInfoDo.java(与数据库中的表对应联系的实体)

@Data
@Table(name = "userinfo")
public class UserInfoDo {
    /**
     * 主键id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "SELECT LAST_INSERT_ID()")
    private Long id;

    /**
     * 用户id
     */
    @Column(name = "userId")
    private Long userId;

    /**
     * 用户姓名
     */
    @Column(name = "userName")
    private String userName;

    /**
     * 用户电话
     */
    @Column(name = "userPhone")
    private String userPhone;
}

Controller.java

RestController(path = "/member/user", module = "用户信息表")
public class UserInfoController extends AbstractManagerBaseController {
    private static Logger logger = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    private UserInfoService userInfoService;

      /**
     * 查询 用户信息列表
     */
    @RequestSelectMapping(op = "getUserInfoList", desc = "用户信息表-条件查询")
    public ResponseResult<PageModel<List<UserInfoQueryOutDto>>> getUserInfoList(@RequestBody UserInfoQueryInDto userInfoQueryInDto) {
        logger.info("用户信息:--->查询:" + JsonUtil.toJson(userInfoQueryInDto));
        PageModel<List<UserInfoQueryOutDto>> pageModel = rpcUserInfoService.getUserInfoList(userInfoQueryInDto);
        return BaseResponse.returnResultSuccess(pageModel);
    }
}

Service.java

@Service
public class UserInfoServiceImpl implements UserInfoService {
    private static final Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);

    @Resource
    private UserInfoMapper userInfoMapper;

     @Override
    public PageModel<List<UserInfoQueryOutDto>> getUserInfoList(UserInfoQueryInDto userInfoQueryInDto) {
        logger.info("用户列表 入参:" + JsonUtil.toJson(userInfoQueryInDto));
        Example example = new Example(UserInfoDo.class);

        Example.Criteria criteria = example.createCriteria();

        if (!StringUtil.isEmpty(userInfoQueryInDto.getUserPhone())) {
            criteria.andEqualTo("userPhone", userInfoQueryInDto.getUserPhone());
        }

        if (!StringUtil.isEmpty(userInfoQueryInDto.getUserName())) {
            criteria.andEqualTo("userName", userInfoQueryInDto.getUserName());
        }
        List<UserInfoQueryOutDto> userInfoQueryOutDtos = new ArrayList<>(userInfoQueryInDto.getPageSize()); //真正要返回给前端的List
        Page<UserInfoDo> pageInfo = PageHelper.startPage(userInfoQueryInDto.getCurrentPage(), userInfoQueryInDto.getPageSize()); //PageHelper.startPage(currentPage,pageSize),返回类型与数据库中的表名一样【因为我这里用的是通用Mapper,如果不用通用的mapper的话,则应该是跟mapper中的sql语句中的查询库里返回数据类型一致】
        List<UserInfoDo> userInfoDos = userInfoMapper.selectByExample(example);//tk通用mapper根据条件查询list记录,无需再在mapper.xml中写sql,更灵活

        if (userInfoDos != null && !userInfoDos.isEmpty()) {
            UserInfoQueryOutDto userInfoQueryOutDto;
            for (UserInfoDo userInfoDo : userInfoDos) {
                userInfoQueryOutDto = new UserInfoQueryOutDto();
                BeanUtils.copyProperties(userInfoDo, userInfoQueryOutDto);

                userInfoQueryOutDtos.add(userInfoQueryOutDto);
            }
        }

        PageModel<List<UserInfoQueryOutDto>> pageModel = new PageModel<>();//PageModel<T>中的T为要返回的类型
        pageModel.setBody(userInfoQueryOutDtos);//PageModel里的body为真正要返回的内容
        pageModel.setCurrentPage(userInfoQueryInDto.getCurrentPage());
        pageModel.setPageSize(userInfoQueryInDto.getPageSize());
        pageModel.setCount(pageInfo.getTotal());//根据PageInfo得到从库里查的满足条件的记录数量
        return pageModel;
    }
}

在PostMan中测试

pagehelper分页+tk.mybatis的使用

pagehelper分页+tk.mybatis的使用 

上一篇:tk.mybatis 调用oracle,生成ID


下一篇:坦克大战!