对于Mybatis而言,我们可以使用XML或者使用注解来编写我们的SQL语句
<select id="listUserByCondition" resultType="com.yang.entity.User">
select id, name, phone, email, profession, age, gender, status, create_time
from tb_user
<where>
<if test="name != null and name != ''">
and name like concat('%', #{name}, '#')
</if>
<if test="profession != null and profession != null">
and profession like concat('%', #{profession}, '%')
</if>
<if test="createTimeMin != null and createTimeMin != ''">
and create_time >= #{createTimeMin}
</if>
<if test="createTimeMax != null and createTimeMax">
and create_time < #{createTimeMax}
</if>
</where>
<if test="orderByColumn != null and orderByColumn != ''">
order by ${orderByColumn} ${isAsc}
</if>
limit ${pageNum} ${pageSize}
</select>
由于业务逻辑可能会发生变化,若该表的结构在后续发生了变化,我们需要对原先编写的sql进行修改,但是由于sql是运行时才能发现,无法及时的发现所有需要修改的地方,可能会造成访问错误等问题。而且该sql的编写比较繁琐。
在MP中,我们可以使用wrapper来构造条件,用page自动进行分页操作,使用MP自带的sql语句,大大减少了我们的代码量,同时可以将经理集中在where条件中。
public PageResVO<User> list1(UserDTO userDTO) {
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
String orderBy = StringUtils.isBlank(userDTO.getIsAsc()) ? "" : userDTO.getIsAsc();
wrapper.like(StringUtils.isNotBlank(userDTO.getName()), User::getName, userDTO.getName())
.like(StringUtils.isNotBlank(userDTO.getProfession()), User::getProfession, userDTO.getProfession())
.ge(Objects.nonNull(userDTO.getCreateTimeMin()), User::getCreateTime, userDTO.getCreateTimeMin())
.lt(Objects.nonNull(userDTO.getCreateTimeMax()), User::getCreateTime, userDTO.getCreateTimeMax())
.last(StringUtils.isNotBlank(userDTO.getOrderByColumn()),
Constant.ORDER_BY_BLANK + StringUtils.camelToUnderline(userDTO.getOrderByColumn()) + " " + orderBy);
IPage<User> page = this.baseMapper.selectPage(new Page<>(userDTO.getPageNum(), userDTO.getPageSize()), wrapper);
return PageResVO.getBean(page, page.getRecords());
}
但是在业务中,我们一般的入参是一个DTO,而返回的是一个VO,对于这种情况,有三种不同的写法
XML
Page<UserVO> list(Page<User> page, @Param("dto") UserDTO dto);
<select id="list" resultType="com.yang.vo.UserVO">
select id, name, phone, email, profession, age, gender, status, create_time
from tb_user
<where>
<if test="dto.name != null and dto.name != ''">
and name like concat('%', #{dto.name}, '#')
</if>
<if test="dto.profession != null and dto.profession != null">
and profession like concat('%', #{dto.profession}, '%')
</if>
<if test="dto.createTimeMin != null and dto.createTimeMin != ''">
and create_time >= #{dto.createTimeMin}
</if>
<if test="dto.createTimeMax != null and dto.createTimeMax">
and create_time < #{dto.createTimeMax}
</if>
</where>
<if test="dto.orderByColumn != null and dto.orderByColumn != ''">
order by ${dto.orderByColumn} ${dto.isAsc}
</if>
</select>
@Override
public PageResVO<UserVO> list1(UserDTO userDTO) {
Page<UserVO> page = new Page<>(userDTO.getPageNum(), userDTO.getPageSize());
Page<UserVO> list = this.baseMapper.list(page, userDTO);
return PageResVO.getBean(list);
}
这种方式相对于Mybatis而言简化了分页操作,将分页交给了MP的page
XML+Wrapper
Page<UserVO> list1(Page<UserVO> page, @Param(Constants.WRAPPER) QueryWrapper wrapper);
<select id="list1" resultType="com.yang.vo.UserVO">
select name, profession, age, status, create_time
from tb_user
<where>
<if test="ew.sqlSegment != null and ew.sqlSegment != ''">
${ew.sqlSegment}
</if>
</where>
</select>
@Override
public PageResVO<UserVO> list1(UserDTO userDTO) {
Page<UserVO> page = new Page<>(userDTO.getPageNum(), userDTO.getPageSize());
QueryWrapper<UserDTO> wrapper = new QueryWrapper<>();
String orderBy = StringUtils.isBlank(userDTO.getIsAsc()) ? "" : userDTO.getIsAsc();
wrapper.like(StringUtils.isNotBlank(userDTO.getName()), "name", userDTO.getName())
.like(StringUtils.isNotBlank(userDTO.getProfession()), "profession", userDTO.getProfession())
.ge(Objects.nonNull(userDTO.getCreateTimeMin()), "create_time", userDTO.getCreateTimeMin())
.lt(Objects.nonNull(userDTO.getCreateTimeMax()), "create_time", userDTO.getCreateTimeMax())
.last(StringUtils.isNotBlank(userDTO.getOrderByColumn()),
Constant.ORDER_BY_BLANK + StringUtils.camelToUnderline(userDTO.getOrderByColumn()) + " " + orderBy);
Page<UserVO> list = this.baseMapper.list1(page, wrapper);
return PageResVO.getBean(list);
该方法是xml负责需要查询的列,而wrapper负责查询条件的编写,不过在MP中我们可以使用wrapper.select() 列出需要的。
QueryWrapper<UserDTO> wrapper = new QueryWrapper<>();
wrapper.select("name", "profession", "create_time");
由于wrapper条件中的列都是写死的,而且都是魔法值,不满足代码规范和后续修改维护,我们可以自定义一个方法来根据方法引用获取方法名。
wrapper
使用wrapper查询的结果为我们的entity实体类,所以我们还需要进行一次类型转换
public PageResVO<UserVO> list1(UserDTO userDTO) {
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
String orderBy = StringUtils.isBlank(userDTO.getIsAsc()) ? "" : userDTO.getIsAsc();
wrapper.like(StringUtils.isNotBlank(userDTO.getName()), User::getName, userDTO.getName())
.like(StringUtils.isNotBlank(userDTO.getProfession()), User::getProfession, userDTO.getProfession())
.ge(Objects.nonNull(userDTO.getCreateTimeMin()), User::getCreateTime, userDTO.getCreateTimeMin())
.lt(Objects.nonNull(userDTO.getCreateTimeMax()), User::getCreateTime, userDTO.getCreateTimeMax())
.last(StringUtils.isNotBlank(userDTO.getOrderByColumn()),
Constant.ORDER_BY_BLANK + com.baomidou.mybatisplus.core.toolkit.StringUtils.camelToUnderline(userDTO.getOrderByColumn()) + " " + orderBy);
IPage<User> page = this.baseMapper.selectPage(new Page<>(userDTO.getPageNum(), userDTO.getPageSize()), wrapper);
IPage<UserVO> userVOs = page.convert(u ->{
UserVO vo = new UserVO();
BeanUtils.copyProperties(u, vo);
return vo;
})
return PageResVO.getBean(userVOs, userVOs.getRecords());
}
类型转换可以使用page自带的转换方法 convert 或者可以使用mapStruct
mapStruct的使用参考:MapStruct_夜光下丶的博客-CSDN博客
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95903.html