Day02-基于RPC软件模型使用PageHelper实现用户商品查询功能

本文档详细介绍了如何基于RPC软件模型利用PageHelper实现商品查询功能,包括新增Item的pojo和Mapper,接口定义,服务提供者实现,EasyUI数据模板以及控制类的编写。同时,还涵盖了批量修改商品的功能实现,包括自定义异常,服务接口扩展,服务提供者方法实现,远程调用和服务控制器的编写。
摘要由CSDN通过智能技术生成

一 功能的实现-商品查询

1 新增Item的pojo和Mapper

@Data
@TableName("tb_item")
public class Item implements Serializable {
  @TableId(type = IdType.AUTO)
  private Long id;
  private String title;
  private String sellPoint;
  private Long price;
  private Integer num;
  private String barcode;
  private String image;
  private Long cid;
  private Integer status;
  private Date created;
  private Date updated;
}
public interface ItemMapper extends BaseMapper<Item> { }

2 commons-api新建接口 ItemDubboService
新增查询商品页面的方法

public interface ItemDubboService {
    /**
     * 分页查询商品信息
     * @param pageNumber
     * @param pageSize
     * @return
     */
    Page selectPage(int pageNumber,int pageSize);
}

3 ego-provider中实现

@DubboService
public class ItemDubboServiceImp implements ItemDubboService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public Page selectPage(int pageNumber, int pageSize) {
        return itemMapper.selectPage(new Page<>(pageNumber,pageSize),null);
    }
}

4新建EasyUI Datagrid数据模板类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class EasyUIDatagrid {
    private long  total;
    private List<?> rows;
}

5 ego-manage调用

public interface ItemService {
    EasyUIDatagrid selectPage(int pageNumber, int pageSize);
}
@Service
public class ItemServiceImp implements ItemService {
    @DubboReference
    private ItemDubboService itemDubboService;

    @Override
    public EasyUIDatagrid selectPage(int pageNumber, int pageSize) {
        //第一步 构建返回对象
        EasyUIDatagrid result=new EasyUIDatagrid();
        //第二步 获得远程调用的数据
        Page page = itemDubboService.selectPage(pageNumber, pageSize);
        //第三步 封装数据
        result.setTotal(page.getTotal());
        result.setRows(page.getRecords());
        //第四步 返回数据
        return result;
    }
}

6 编写控制类

@Controller
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemService itemService;

    /**
     * 查询商品列表
     * @param page 当前索引
     * @param rows 每页记录数
     */
    @GetMapping("/list")
    public EasyUIDatagrid showItem(int page,int rows){
        try {
            return itemService.selectPage(page,rows);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

7 WebConfig文件新增跳转路径

@Configuration
public class WebConifguration implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/main").setViewName("index");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/item-list").setViewName("item-list");
    }
}

7 启动项目 查看商品页面

二 功能的实现-批量修改

1 ego-commons新建一个自定义异常

public class DaoException extends Exception{

    public DaoException(String message) {
        super(message);
    }
}

2 ItemDubboService新增批量修改商品方法

/**
 * 事务一定要写在provider方
 * 批量修改
 * @param ids 所有要修改的id
 * @param status 修改的状态值
 * @return 成功1,失败0
 */
int updateStatusByIds(long[] ids,int status) throws DaoException;

3 ego-provider中实现该方法

@Override
@Transactional//事务处理
public int updateStatusByIds(long[] ids, int status) throws DaoException {
    //思路 update tb_item set status=#{status} where item_id in (ids)
    //第一步 可以使用MybatisPlus里面的更新功能,通过for循环实现
    int index=0;
    for (long id : ids) {
        Item item = itemMapper.selectById(id);
        item.setStatus(status);
        index+=itemMapper.updateById(item);
    }
    //第二步 返回数据
    return index;
}

4 ego-manage远程调用批量修改方法
ItemService

/**
 * 操作状态值
 * @param ids
 * @param status
 * @return
 */
EgoResult updateStatus(long[] ids, int status) throws DaoException;

ItemServiceImp

@Override
public EgoResult updateStatus(long[] ids, int status) throws DaoException {
    int index = itemDubboService.updateStatusByIds(ids, status);
    return EgoResult.ok(index);
}

5 编写ItemController
编写之前检查页面的返回路径是什么,必须确保路径能存在!

/* *
 * 删除
 * @param ids
 * @return*/

@RequestMapping("/delete")
public EgoResult delete(long[] ids){
    try {
        return itemService.updateStatus(ids,3);
    } catch (DaoException e) {
        e.printStackTrace();
    }
    return EgoResult.error("删除失败");
}
/**
 * 上架
 * @param ids
 * @return
 */
@RequestMapping("/reshelf")
public EgoResult reshelf(long[] ids){
    try {
        return itemService.updateStatus(ids,1);
    } catch (DaoException e) {
        e.printStackTrace();
    }
    return EgoResult.error("上架失败");
}
/**
 * 下架
 * @param ids
 * @return
 */
@RequestMapping("/instock")
public EgoResult instock(long[] ids){
    try {
        return itemService.updateStatus(ids,2);
    } catch (DaoException e) {
        e.printStackTrace();
    }
    return EgoResult.error("下架失败");
}

6 启动项目 批量操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值