1.插入的sql语句
insert into table_name (cloum1,cloum2,cloum3) values
(value1,value2,value3),
(value1,value2,value3),
.....
2.java代码
// web层
package com.test.order.web;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.test.demo.service.UserService;
@Controller
public class UserController {
@Autowired
private OrderService orderService;
public void saveOrderBatch(List<Map<String, Object>> list){
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", list);
int n = orderService.insertOrderBatch(map);
if(n>0){
logger.info("Batch insert Order SUCCESS ,order info: 批量插入订单记录成功");
} else {
logger.info("Batch insert Order FAILD");
}
}
}
// service层
package com.test.order.service;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OrderService{
public int insertOrderBatch(Map<String, Object> map){
return dao.insertOrderBatch(map);
}
}
// dao层
package com.test.order.dao;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
/**
* 订单DAO接口
*/
@Repository
public interface OrderDao{
public int insertOrderBatch(Map<String, Object> map);
}
3.mapping 文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.dao.OrderDao">
<insert id ="insertOrderBatch" parameterType="java.util.Map" >
INSERT INTO drug_order(
id,order_no,prescription_id,parchaser_id,pharmacy_id,chainshop_id,
user_id,authorization_id,create_by,create_date,update_by,update_date,
del_flag,extfiled_1,extfiled_2,extfiled_3,extfiled_4,extfiled_5,extfiled_6
) VALUES
<foreach collection ="list" item="item" index= "index" separator =",">
(
REPLACE(UUID(), "-",''),
#{item.orderNo},
#{item.prescriptionId},
#{item.parchaserId},
#{item.pharmacyId},
#{item.chainshopId},
#{item.userId},
#{item.authorizationId},
#{item.userId},
now(),
#{item.userId},
now(),
'0',
#{item.extfiled1},
#{item.extfiled2},
#{item.extfiled3},
#{item.extfiled4},
#{item.extfiled5},
#{item.extfiled6}
)
</foreach >
</insert >
</mapper>