Java项目实战 - 实现商品添加

商品的添加不只添加基本的商品信息,还要添加商品描述信息。

Dao

逆向工程已经实现了Mapper,不需要再做修改

Service

在ItemService中实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Override
public TaotaoResult createItem(TbItem item, String desc) {
// 补全字段
long itemId = IDUtils.genItemId();
item.setId(itemId); //id
item.setStatus((byte) 1); //商品状态,1-正常,2-下架,3-删除
item.setCreated(new Date());
item.setUpdated(new Date());

//Add Item
itemMapper.insert(item);

//Insert Desc
TaotaoResult result = insertItemDesc(itemId, desc);

return TaotaoResult.ok();
}

private TaotaoResult insertItemDesc(Long itemId, String desc){
TbItemDesc itemDesc = new TbItemDesc();
itemDesc.setItemId(itemId);
itemDesc.setItemDesc(desc);
itemDesc.setCreated(new Date());
itemDesc.setUpdated(new Date());
itemDescMapper.insert(itemDesc);

return TaotaoResult.ok();

}

Controller

在ItemServiceController中实现。

1
2
3
4
5
6
@RequestMapping(value = "/save", method = RequestMethod.POST)
@ResponseBody
public TaotaoResult createItem(TbItem item, String desc){
TaotaoResult result = itemService.createItem(item, desc);
return result;
}

DB结果验证

商品:

1
2
3
4
5
6
7
mysql> SELECT * FROM `tb_item` where title='商务投资';

+-----------------+----------+------------+-------+------+---------+-----------------------------------------------------------------------------------+-----+--------+---------------------+---------------------+
| id | title | sell_point | price | num | barcode | image | cid | status | created | updated |
+-----------------+----------+------------+-------+------+---------+-----------------------------------------------------------------------------------+-----+--------+---------------------+---------------------+
| 153156317952887 | 商务投资 | 商务投资 | 33300 | 4444 | 1241324 | http://192.168.220.133/images/2018/07/14/f93d15e9-2153-4d81-af0b-32fe26d63f01.png | 13 | 1 | 2018-07-14 18:13:00 | 2018-07-14 18:13:00 |
+-----------------+----------+------------+-------+------+---------+-----------------------------------------------------------------------------------+-----+--------+---------------------+---------------------+

描述:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
mysql> SELECT * FROM `tb_item_desc` where item_id=153156317952887;

+-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------+---------------------+---------------------+

| item_id | item_desc


| created | updated |
+-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------+---------------------+---------------------+
| 153156317952887 | <p>
<span style="font-family:&quot;background-color:#FFFFFF;">商务投资</span><span style="font-family:&quot;background-color:#FFFFFF;">商务投资</span><span style="font-family:&quot;background-color:#FFFFFF;">商务投资</span><span style="font-fami
y:&quot;background-color:#FFFFFF;">商务投资</span><span style="font-family:&quot;background-color:#FFFFFF;">商务投资</span><span style="font-family:&quot;background-color:#FFFFFF;"></span><span style="font-family:&quot;background-color:#FFFFFF;"></s
an><span style="font-family:&quot;background-color:#FFFFFF;"></span>
</p>
<p>
<span style="font-family:&quot;background-color:#FFFFFF;"><img src="http://192.168.220.133/images/2018/07/14/c23109f3-c223-4ffc-b58f-b5d286114a4a.png" alt="" /><br />
</span>
</p> | 2018-07-14 18:13:00 | 2018-07-14 18:13:00 |
+-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!