MyBatis version
3.5.5
Database vendor and version
MS Sql Server 2008 R2,MS Sql Server 2019 com.microsoft.sqlserver:mssql-jdbc:8.4.0.jre8 com.microsoft.sqlserver:mssql-jdbc:7.4.1.jre8
Test case or example project
Table:
CREATE TABLE [dbo].[student](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
CONSTRAINT [PK_student] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
Entity:
public class Student implements Serializable {
private static final long serialVersionUID = -9113130818289196330L;
private Integer id;
private String name;
}
dao:
public interface StudentMapper {
int batchInsert(List<Student> list);
}
mapper:
<insert id="batchInsert" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
insert into student (name) values
<foreach collection="list" item="item" separator="," index="index">
(#{item.name,jdbcType=VARCHAR})
</foreach>
</insert>
test:
@Test
void batchInsert() {
Student student1=new Student();
student1.setName("Jack");
Student student2=new Student();
student2.setName("Tom");
List<Student>students=new ArrayList<>();
students.add(student1);
students.add(student2);
studentMapper.batchInsert(students);
students.forEach(
e->{
log.info("{}",e.getId());
}
);
}
Steps to reproduce
Expected result
10 9
Actual result
2020-08-03 22:42:35.691 INFO 1940 --- [ Test worker] c.e.testbatch.dao.StudentMapperTest : 10 2020-08-03 22:42:35.692 INFO 1940 --- [ Test worker] c.e.testbatch.dao.StudentMapperTest : null
Comment From: harawata
Hello @xiongbb ,
This is the same report as #1439 and the driver still does not support it, unfortunately. https://github.com/microsoft/mssql-jdbc/issues/245
Closing as there is nothing MyBatis can do.
Comment From: xiongbb
@harawata Thanks for your reply, I got it
<select id="batchInsert" parameterType="java.util.List" resultType="java.lang.Integer">
insert into student (name) output inserted.id values
<foreach collection="list" item="item" separator="," index="index">
(#{item.name,jdbcType=VARCHAR})
</foreach>
</select>