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>trueverbose>
<overwrite>falseoverwrite>
<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>Titletitle>
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