发布于2021-05-29 22:44 阅读(989) 评论(0) 点赞(16) 收藏(0)
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for item
-- ----------------------------
DROP TABLE IF EXISTS `item`;
CREATE TABLE `item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`price` double(10,0) NOT NULL DEFAULT '0',
`description` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`sales` int(11) NOT NULL DEFAULT '0',
`img_url` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- ----------------------------
-- Table structure for item_stock
-- ----------------------------
DROP TABLE IF EXISTS `item_stock`;
CREATE TABLE `item_stock` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`stock` int(11) NOT NULL DEFAULT '0' COMMENT '库存',
`item_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
<configuration>
<!--允许移动生成的文件-->
<verbose>true</verbose>
<!--允许自动覆盖文件(生产环境中千万不要这样做)-->
<overwrite>false</overwrite>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
修改mybatis-generator配置文件
<table tableName="item" domainObjectName="ItemDO"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="item_stock" domainObjectName="ItemStockDO"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"></table>
重新运行mybatis-generator
<insert id="insert" parameterType="com.miaoshaproject.dataobject.ItemDO" useGeneratedKeys="true" keyProperty="id">
<insert id="insertSelective" parameterType="com.miaoshaproject.dataobject.ItemDO" useGeneratedKeys="true" keyProperty="id">
<insert id="insertSelective" parameterType="com.miaoshaproject.dataobject.ItemStockDO" useGeneratedKeys="true" keyProperty="id">
<html>
<head>
<meta charset="UTF-8">
<link href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="static/assets/global/plugins/css/component.css" rel="stylesheet" type="text/css"/>
<link href="static/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css"/>
<script src="static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
<title>Title</title>
</head>
<body class="login">
<div class="content">
<h3 class="form-title">创建商品</h3>
<div class="form-group">
<label class="control-label">商品名</label>
<div>
<input class="form-control" type="text" name="title" id="title">
</div>
</div>
<div class="form-group">
<label class="control-label">商品描述</label>
<div>
<input class="form-control" type="text" name="description" id="description">
</div>
</div>
<div class="form-group">
<label class="control-label">商品价格</label>
<div>
<input class="form-control" type="text" name="price" id="price">
</div>
</div>
<div class="form-group">
<label class="control-label">商品图片</label>
<div>
<input class="form-control" type="text" name="imgUrl" id="imgUrl">
</div>
</div>
<div class="form-group">
<label class="control-label">库存</label>
<div>
<input class="form-control" type="text" name="stock" id="stock">
</div>
</div>
<div class="form-actions">
<button class="btn blue" id="create" type="submit">
提交创建
</button>
</div>
</div>
</body>
<script>
jQuery(document).ready(function () {
//绑定otp的click事件用于向后端发送获取手机验证码的请求
$("#create").on("click",function () {
var title = $("#title").val();
var price = $("#price").val();
var stock = $("#stock").val();
var description = $("#description").val();
var imgUrl = $("#imgUrl").val();
if (title == null || title == "") {
alert("商品名不能为空");
return false;
}
if (price == null || price == "") {
alert("商品价格不能为空");
return false;
}
if (stock == null || stock == "") {
alert("商品库存不能为空");
return false;
}
if (description == null || description == "") {
alert("商品描述不能为空");
return false;
}
if (imgUrl == null || imgUrl == "") {
alert("商品图片不能为空");
return false;
}
//映射到后端@RequestMapping(value = "/register", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
$.ajax({
type:"POST",
contentType:"application/x-www-form-urlencoded",
url:"http://localhost:8090/item/create",
data:{
"title": title,
"price": price,
"stock": stock,
"description": description,
"imgUrl": imgUrl,
"name":name
},
//允许跨域请求
xhrFields:{withCredentials:true},
success:function (data) {
if (data.status=="success") {
alert("创建成功");
}else {
alert("创建失败,原因为" + data.data.errMsg);
}
},
error:function (data) {
alert("创建失败,原因为"+data.responseText);
}
});
return false;
});
});
</script>
</html>
<select id="selectByItemId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from item_stock
where item_id = #{item_id,jdbcType=INTEGER}
</select>
ItemStockDOMapper.java
ItemStockDO selectByItemId(Integer itemId);
public interface ItemService {
//创建商品
ItemModel createItem(ItemModel itemModel);
//商品列表浏览
List<ItemModel> listItem();
//商品详情浏览
ItemModel getItemById(Integer id);
}
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ValidatorImpl validator;
@Autowired
private ItemDOMapper itemDOMapper;
@Autowired
private ItemStockDOMapper itemStockDOMapper;
private ItemDO convertItemDOFromItemModel(ItemModel itemModel) {
if (itemModel == null) {
return null;
}
ItemDO itemDO = new ItemDO();
BeanUtils.copyProperties(itemModel, itemDO);
itemDO.setPrice(itemModel.getPrice().doubleValue());//视频里有
return itemDO;
}
private ItemStockDO convertItemStockDOFromItemModel(ItemModel itemModel) {
if (itemModel == null) {
return null;
}
ItemStockDO itemStockDO = new ItemStockDO();
itemStockDO.setItemId(itemModel.getId());
itemStockDO.setStock(itemModel.getStock());
return itemStockDO;
}
@Override
@Transactional
public ItemModel createItem(ItemModel itemModel) throws BusinessException {
//校验入参
ValidationResult result = validator.validate(itemModel);
if (result.isHasErrors()){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
}
//转化itemmodel->dataobject
ItemDO itemDO = this.convertItemDOFromItemModel(itemModel);
//写入数据库
itemDOMapper.insertSelective(itemDO);
itemModel.setId(itemDO.getId());
ItemStockDO itemStockDO = this.convertItemStockDOFromItemModel(itemModel);
itemStockDOMapper.insertSelective(itemStockDO);
//返回创建完成的对象
return this.getItemById(itemModel.getId());
}
@Override
public List<ItemModel> listItem() {
return null;
}
@Override
public ItemModel getItemById(Integer id) {
ItemDO itemDO = itemDOMapper.selectByPrimaryKey(id);
if (itemDO == null) {
return null;
}
//操作获得库存数量
ItemStockDO itemStockDO = itemStockDOMapper.selectByItemId(itemDO.getId());
//将dataobject-> Model
ItemModel itemModel = convertModelFromDataObject(itemDO, itemStockDO);
return itemModel;
}
private ItemModel convertModelFromDataObject(ItemDO itemDO, ItemStockDO itemStockDO) {
ItemModel itemModel = new ItemModel();
BeanUtils.copyProperties(itemDO, itemModel);
itemModel.setPrice(new BigDecimal(itemDO.getPrice()));
itemModel.setStock(itemStockDO.getStock());
return itemModel;
}
}
public class ItemModel {
private Integer id;
//商品名称
@NotBlank(message = "商品名称不能为空")
private String title;
//商品价格
@NotNull(message = "商品价格不能为空")
@Min(value = 0,message = "商品价格必须大于0")
private BigDecimal price;
//商品的库存
@NotNull(message = "库存不能不填")
private Integer stock;
//商品的描述
@NotBlank(message = "商品描述信息不能为空")
private String description;
//商品的销量
private Integer sales;
//商品描述图片的url
@NotBlank(message = "商品图片信息不能为空")
private String imgUrl;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getSales() {
return sales;
}
public void setSales(Integer sales) {
this.sales = sales;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
}
//用于前端展示
public class ItemVO {
private Integer id;
//商品名称
private String title;
//商品价格
private BigDecimal price;
//商品的库存
private Integer stock;
//商品的描述
private String description;
//商品的销量
private Integer sales;
//商品描述图片的url
private String imgUrl;
public Integer getId() {
return id;
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getSales() {
return sales;
}
public void setSales(Integer sales) {
this.sales = sales;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
}
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ValidatorImpl validator;
@Autowired
private ItemDOMapper itemDOMapper;
@Autowired
private ItemStockDOMapper itemStockDOMapper;
private ItemDO convertItemDOFromItemModel(ItemModel itemModel) {
if (itemModel == null) {
return null;
}
ItemDO itemDO = new ItemDO();
BeanUtils.copyProperties(itemModel, itemDO);
itemDO.setPrice(itemModel.getPrice().doubleValue());//视频里有
return itemDO;
}
private ItemStockDO convertItemStockDOFromItemModel(ItemModel itemModel) {
if (itemModel == null) {
return null;
}
ItemStockDO itemStockDO = new ItemStockDO();
itemStockDO.setItemId(itemModel.getId());
itemStockDO.setStock(itemModel.getStock());
return itemStockDO;
}
@Override
@Transactional
public ItemModel createItem(ItemModel itemModel) throws BusinessException {
//校验入参
ValidationResult result = validator.validate(itemModel);
if (result.isHasErrors()){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
}
//转化itemmodel->dataobject
ItemDO itemDO = this.convertItemDOFromItemModel(itemModel);
//写入数据库
itemDOMapper.insertSelective(itemDO);
itemModel.setId(itemDO.getId());
ItemStockDO itemStockDO = this.convertItemStockDOFromItemModel(itemModel);
itemStockDOMapper.insertSelective(itemStockDO);
//返回创建完成的对象
return this.getItemById(itemModel.getId());
}
@Override
public List<ItemModel> listItem() {
return null;
}
@Override
public ItemModel getItemById(Integer id) {
ItemDO itemDO = itemDOMapper.selectByPrimaryKey(id);
if (itemDO == null) {
return null;
}
//操作获得库存数量
ItemStockDO itemStockDO = itemStockDOMapper.selectByItemId(itemDO.getId());
//将dataobject-> Model
ItemModel itemModel = convertModelFromDataObject(itemDO, itemStockDO);
return itemModel;
}
private ItemModel convertModelFromDataObject(ItemDO itemDO, ItemStockDO itemStockDO) {
ItemModel itemModel = new ItemModel();
BeanUtils.copyProperties(itemDO, itemModel);
itemModel.setPrice(new BigDecimal(itemDO.getPrice()));
itemModel.setStock(itemStockDO.getStock());
return itemModel;
}
}
创建成功
数据库保存成功!
//商品详情页面浏览--采用get,对服务端不产生任何变化操作
@RequestMapping(value = "/get", method = {RequestMethod.GET})
@ResponseBody
public CommonReturnType getItem(@RequestParam(name = "id") Integer id) {
ItemModel itemModel = itemService.getItemById(id);
ItemVO itemVO = this.convertVOFromModel(itemModel);
return CommonReturnType.create(itemVO);
}
http://localhost:8090/item/get?id=10
原文链接:https://blog.csdn.net/hequnwang10/article/details/117278042
作者:coding
链接:http://www.javaheidong.com/blog/article/207647/edc99ade6857305c952b/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!