!269 fix: 自增 id 不应该被拼接到 insert 语句中。

Merge pull request !269 from 王帅/main
This commit is contained in:
Michael Yang 2023-08-10 07:45:10 +00:00 committed by Gitee
commit 1ac5461291
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 89 additions and 1 deletions

View File

@ -376,7 +376,7 @@ public class TableInfo {
primaryColumns[i] = idInfo.getColumn();
if (idInfo.getKeyType() != KeyType.Auto
|| (idInfo.getBefore() != null && idInfo.getBefore())
&& (idInfo.getBefore() != null && idInfo.getBefore())
) {
insertIdFields.add(idInfo.getColumn());
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.test;
import com.mybatisflex.core.MybatisFlexBootstrap;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.audit.ConsoleMessageCollector;
import com.mybatisflex.core.audit.MessageCollector;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.mapper.ArticleMapper;
import org.apache.ibatis.logging.nologging.NoLoggingImpl;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author 王帅
* @since 2023-08-10
*/
public class ArticleTester {
static ArticleMapper articleMapper;
@BeforeClass
public static void init() {
DataSource dataSource = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.addScript("data.sql")
.build();
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
.setDataSource(dataSource)
.setLogImpl(NoLoggingImpl.class)
.addMapper(ArticleMapper.class)
.start();
//开启审计功能
AuditManager.setAuditEnable(true);
//设置 SQL 审计收集器
MessageCollector collector = new ConsoleMessageCollector();
AuditManager.setMessageCollector(collector);
articleMapper = bootstrap.getMapper(ArticleMapper.class);
}
@Test
public void testBatch() {
int count = 10;
Collection<Article> articles = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
Article article = new Article();
article.setTitle("title" + i);
articles.add(article);
}
Db.executeBatch(articles, ArticleMapper.class, (mapper, article) -> {
System.out.println("article: " + article);
mapper.insertSelective(article);
});
articleMapper.selectAll().forEach(System.out::println);
}
}