Mybatis 循环插入

方式一:
service 层
List<Map<String,Object>> data = new ArrayList<>();
dispatchDao.insertBatch(data);
 
dao层
void insertBatch(List<Map<String,Object>> list);
 
xml
<insert id="insertBatch" parameterType="java.util.List">
insert into C_DP_DISPATCH(
DISPATCH_ID,
DISPATCH_BATCH_ID,
ENTERPRISE_ID,
ENTERPRISE_NAME,
OBJECT_TYPE,
OBJECT_ID,
MISSION_NAME,
MISSION_TYPE,
ISSUE_TIME,
STOP_TIME,
MISSION_CONTENT,
OBJECT_NAME,
IS_DEAL,
IS_ACTIVE,
CREATOR,
CREATE_TIME,
UPDATOR,
UPDATE_TIME
)values
<foreach collection="list" item="item" separator=",">
(
#{item.dispatchId},
#{item.dispatchBatchId},
#{item.enterpriseId},
#{item.enterpriseName},
#{item.objectType},
#{item.objectId},
#{item.missionName},
#{item.missionType},
#{item.issueTime,typeHandler=com.topinfo.common.util.MybatisTimeHandler},
#{item.stopTime,typeHandler=com.topinfo.common.util.MybatisTimeHandler},
#{item.missionContent},
#{item.objectName},
#{item.isDeal},
#{item.isActive},
#{item.creator},
#{item.createTime,typeHandler=com.topinfo.common.util.MybatisTimeHandler},
null,
null
)
</foreach>
</insert>

  

 
 
方式二:
 service 层
/**
* @Description 保存问题
**/
private void doSaveData(List<QuestionExcel> list, String jwtdid, String jwtpid) {
String nowTime = LocalDateTime.now().format(TpConstant.DateFormatter.yyyyMMddHHmmss);
 
//批量保存
List<SQaQuestion> questionList = list.stream().map(q -> {
SQaQuestion question = new SQaQuestion();
BeanUtil.copyProperties(q, question);
question.setQuestionId(SnowflakeIdUtil.nextIdStr());
question.setCreator(jwtpid);
question.setCreateTime(nowTime);
question.setUpdator(jwtpid);
question.setUpdateTime(nowTime);
question.setActived(TpConstant.YES);
question.setEnterDeptId(jwtdid);
return question;
}).collect(Collectors.toList());
sQaQuestionMapper.batchAdd(questionList);
}

 

 dao层

int batchAdd(@Param("questionList") List<SQaQuestion> questionList);
 
xml
<insert id="batchAdd">
INSERT INTO s_qa_question
(question_id, industry_type_code, question_type, question_content, question_option_a, question_option_b, question_option_c, question_option_d, extend_value1, extend_value2, right_key, chapters, question_source, remark, sort_no, answerd, enter_dept_id, actived, creator, create_time, updator, update_time)
VALUES
<foreach collection="questionList" item="question" separator=",">
(#{question.questionId}, #{question.industryTypeCode}, #{question.questionType}, #{question.questionContent}
, #{question.questionOptionA}, #{question.questionOptionB}, #{question.questionOptionC}, #{question.questionOptionD}
, #{question.extendValue1}, #{question.extendValue2}, #{question.rightKey}, #{question.chapters}
, #{question.questionSource}, #{question.remark}, #{question.sortNo}, #{question.answerd}
, #{question.enterDeptId}, #{question.actived}, #{question.creator}, #{question.createTime}
, #{question.updator}, #{question.updateTime})
</foreach>
</insert>
 

  

 
 
 
 
posted @ 2022-03-23 16:23  甜8筒  阅读(1101)  评论(0编辑  收藏  举报