您的位置 首页 > 数码极客

Java,SpringBoot,MyBatisPlus,自动分页插件的应用,代码分享

背景

分页,Web项目用的最频繁也最多,MyBatisPlus的分页插件:com.baomidou.Myba。

工程结构以及SQL等参考:乐观锁插件的应用

测试分页的代码:

package com.w; import com.baomidou.myba; import com.baomidou.myba; import com.baomidou.myba; import com.baomidou.myba; import org.; import org.; @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); // 数据库类型 ); // 方言实现类 (new MySqlDialect()); // 单页分页条数限制 (500L); // 溢出总页数后是否进行处理 // 设置请求的页面大于最大页后操作, true调回到首页,false继续请求 // 默认false (false); myba(new PaginationInnerInterceptor()); return mybatisPlusInterceptor; } }package com.w; import com.baomidou.myba; import com.baomidou.myba; import com.baomidou.myba; import com.baomidou.myba; import lombok.Data; import lombok.EqualsAndhashCode; import java.io.Serializable; import java.ma; import java.u; @Data // 此注解会生成equals(Object other) 和 hashCode()方法 @EqualsAndHashCode(callSuper = false) @TableName("t_order") public class Order implements Serializable { private static final long serialVersionUID = 1L; @TableId(value = "id", type = IdTy) private Integer id; // 订单编号 private String code; // 所属用户 private Integer userId; // 总价钱 private BigDecimal totalPrice; // 备注 private String remark; // 创建时间 private Date createTime; @Version private Long version; }package com.w; import lombok.Data; import java.ma; @Data public class OrderDTO { private Integer id; // 订单编号 private String code; // 所属用户 private Integer userId; // 总价钱 private BigDecimal totalPrice; }package com.w; import com.baomidou.myba; import com.baomidou.myba; import com.w.Order; import com.w.OrderDTO; import org.a; import org.a; import java.u; @Mapper public interface OrderMapper extends BaseMapper<Order> { /** * @param page * @return */ public IPage<Order> selectPageOrder(IPage<?> page); /** * @param page * @param userId * @return */ public IPage<Order> selectPageOrder2(IPage<?> page,@Param("userId")Integer userId); /** * @param page * @param paramsMap * @return */ public IPage<Order> selectPageOrder3(IPage<?> page,@Param("query")Map<String,Object> paramsMap); /** * @param page * @param paramsMap * @return */ public IPage<OrderDTO> selectPageOrderDTO(IPage<?> page, @Param("Query")Map<String,Object> paramsMap); }

OrderMa

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "- Mapper 3.0//EN" ";> <mapper namespace="com.w.OrderMapper"> <!-- 结果集 --> <resultMap id="orderDTOResultMap" type="com.w.OrderDTO"> <id column="id" property="id"/> <result column="channel_business_name" property="channelBusinessName" /> <result column="channel_business_number" property="channelBusinessNumber" /> </resultMap> <!-- 分页查询 --> <select id="selectPageOrder" resultType="com.w.Order"> SELECT * FROM `t_order` </select> <!-- 分页查询 --> <select id="selectPageOrder2" resultType="com.w.Order"> SELECT * FROM `t_order` <where> <if test="userId!=null"> and user_id = #{userId} </if> </where> order by `create_time` desc </select> <!-- 分页查询 --> <select id="selectPageOrder3" resultType="com.w.Order"> SELECT * FROM `t_order` <where> <if test="query.userId!=null"> and user_id = #{query.userId} </if> </where> order by `create_time` desc </select> <!-- 分页查询 --> <select id="selectPageOrderDTO" resultMap="orderDTOResultMap"> SELECT * FROM `t_order` <where> <if test="query.userId!=null"> and user_id = #{query.userId} </if> </where> order by `create_time` desc </select> </mapper>

测试类:

package com.w; import com.baomidou.myba; import com.baomidou.myba; import com.baomidou.myba; import com.baomidou.myba; import com.baomidou.myba; import com.w.Order; import com.w.OrderDTO; import org.junit.Test; import org.junit.runner.RunWith; import org.; import org.; import org.; import java.ma; import java.u; import java.u; import java.u; import java.u; @RunWi) @SpringBootTest public class OrderMapperTest { @Autowired OrderMapper orderMapper; @Test public void testInsert() { for (int i = 0; i < 1000; i++) { Order newOrder = new Order(); newOrder.setCode("order" + i); newOrder.setUserId(1); newOrder.setTotalPrice(new BigDecimal(i)); newOrder.setRemark("备注"); newOrder.setCreateTime(new Date()); //mybatis-plus乐观锁==>插入数据时,必须给值 newOrder.setVersion(0L); int operate = orderMa(newOrder); Sy(operate); } } @Test public void testPage() { int pageNum = 1; int pageSize = 50; IPage page = new Page(pageNum, pageSize); Wrapper warpper = new QueryWrapper(); IPage dataPage = orderMa(page, warpper); Sy("请求页:" + da()); Sy("页大小:" + da()); Sy("总页数:" + da()); Sy("总记录:" + da()); List<Map> dataList = da(); for (Map map : dataList) { // 与数据库字段名对应 Sy("code") + "," + map.get("total_price")); Sy(); } } @Test public void testPage2() { int pageNum = 1; int pageSize = 50; IPage<Order> page = new Page<Order>(pageNum, pageSize); QueryWrapper<Order> warpper = new QueryWrapper<Order>(); war("code", "order1"); IPage<Order> dataPage = orderMa(page, warpper); Sy("请求页:" + da()); Sy("页大小:" + da()); Sy("总页数:" + da()); Sy("总记录:" + da()); List<Order> orderList = da(); for (Order order : orderList) { Sy(order); } } @Test public void testPageOrder() { int pageNum = 1; int pageSize = 50; IPage<Order> page = new Page<Order>(pageNum, pageSize); IPage<Order> dataPage = orderMaOrder(page); Sy("请求页:" + da()); Sy("页大小:" + da()); Sy("总页数:" + da()); Sy("总记录:" + da()); List<Order> orderList = da(); orderLi(System.out::println); } @Test public void testPageOrder2() { int pageNum = 1; int pageSize = 50; IPage<Order> page = new Page<Order>(pageNum, pageSize); IPage<Order> dataPage = orderMaOrder2(page, 1); Sy("请求页:" + da()); Sy("页大小:" + da()); Sy("总页数:" + da()); Sy("总记录:" + da()); List<Order> orderList = da(); orderLi(System.out::println); } @Test public void testPageOrder3() { int pageNum = 1; int pageSize = 50; IPage<Order> page = new Page<Order>(pageNum, pageSize); Map<String, Object> paramsMap = new HashMap<>(); ("userId", 1); IPage<Order> dataPage = orderMaOrder3(page, paramsMap); Sy("请求页:" + da()); Sy("页大小:" + da()); Sy("总页数:" + da()); Sy("总记录:" + da()); List<Order> orderList = da(); orderLi(System.out::println); } @Test public void testPageOrderDTO() { int pageNum = 1; int pageSize = 50; IPage<OrderDTO> page = new Page<>(pageNum, pageSize); Map<String, Object> paramsMap = new HashMap<>(); ("userId", 1); IPage<OrderDTO> dataPage = orderMaOrderDTO(page, paramsMap); Sy("请求页:" + da()); Sy("页大小:" + da()); Sy("总页数:" + da()); Sy("总记录:" + da()); List<OrderDTO> orderList = da(); orderLi(System.out::println); } }

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“如何自动分页”边界阅读