mybatis 批量导入数据到mysql返回批量Id

1、首先mybatis版本必需是3.3.1或以上

2、mapper配置文件中



<insert id="insertOrderBatch" parameterType="java.util.List"
    useGeneratedKeys="true" keyProperty="id">
    insert into user (name,sex,age)
    values
	<foreach collection="list" item="item" index="index" separator=",">
    	(#{item.name,jdbcType=VARCHAR},
    	#{item.sex,jdbcType=VARCHAR},
    	#{item.age,jdbcType=INTEGER})
	</foreach>
</insert>
		


注意部分:

parameterType="java.util.List"
useGeneratedKeys="true"
keyProperty="id"
collection="list"

3、Mapper接口方法

public void insertUserBatch(List<UserEntity> entitys);

4、DAO实现

public List<Long> createUserBatch(List<UserEntity> userEntitys){
		userUpdateMapper.insertUserBatch(userEntitys);
		// 只有mybatis3.3.1才支持取返回ID
		List<UserEntity> users = userEntitys;
		List<Long> ids = Lists.newArrayList();
		if(users.size()>0){
			Long id;
			for(UserEntity user:users){
				id = null;
				id = user.getId();
				ids.add(id);
			}
		}
		return ids;




6、参考链接

https://blog.csdn.net/top_code/article/details/52404345

https://blog.csdn.net/u014336799/article/details/52023887

posted @ 2019-04-18 20:59  张什么锋  阅读(1260)  评论(0编辑  收藏  举报