总概
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;
@Configuration
public class GlobalMybatisConfig {
/**
* 设置插件
*/
@Bean
public 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时,不想提供两个参数,而共用一个参数。
@Data
public 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<Order> {
/**
* 分页查询订单
*
* @param page 分页查询参数
* @param qry 查询参数
* @return
*/
IPage<OrderDTO> pageOrder(Page page, @Param("qry") OrderPageQry qry);
}
xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ac.order.mapper.OrderMapper">
<sql id="OrderDTO_Column">
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
</sql>
<select id="pageOrder" resultType="com.ac.order.dto.OrderDTO">
select
<include refid="OrderDTO_Column"></include>
from
t_order t
<where>
t.deleted = 0
<if test="qry.memberId!=null">
and t.member_id=#{qry.memberId}
</if>
<if test="qry.orderNo!=null and qry.orderNo!=''">
and t.order_no=#{qry.orderNo}
</if>
<if test="qry.memberName!=null and qry.memberName!=''">
and t.member_name like concat('%', #{qry.memberName}, '%')
</if>
<if test="qry.mobile!=null and qry.mobile!=''">
and t.mobile = #{qry.mobile}
</if>
</where>
order by t.create_time desc
</select>
</mapper>
2.3 dao层
@Slf4j
@Repository
public class OrderDaoImpl extends ServiceImpl<OrderMapper, Order> implements OrderDao {
@Resource
private OrderMapper orderMapper;
@Override
public IPage<OrderDTO> pageOrder(OrderPageQry qry) {
return orderMapper.pageOrder(qry, qry);
}
}
2.4 service层
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
@Override
public IPage<OrderDTO> pageOrder(OrderPageQry qry) {
return orderDaoImpl.pageOrder(qry);
}
}
2.5 controller层
@Api(tags = "订单")
@RestController
@RequestMapping("order")
public class OrderController {
@Resource
private OrderService orderServiceImpl;
@ApiOperation(value = "分页查询订单")
@GetMapping("page")
public IPage<OrderDTO> 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)