Open qiaohui opened 7 years ago
构建器ModelBuilder的使用说明貌似不可用,应该是版本升级后,文档及 test case 也没有同步更新,目前已经找不到正确的构建方式了。
有时间的话,麻烦更新一下呗
testcase应该更新了,readme还没更新,晚些时候我更新下。
天船老师,我准备在新公司推广该组件,readme应该还是老版本吧,现在应该变成类似这样的吧
另外问一下,你们也在用这个1.1.1
版本吗?
@PostConstruct
private void init() {
prodViewMapper = new DefaultViewMapperImpl();
prodBuildContextModelBuilder = new SimpleModelBuilder<>();
//商品sku
prodViewMapper.addMapper(ProductStock.class,
(buildContext, product) -> new ProductStockView(product, buildContext));
prodBuildContextModelBuilder.extractId(ProductStock.class, ProductStock::getProdId, Product.class);
prodBuildContextModelBuilder.<Integer>buildValueTo(Product.class,
(buildContext, prodIds) -> productService.getByIds(prodIds), Product.class);
//sku 库存数目
prodBuildContextModelBuilder.extractId(ProductStock.class, ProductStock::getId, Constants.PROD_STOCK_COUNTS);
prodBuildContextModelBuilder.<Integer>buildValueTo(Constants.PROD_STOCK_COUNTS,
(buildContext, prodIds) -> productStockService.getStockCounts(prodIds), Constants.PROD_STOCK_COUNTS);
//商品详情mapper sku spu detail
prodViewMapper.addMapper(ProductDetail.class,
(buildContext, product) -> new ProductDetailView(product, buildContext));
prodBuildContextModelBuilder.extractId(ProductDetail.class, ProductDetail::getProdId, ProductStock.class);
prodBuildContextModelBuilder.extractId(ProductDetail.class, ProductDetail::getProdId, Product.class);
prodBuildContextModelBuilder.<Integer>buildValueTo(ProductStock.class,
(buildContext, prodIds) -> productStockService.getAllByProdIds(prodIds), ProductStock.class);
}
public <M, V> V mapSingle(M model) {
SimpleBuildContext buildContext = new SimpleBuildContext();
prodBuildContextModelBuilder.buildSingle(model, buildContext);
return prodViewMapper.map(model, buildContext);
}
public <M, V> List<V> mapItems(Collection<M> models) {
SimpleBuildContext buildContext = new SimpleBuildContext();
prodBuildContextModelBuilder.buildMulti(models, buildContext);
return prodViewMapper.map(models, buildContext);
}
BuildContext buildContext = new BuildContext(); 应为 BuildContext buildContext = new SimpleBuildContext();
高级技巧中: public class MyBuildContext extends BuildContext 应是 public class MyBuildContext extends SimpleBuildContext