使用 easyui 的 DataGrid 控件实现商品的分页查询,DataGrid 控件提交分页所需要的 page 和rows 参数,后台响应包含总记录数 total 和需要显示的商品对象的集合 rows 的 json 对象。

PageHelper 资源地址

https://github.com/pagehelper/Mybatis-PageHelper

PageHelper 实现原理

商品分页查询 ego-prc 实现-easyui-LMLPHP

PageHelper 配置

mybatisConfig.xml

<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="dialect" value="mysql" />
</plugin>
</plugins>

ego-common 创建 PageResult

com.bjsxt.ego.beans.PageResult

package com.bjsxt.ego.beans;

import java.io.Serializable;
import java.util.List; public class PageResult<T> implements Serializable { private List<T> rows;
private Long tota; public PageResult(List<T> rows, Long tota) {
this.rows = rows;
this.tota = tota;
}
public PageResult(){} public List<T> getRows() {
return rows;
} public void setRows(List<T> rows) {
this.rows = rows;
} public Long getTota() {
return tota;
} public void setTota(Long tota) {
this.tota = tota;
}
}

创建 ItemService 接口

com.bjsxt.ego.rpc.service.ItemService

package com.bjsxt.ego.rpc.service;

import com.bjsxt.ego.beans.EgoResult;
import com.bjsxt.ego.beans.PageResult;
import com.bjsxt.ego.rpc.pojo.TbItem; import java.util.List; public interface ItemService { /**
* 实现商品的分页查询
*/
public PageResult<TbItem> selectItemList(Integer page,Integer rows); /**
* 完成商品上下架的处理
*/
public EgoResult updataItemStatus(List<Long> itemIds,Boolean flag); public EgoResult deleteItem(List<Long> itemIds); }

创建 ItemServiceImpl 实现类

com.bjsxt.ego.rpc.service.impl.ItemServiceImpl

package com.bjsxt.ego.rpc.service.impl;

import com.bjsxt.ego.beans.EgoResult;
import com.bjsxt.ego.beans.PageResult;
import com.bjsxt.ego.rpc.mapper.TbItemMapper;
import com.bjsxt.ego.rpc.pojo.TbItem;
import com.bjsxt.ego.rpc.pojo.TbItemExample;
import com.bjsxt.ego.rpc.service.ItemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class ItemServiceImpl implements ItemService { @Autowired
private TbItemMapper tbItemMapper; @Override
public PageResult<TbItem> selectItemList(Integer page, Integer rows) {
//执行分页操作
Page ps = PageHelper.startPage(page, rows); TbItemExample tbItemExample=new TbItemExample(); //执行数据库查询操作
List<TbItem> list = tbItemMapper.selectByExample(tbItemExample);
PageResult<TbItem> result=new PageResult<>(); result.setRows(list);
result.setTota(ps.getTotal());
return result;
} @Override
public EgoResult updataItemStatus(List<Long> itemIds, Boolean flag) {
//创建TbItem对象
TbItem item=new TbItem();
if (flag){
item.setStatus((byte) 1);
}else {
item.setStatus((byte) 2);
} //产生动态where条件
TbItemExample tbItemExample=new TbItemExample();
TbItemExample.Criteria c =tbItemExample.createCriteria();
c.andIdIn(itemIds); tbItemMapper.updateByExampleSelective(item,tbItemExample); return EgoResult.ok();
} @Override
public EgoResult deleteItem(List<Long> itemIds) { //产生动态的where条件
TbItemExample itemExample=new TbItemExample();
TbItemExample.Criteria c = itemExample.createCriteria();
c.andIdIn(itemIds);
tbItemMapper.deleteByExample(itemExample);
return EgoResult.ok();
}
}

配置 applicationContext-dubbo.xml

applicationContext-dubbo.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 添加服务提供者的标志 -->
<dubbo:application name="ego-rpc-provider" /> <!-- 指定注册中心 -->
<dubbo:registry address="192.168.181.129:2181,192.168.181.129:2182,192.168.181.129:2183" protocol="zookeeper" /> <!-- 指定当前项目发布dubbo服务的方式 -->
<!-- 指定服务发布的协议:dubbo协议 -->
<!-- 指定服务发布的端口:10000 -->
<dubbo:protocol name="dubbo" port="20000" /> <!-- &lt;!&ndash; 发布dubbo服务 &ndash;&gt;-->
<dubbo:service interface="com.bjsxt.ego.rpc.service.ItemService" ref="itemServiceImpl"></dubbo:service>
<dubbo:service interface="com.bjsxt.ego.rpc.service.ItemCatService" ref="itemCatServiceImpl"></dubbo:service> </beans>

启动 ego-rpc-service-impl 发布 RPC 服务

com.bjsxt.provider.test.ProviderTest

