商品数据库原型
上来第一步不是根据产品经理给的需求建数据库、表。
而是先进行领域模型设计,再建库、表。
比如,先想好,密码和用户信息应该分开存放,然后再去建表。
也就是说,先思考设计Model,再去动手建表。
1.首先设计商品领域模型 然后再设计数据库
public class ItemModel {
private Integer id;
//商品名称
private String title;
//商品价格
private BigDecimal price;
//商品库存
private Integer stock;
//商品描述
private String description;
//商品销量
private Integer seles;
//商品图片描述
private String imgUrl;
}
数据库设计
item表 根据领域模型 考虑到库存需要分开设计
item_stock表
根据之前逆向工程生成dao和xml文件
修改mapper的xml文件
把insert和insertSelective方法后添加属性 keyProperty=“id” useGeneratedKeys=“true”,使其保持自增
创建接口ItemService
//创建商品
ItemModel createItem(ItemModel itemModel) throws BusinessException;
//商品详情浏览
ItemModel getItemById(Integer id);
itemServieImpl接口实现 先实现添加商品和商品详情方法
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ValidatiorImpl validatior;
@Resource
private ItemDaoMapper itemDaoMapper;
@Resource
private ItemStockDaoMapper itemStockDaoMapper;
//model转dao方法
private ItemDao converItemDoFromitemModel(ItemModel itemModel){
if (itemModel==null){
return null;
}
ItemDao itemDao=new ItemDao();
BeanUtils.copyProperties(itemModel,itemDao);
//BigDecimal因为和Dao Double 类型不一直需要手动设置
itemDao.setPrice(itemModel.getPrice().doubleValue());
return itemDao;
}
//model转dao方法
private ItemStockDao converItemStockDaoFromitemModel(ItemModel itemModel){
if (itemModel==null){
return null;
}
ItemStockDao itemStockDao=new ItemStockDao();
itemStockDao.setItemId(itemModel.getId());
itemStockDao.setStock(itemModel.getStock());
return itemStockDao;
}
//创建商品
@Override
@Transactional
public ItemModel createItem(ItemModel itemModel) throws BusinessException {
//校验入参
ValidationResult validate = this.validatior.validate(itemModel);
if (validate.isHasErrors()){
throw new BusinessException(EmBusineError.PARAMETER_VALIDATION_ERROR,validate.getErrMsg());
}
//转换itemModel>dao
ItemDao itemDao = this.converItemDoFromitemModel(itemModel);
//写入数据库
itemDaoMapper.insertSelective(itemDao);
//写入后赋值给里面将Id赋值给itemModel
itemModel.setId(itemDao.getId());
//获取库存数量
ItemStockDao itemStockDao = this.converItemStockDaoFromitemModel(itemModel);
itemStockDaoMapper.insertSelective(itemStockDao);
//返回创建完成的对象
return this.getItemById(itemModel.getId());
}
//商品详情浏览接口
@Override
public ItemModel getItemById(Integer id) {
//根据id查询出商品表
ItemDao itemDao = itemDaoMapper.selectByPrimaryKey(id);
if (itemDao==null){
return null;
}
//操作获得库存数量 在mapper xml文件编写sql
ItemStockDao itemStockDao = itemStockDaoMapper.selectByItemId(id);
//拼接成itemModel返回
ItemModel itemModel = this.converItemStockDaoItemDaoFromitemModel(itemStockDao, itemDao);
return itemModel;
}
//拼接ItemModel
private ItemModel converItemStockDaoItemDaoFromitemModel(ItemStockDao itemStockDao,ItemDao itemDao){
ItemModel itemModel=new ItemModel();
BeanUtils.copyProperties(itemDao,itemModel);
itemModel.setPrice(new BigDecimal(itemDao.getPrice()));
BeanUtils.copyProperties(itemStockDao,itemModel);
return itemModel;
}
//商品列表
@Override
public List<ItemModel> listItem() {
return null;
}
}
controller实现 记得要在mode层判空
@Controller("item")
@RequestMapping("/item")
//解决跨域
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class ItemController extends BaseController{
@Resource
private ItemService itemService;
//创建商品的接口
@RequestMapping(value = "/createItem")
@ResponseBody
public CommonReturnType createItem(@RequestParam(name = "title")String title,
@RequestParam(name = "price") BigDecimal price,
@RequestParam(name = "stock")Integer stock,
@RequestParam(name = "description")String description,
@RequestParam(name = "seles")Integer seles,
@RequestParam(name = "imgUrl")String imgUrl) throws BusinessException {
//封装service请求来创建商品
ItemModel itemModel=new ItemModel();
itemModel.setTitle(title);
itemModel.setPrice(price);
itemModel.setStock(stock);
itemModel.setDescription(description);
itemModel.setSeles(seles);
itemModel.setImgUrl(imgUrl);
ItemModel item = itemService.createItem(itemModel);
ItemVo itemVo=new ItemVo();
BeanUtils.copyProperties(itemModel,itemVo);
return CommonReturnType.create(itemVo);
}
//根据商品id浏览详情接口
@RequestMapping(value = "/get",method = {RequestMethod.GET})
@ResponseBody
public CommonReturnType getItem(@RequestParam(name = "id")Integer id){
ItemModel item = itemService.getItemById(id);
ItemVo itemVo=new ItemVo();
BeanUtils.copyProperties(item,itemVo);
return CommonReturnType.create(itemVo);
}
在xml文件编写sql
<select id="listItem" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from item
</select>
service实现
//商品列表
@Override
public List<ItemModel> listItem() {
//查询所有的商品
List<ItemDao> itemDOList = itemDaoMapper.listItem();
//把查询到的商品封装成ItemModel
List<ItemModel> itemModelList = itemDOList.stream().map(itemDO -> {
ItemStockDao itemStockDO = itemStockDaoMapper.selectByItemId(itemDO.getId());
ItemModel itemModel = converItemStockDaoItemDaoFromitemModel(itemStockDO, itemDO);
return itemModel;
}).collect(Collectors.toList());
return itemModelList;
}
controller实现
//商品列表页面浏览
//根据商品id浏览详情接口
@RequestMapping(value = "/list",method = {RequestMethod.GET})
@ResponseBody
public CommonReturnType listItem(){
List<ItemModel> itemModels = itemService.listItem();
return CommonReturnType.create(itemModels);
}