一、controller
@CrossOrigin
@RestController
@RequestMapping("/sku")
public class SkuDataController {
@Autowired
private SkuDataService skuDataService;
//添加sku
@PostMapping(value = "/add",produces = "application/json;charset=utf-8")
public Result saveSku(@RequestBody List<SkuData>list){
skuDataService.insert(list);
return new Result(true, StatusCode.OK,"insert success");
二、service
public interface SkuDataService {
void insertAll(List<SkuData> list);
}
impl
@Override
public void insertAll(List<SkuData> list) {
skuDataDao.insertAll(list);
}
三、dao
@Component
@org.apache.ibatis.annotations.Mapper
public interface SkuDataDao extends Mapper<SkuData> {
//Mybatis批量新增
@InsertProvider(type = Provider.class, method = "insertAll")
void insertAll(@Param(value="list")List<SkuData> skuData);
class Provider {
/* 批量插入 */
public String insertAll(Map map) {
List<SkuData> skuData = (List<SkuData>) map.get("list");
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO tb_sku ");
sb.append("(id, sn, name, price, num, alertNum, image, images, weight , createTime , updateTime, spuId, categoryId, categoryName, brandName, spec, saleNum, commentNum, status, version) ");
sb.append("VALUES ");
MessageFormat mf = new MessageFormat(
"(#'{'list[{0}].id}, " +
"#'{'list[{0}].sn}," +
"#'{'list[{0}].name}," +
"#'{'list[{0}].price," +
"#'{'list[{0}].num}," +
"#'{'list[{0}].alertNum}," +
"#'{'list[{0}].image}," +
"#'{'list[{0}].images}," +
"#'{'list[{0}].weight}," +
"#'{'list[{0}].createTime}," +
"#'{'list[{0}].createTime}," +
"#'{'list[{0}].updateTime}," +
"#'{'list[{0}].spuId}," +
"#'{'list[{0}].categoryId}," +
"#'{'list[{0}].categoryName}," +
"#'{'list[{0}].brandName}," +
"#'{'list[{0}].spec}," +
"#'{'list[{0}].saleNum}," +
"#'{'list[{0}].commentNum}," +
"#'{'list[{0}].status}," +
"#'{'list[{0}].version})");
for (int i = 0; i < skuData.size(); i++) {
sb.append(mf.format(new Object[]{i}));
if (i < skuData.size() - 1)
sb.append(",");
}
return sb.toString();
}
}
}
postman
http://localhost:9008/sku/agent/insertAll
[
{
"id": "100000001111",
"sn": "",
"name": "锤子手机 X23 8GB+128GB 幻夜蓝 水滴屏全面屏 游戏手机 移动联通电信全网通4G手机",
"price": 99999,
"num": 9980,
"alertNum": 100,
"image": "https://m.360buyimg.com/mobilecms/s720x720_jfs/t1/4612/28/6223/298257/5ba22d66Ef665222f/d97ed0b25cbe8c6e.jpg!q70.jpg.webp",
"images": "https://m.360buyimg.com/mobilecms/s720x720_jfs/t1/4612/28/6223/298257/5ba22d66Ef665222f/d97ed0b25cbe8c6e.jpg!q70.jpg.webp",
"weight": 10,
"createTime": "2020-05-01 00:00:00",
"updateTime": "2020-05-21 11:15:00",
"spuId": "889527500",
"categoryId": 0,
"categoryName": "手机",
"brandName": "vivo",
"spec": "{'颜色': '红色', '版本': '8GB+128GB'}",
"saleNum": 0,
"commentNum": 0,
"status": "1"
},
{
"id": "100000002222",
"sn": "",
"name": "薇妮(viney)女士单肩包 时尚牛皮女包百搭斜挎包女士手提大包(经典黑)",
"price": 87900,
"num": 10000,
"alertNum": 100,
"image": "https://m.360buyimg.com/mobilecms/s720x720_jfs/t5590/64/5811657380/234462/5398e856/5965e173N34179777.jpg!q70.jpg.webp",
"images": "https://m.360buyimg.com/mobilecms/s720x720_jfs/t5590/64/5811657380/234462/5398e856/5965e173N34179777.jpg!q70.jpg.webp",
"weight": 10,
"createTime": "2019-05-01 11:11:11",
"updateTime": "2019-05-01 11:11:11",
"spuId": "451277100",
"categoryId": 0,
"categoryName": "真皮包",
"brandName": "viney",
"spec": "{'颜色': '黑色'}",
"saleNum": 0,
"commentNum": 0,
"status": "1"
},
{
"id": "100000003333",
"sn": "",
"name": "巴布豆(BOBDOG)柔薄悦动婴儿拉拉裤XXL码80片(15kg以上)",
"price": 1,
"num": 10000,
"alertNum": 100,
"image": "https://m.360buyimg.com/mobilecms/s720x720_jfs/t23998/350/2363990466/222391/a6e9581d/5b7cba5bN0c18fb4f.jpg!q70.jpg.webp",
"images": "https://m.360buyimg.com/mobilecms/s720x720_jfs/t23998/350/2363990466/222391/a6e9581d/5b7cba5bN0c18fb4f.jpg!q70.jpg.webp",
"weight": 10,
"createTime": "2018-05-01 00:00:00",
"updateTime": "2018-05-01 11:11:11",
"spuId": "10000000616300",
"categoryId": 757,
"categoryName": "拉拉裤",
"brandName": "巴布豆",
"spec": "{}",
"saleNum": 0,
"commentNum": 0,
"status": "1"
},
{
"id": "100000004444",
"sn": "",
"name": "莎米特SUMMIT拉杆箱22英寸PC材质万向轮旅行箱行李箱PC154T4A可扩容 法拉利红",
"price": 78900,
"num": 10000,
"alertNum": 100,
"image": "https://m.360buyimg.com/mobilecms/s720x720_jfs/t1/25363/12/2929/274060/5c21df3aE1789bda7/030af31afd116ae0.jpg!q70.jpg.webp",
"images": "https://m.360buyimg.com/mobilecms/s720x720_jfs/t1/25363/12/2929/274060/5c21df3aE1789bda7/030af31afd116ae0.jpg!q70.jpg.webp",
"weight": 10,
"createTime": "2017-05-01 00:00:00",
"updateTime": "2017-05-01 11:11:11",
"spuId": "595502400",
"categoryId": 0,
"categoryName": "拉杆箱",
"brandName": "莎米特",
"spec": "{'颜色': '红色'}",
"saleNum": 0,
"commentNum": 0,
"status": "1"
}
]
网友评论