package com.bjsxt.provider.test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class ProviderTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("spring/applicationContext-dao.xml",
"spring/applicationContext-dubbo.xml",
"spring/applicationContext-service.xml",
"spring/applicationContext-tx.xml");
ac.start();
System.out.println("================完成!=================");
//阻塞程序的运行
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
ac.stop(); }
}

商品分页查询 ego-manager-web 实现

配置 applicationContext-dubbo.xml

spring/applicationContext-dubbo.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 添加服务消费者的标志 -->
<dubbo:application name="ego-manager-web-consumer"/>
<!-- 指定注册中心 -->
<dubbo:registry address="192.168.181.129:2181,192.168.181.129:2182,192.168.181.129:2183" protocol="zookeeper" /> <!-- &lt;!&ndash; spring容器中存在一个远程服务的代理对象 &ndash;&gt;-->
<dubbo:reference interface="com.bjsxt.ego.rpc.service.ItemService" id="itemServiceProxy"></dubbo:reference>
<dubbo:reference interface="com.bjsxt.ego.rpc.service.ItemCatService" id="itemCatServiceProxy"></dubbo:reference> </beans>

创建 ManagerItemService 接口

com.bjsxt.ego.manager.service.ManagerItemService

package com.bjsxt.ego.manager.service;

import com.bjsxt.ego.beans.EgoResult;
import com.bjsxt.ego.beans.PageResult;
import com.bjsxt.ego.rpc.pojo.TbItem; public interface ManagerItemService { /**
* 完成商品信息的分页查询
* **/
public PageResult<TbItem> selectItemListService(Integer page,Integer rows); /**
* 完成商品上架处理
* @param ids
* @return
*/
public EgoResult reshlfItem(Long[] ids); /**
* 完成商品下架处理
* @param ids
* @return
*/
public EgoResult instockItem(Long[] ids); /**
* 完成商品的删除
* @param ids
* @return
*/
public EgoResult deleteItem(Long[] ids); }

创建 ManagerItemServiceImpl 实现类

com.bjsxt.ego.manager.service.impl.ManagerItemServiceImpl

package com.bjsxt.ego.manager.service.impl;

import com.bjsxt.ego.beans.EgoResult;
import com.bjsxt.ego.beans.PageResult;
import com.bjsxt.ego.manager.service.ManagerItemService;
import com.bjsxt.ego.rpc.pojo.TbItem;
import com.bjsxt.ego.rpc.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.Arrays;
import java.util.List; @Service
public class ManagerItemServiceImpl implements ManagerItemService { @Autowired
private ItemService itemServiceProxy; @Override
public PageResult<TbItem> selectItemListService(Integer page, Integer rows) { PageResult<TbItem> pageResult = itemServiceProxy.selectItemList(page, rows);
return pageResult;
} @Override
public EgoResult reshlfItem(Long[] ids) {
List<Long> itemIds = Arrays.asList(ids);
return itemServiceProxy.updataItemStatus(itemIds,true);
} @Override
public EgoResult instockItem(Long[] ids) {
List<Long> itemIds = Arrays.asList(ids);
return itemServiceProxy.updataItemStatus(itemIds,false);
} @Override
public EgoResult deleteItem(Long[] ids) {
List<Long> itemIds=Arrays.asList(ids);
return itemServiceProxy.deleteItem(itemIds);
}
}

创建 ItemController

com.bjsxt.ego.manager.controller.ItemController

package com.bjsxt.ego.manager.controller;

import com.bjsxt.ego.beans.EgoResult;
import com.bjsxt.ego.beans.PageResult;
import com.bjsxt.ego.manager.service.ManagerItemService;
import com.bjsxt.ego.rpc.pojo.TbItem;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class ItemController {
@Autowired
private ManagerItemService managerItemService; /***
* 处理商品信息分页查询的请求
* **/
@RequestMapping(value = "item/list",produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8")
@ResponseBody
public PageResult<TbItem> itemList(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "30") Integer rows){
return managerItemService.selectItemListService(page,rows);
} /**
*处理商品上架
* @param ids
* @return
*/
@RequestMapping(value = "item/reshelf")
@ResponseBody
public String reshelfItem(Long[] ids){
EgoResult egoResult = managerItemService.reshlfItem(ids);
String s = new Gson().toJson(egoResult);
return s;
} /**
*处理商品下架
* @param ids
* @return
*/
@RequestMapping(value = "item/instock")
@ResponseBody
public String instockItem(Long[] ids){
EgoResult egoResult = managerItemService.instockItem(ids);
String s = new Gson().toJson(egoResult);
return s;
} /**
*处理商品删除
* @param ids
* @return
*/
@RequestMapping(value = "item/delete")
@ResponseBody
public String deleteItem(Long[] ids){
EgoResult egoResult = managerItemService.deleteItem(ids);
String s = new Gson().toJson(egoResult);
return s;
}
}

发布 ego-manager-web 访问商品列表

商品分页查询 ego-prc 实现-easyui-LMLPHP

商品分页查询 ego-prc 实现-easyui-LMLPHP

05-11 21:54