Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use foreach to insert data into ms sql server database, and the auto increment field only returns the last data #2002

Closed
xiongbb opened this issue Aug 3, 2020 · 2 comments

Comments

@xiongbb
Copy link

xiongbb commented Aug 3, 2020

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

@harawata
Copy link
Member

harawata commented Aug 3, 2020

Hello @xiongbb ,

This is the same report as #1439 and the driver still does not support it, unfortunately.
microsoft/mssql-jdbc#245

Closing as there is nothing MyBatis can do.

@harawata harawata closed this as completed Aug 3, 2020
@xiongbb
Copy link
Author

xiongbb commented Aug 3, 2020

@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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants