总概

A、技术栈

  • 开发语言:Java 1.8
  • 数据库:MySQL、Redis、MongoDB、Elasticsearch
  • 微服务框架:Spring Cloud Alibaba
  • 微服务网关:Spring Cloud Gateway
  • 服务注册和配置中心:Nacos
  • 分布式事务:Seata
  • 链路追踪框架:Sleuth
  • 服务降级与熔断:Sentinel
  • ORM框架:MyBatis-Plus
  • 分布式任务调度平台:XXL-JOB
  • 消息中间件:RocketMQ
  • 分布式锁:Redisson
  • 权限:OAuth2
  • DevOps:Jenkins、Docker、K8S

B、本节实现目标

  • 分页查询订单。

一、MyBatis-Plus分页插件

MyBatis新版分页插件配置和旧版的一样,参考MyBatis-Plus官网:分页插件

在mall-common项目中新建MyBatis-Plus分页插件配置

package com.ac.common.config.mybatis;import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class GlobalMybatisConfig {/** * 设置插件 */@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));//乐观锁interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return interceptor;}}

mall-common项目截图

二、分页查询订单实现

2.1 分页查询对象

分页查询对象OrderPageQry继承MyBaits分页对象Page,注意是为了在写订单分页查询service时,不想提供两个参数,而共用一个参数。

@Datapublic class OrderPageQry extends Page {@ApiModelProperty("用户ID")private Long memberId;@ApiModelProperty("订单号")private String orderNo;@ApiModelProperty("用户姓名")private String memberName;@ApiModelProperty("手机号")private String mobile;}

2.2 mapper层

/** * @description 订单-数据访问层(依赖mybatis-plus),对应mapper.xml里的自定义sql */public interface OrderMapper extends BaseMapper {/** * 分页查询订单 * * @param page 分页查询参数 * @param qry查询参数 * @return */IPage pageOrder(Page page, @Param("qry") OrderPageQry qry);}

xml代码

t.id,t.order_no,t.pay_channel,t.order_state,t.order_time,t.pay_time,t.refund_time,t.member_id,t.member_name,t.mobile,t.product_amount,t.discount_amount,t.pay_amountselectfromt_order tt.deleted = 0and t.member_id=#{qry.memberId}and t.order_no=#{qry.orderNo}and t.member_name like concat('%', #{qry.memberName}, '%')and t.mobile = #{qry.mobile}order by t.create_time desc

2.3 dao层

@Slf4j@Repositorypublic class OrderDaoImpl extends ServiceImpl implements OrderDao {@Resourceprivate OrderMapper orderMapper;@Overridepublic IPage pageOrder(OrderPageQry qry) {return orderMapper.pageOrder(qry, qry);}}

2.4 service层

@Slf4j@Servicepublic class OrderServiceImpl implements OrderService {@Overridepublic IPage pageOrder(OrderPageQry qry) {return orderDaoImpl.pageOrder(qry);}}

2.5 controller层

@Api(tags = "订单")@RestController@RequestMapping("order")public class OrderController {@Resourceprivate OrderService orderServiceImpl;@ApiOperation(value = "分页查询订单")@GetMapping("page")public IPage pageOrder(@Valid OrderPageQry qry) {return orderServiceImpl.pageOrder(qry);}}

三、订单分页查询测试

订单分页查询测试

后台打印sql

==>Preparing: select t.id, t.order_no, t.pay_channel, t.order_state, t.order_time, t.pay_time, t.refund_time, t.member_id, t.member_name, t.mobile, t.product_amount, t.discount_amount, t.pay_amount from t_order t WHERE t.deleted = 0 and t.member_id=? order by t.create_time desc LIMIT ? OFFSET ?==> Parameters: 264260572479489(Long), 2(Long), 2(Long)