From 118a7772834fb8a75e6a541d39f430b35d691608 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 25 Aug 2023 17:07:01 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=89=B9=E9=87=8F=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/test/BatchTester.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/BatchTester.java diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/BatchTester.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/BatchTester.java new file mode 100644 index 00000000..438c5ed0 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/BatchTester.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.row.Db; +import com.mybatisflex.core.row.RowUtil; +import com.mybatisflex.core.util.SqlUtil; +import org.apache.ibatis.logging.stdout.StdOutImpl; +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.Arrays; +import java.util.Date; +import java.util.List; + +/** + * @author 王帅 + * @since 2023-08-25 + */ +public class BatchTester { + + @BeforeClass + public static void init() { + DataSource dataSource = new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2) + .addScript("schema.sql") + .addScript("data.sql") + .build(); + + MybatisFlexBootstrap.getInstance() + .setDataSource(dataSource) + .setLogImpl(StdOutImpl.class) + .addMapper(AccountMapper.class) + .start(); + } + + @Test + public void testBatch() { + List accounts = initAccounts(); + int[] ints = Db.executeBatch(accounts, AccountMapper.class, AccountMapper::insertSelective); + System.out.println(Arrays.toString(ints)); + System.out.println(SqlUtil.toBool(ints)); + RowUtil.printPretty(Db.selectAll("tb_account")); + } + + private static List initAccounts() { + List accounts = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + Account account = new Account(); + account.setUserName("wangshuai" + i); + account.setAge(168 + i); + account.setBirthday(new Date()); + accounts.add(account); + } + return accounts; + } + +}