一 功能的实现-商品查询
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 启动项目 批量操作