mybatis-plus两表联查带分页带模糊查询所有订单信息
Posted 奔跑的小山羊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis-plus两表联查带分页带模糊查询所有订单信息相关的知识,希望对你有一定的参考价值。
mapper.xml
<resultMap id="orderMap" type="com.tenli.entity.Order">
<id column="id" property="id"/>
<result column="order_no" property="orderNo"/>
<result column="course_id" property="courseId"/>
<result column="course_title" property="courseTitle"/>
<result column="course_cover" property="courseCover"/>
<result column="teacher_name" property="teacherName"/>
<result column="member_id" property="memberId"/>
<result column="nickname" property="nickname"/>
<result column="mobile" property="mobile"/>
<result column="total_fee" property="totalFee"/>
<result column="pay_type" property="payType"/>
<result column="status" property="status"/>
<result column="trade_type" property="tradeType"/>
<result column="is_deleted" property="isDeleted"/>
<result column="gmt_create" property="gmtCreate"/>
<result column="gmt_modified" property="gmtModified"/>
<!-- 一对一关系-->
<!-- column把第一次查询的某一个作为第二次查询的值
select把column列值交给select这个查询语句
-->
<association property="payLog" javaType="com.tenli.entity.PayLog">
<id column="id" property="id"/>
<result column="order_no" property="orderNo"/>
<result column="pay_time" property="payTime"/>
<result column="total_fee" property="totalFee"/>
<result column="transaction_id" property="transactionId"/>
<result column="trade_state" property="tradeState"/>
<result column="pay_type" property="payType"/>
<result column="attr" property="attr"/>
<result column="is_deleted" property="isDeleted"/>
<result column="gmt_create" property="gmtCreate"/>
<result column="gmt_modified" property="gmtModified"/>
</association>
</resultMap>
<!-- 只要使用联表查询,必须使用resultMap 查询所有订单信息带分页-->
<select id="findOrder" resultMap="orderMap">
SELECT o.*,p.pay_time
FROM t_order o left join t_pay_log p
on o.order_no=p.order_no
<where>
1=1
<if test="order.mobile !=null and order.mobile !=''">
and o.mobile like concat('%',#{order.mobile},'%')
</if>
<if test="order.courseTitle !=null and order.courseTitle !=''">
and o.course_title like concat('%',#{order.courseTitle},'%')
</if>
<if test="order.teacherName !=null and order.teacherName !=''">
and o.teacher_name like concat('%',#{order.teacherName},'%')
</if>
<if test="order.startDate !=null and order.startDate !=''">
and o.gmt_create >=#{order.startDate}
</if>
<if test="order.endDate !=null and order.endDate !=''">
<![CDATA[and o.gmt_create <=#{order.endDate}]]>
</if>
<if test="order.status !=null and order.status !=''">
and o.status =#{order.status}
</if>
<if test="order.orderNo !=null and order.orderNo !=''">
and o.order_no =#{order.orderNo}
</if>
<if test="order.teacherName !=null and order.teacherName !=''">
and o.teacher_name like concat('%',#{order.teacherName},'%')
</if>
<if test="order.payType !=null and order.payType !=''">
and o.pay_type =#{order.payType}
</if>
</where>
ORDER BY p.pay_time DESC
</select>
dao层
Page<Order> findOrder(Page<Order> page1,@Param("order") OrderVo orderVo);
service 层接口
CommonResult findOrder(Integer page, Integer limit, OrderVo orderVo);
service 层接口的实现类
/**
* 查询所有订单信息带分页
*
* @param page
* @param limit
* @return
*/
@Override
public CommonResult findOrder(Integer page, Integer limit, OrderVo orderVo) {
Page<Order> page1 = new Page<>(page, limit);
// 使用分页查询
Page<Order> orderPage = orderDao.findOrder(page1, orderVo); //这个走的是mybatis的查询 orderVo 这就是装参数的
// Page<Order> orderPage = orderDao.findOrder(page1, wrapper); 这个是走的mp 查询 wrapper mybatis可不认识
if (orderPage != null) {
return new CommonResult(2000, "查询订单信息成功", orderPage);
}
return new CommonResult(5000, "查询订单信息失败", null);
}
controller层实现类
/**
* 查询所有订单信息带分页
* @param page
* @param limit
* @return
*/
@ApiOperation(value = "查询所有订单信息")
@PostMapping("/findOrder/{page}/{limit}")
public CommonResult findOrder(@PathVariable("page")Integer page, @PathVariable("limit") Integer limit,@RequestBody OrderVo orderVo) {
System.out.println(orderVo);
return orderService.findOrder(page,limit,orderVo);
}
mapper.xml
<resultMap id="orderMap" type="com.tenli.entity.Order">
<id column="id" property="id"/>
<result column="order_no" property="orderNo"/>
<result column="course_id" property="courseId"/>
<result column="course_title" property="courseTitle"/>
<result column="course_cover" property="courseCover"/>
<result column="teacher_name" property="teacherName"/>
<result column="member_id" property="memberId"/>
<result column="nickname" property="nickname"/>
<result column="mobile" property="mobile"/>
<result column="total_fee" property="totalFee"/>
<result column="pay_type" property="payType"/>
<result column="status" property="status"/>
<result column="trade_type" property="tradeType"/>
<result column="is_deleted" property="isDeleted"/>
<result column="gmt_create" property="gmtCreate"/>
<result column="gmt_modified" property="gmtModified"/>
<!-- 一对一关系-->
<!-- column把第一次查询的某一个作为第二次查询的值
select把column列值交给select这个查询语句
-->
<association property="payLog" javaType="com.tenli.entity.PayLog">
<id column="id" property="id"/>
<result column="order_no" property="orderNo"/>
<result column="pay_time" property="payTime"/>
<result column="total_fee" property="totalFee"/>
<result column="transaction_id" property="transactionId"/>
<result column="trade_state" property="tradeState"/>
<result column="pay_type" property="payType"/>
<result column="attr" property="attr"/>
<result column="is_deleted" property="isDeleted"/>
<result column="gmt_create" property="gmtCreate"/>
<result column="gmt_modified" property="gmtModified"/>
</association>
</resultMap>
<!-- 只要使用联表查询,必须使用resultMap 查询所有订单信息带分页-->
<select id="findOrder" resultMap="orderMap">
SELECT o.*,p.pay_time
FROM t_order o join t_pay_log p
where o.order_no=p.order_no
<if test="ew.sqlSegment != null and ew.sqlSegment != '' and ew.nonEmptyOfWhere">
and ${ew.sqlSegment}
</if>
</select>
dao层
@Param(Constants.WRAPPER)QueryWrapper wrapper加上这个就能进行模糊查询
Page<Order> findOrder(Page<Order> page1,@Param(Constants.WRAPPER)QueryWrapper<Order> wrapper);
service 层接口
CommonResult findOrder(Integer page, Integer limit, OrderVo orderVo);
service 层接口的实现类
/**
* 查询所有订单信息带分页
*
* @param page
* @param limit
* @return
*/
@Override
public CommonResult findOrder(Integer page, Integer limit, OrderVo orderVo) {
QueryWrapper<Order> wrapper = new QueryWrapper<>();
if (StringUtils.isNotEmpty(orderVo.getMobile())) {
wrapper.like("o.mobile", orderVo.getMobile());
}
if (StringUtils.isNotEmpty(orderVo.getCourseTitle())) {
wrapper.like("o.course_title", orderVo.getCourseTitle());
}
// 判断支付的时间
if (StringUtils.isNotEmpty (orderVo.getEndDate())) {
wrapper.between("p.pay_time", orderVo.getStartDate(),orderVo.getEndDate());
}
if (orderVo.getStatus() != null) {
wrapper.eq("o.status", orderVo.getStatus());
}
if (StringUtils.isNotEmpty(orderVo.getOrderNo())) {
wrapper.like("o.order_no", orderVo.getOrderNo());
}
if (StringUtils.isNotEmpty(orderVo.getTeacherName())) {
wrapper.like("o.teacher_name", orderVo.getTeacherName());
}
if (orderVo.getPayType() != null) {
wrapper.eq("o.pay_type", orderVo.getPayType());
}
// 根据支付的时间倒序
wrapper.orderByDesc("p.pay_time");
Page<Order> page1 = new Page<>(page, limit);
Page<Order> orderPage = orderDao.findOrder(page1, wrapper);
if (orderPage != null) {