From fd9465ecd9ac183ddfde9b21e25a65a766b846be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sun, 9 Apr 2023 16:27:32 +0800 Subject: [PATCH] v1.0.9 release (^.^)YYa!! --- .../test/MultiDataSourceTester.java | 7 +- .../com/mybatisflex/test/TenantAccount.java | 62 +++++++++++++ .../com/mybatisflex/test/TenantTester.java | 88 +++++++++++++++++++ .../src/main/resources/data03.sql | 3 + .../src/main/resources/schema03.sql | 8 ++ 5 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TenantAccount.java create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TenantTester.java create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data03.sql create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema03.sql diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MultiDataSourceTester.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MultiDataSourceTester.java index caad1bc0..1a8e6658 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MultiDataSourceTester.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MultiDataSourceTester.java @@ -44,6 +44,7 @@ public class MultiDataSourceTester { MybatisFlexBootstrap.getInstance() .setDataSource(dataSource) + .addMapper(AccountMapper.class) .addDataSource("ds2", dataSource2) .start(); @@ -68,9 +69,11 @@ public class MultiDataSourceTester { return false; }); System.out.println("tx: " + success); + DataSourceKey.clear(); - rows = Db.selectAll("tb_account"); - System.out.println(rows); + AccountMapper mapper = MybatisFlexBootstrap.getInstance().getMapper(AccountMapper.class); + List accounts = mapper.selectAll(); + System.out.println(accounts); } } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TenantAccount.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TenantAccount.java new file mode 100644 index 00000000..8d664d46 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TenantAccount.java @@ -0,0 +1,62 @@ +package com.mybatisflex.test; + +import com.mybatisflex.annotation.*; +import com.mybatisflex.core.mask.Masks; + +import java.util.Date; + +@Table("tb_account") +public class TenantAccount{ + @Id(keyType = KeyType.Auto) + private Long id; + + @ColumnMask(Masks.CHINESE_NAME) + private String userName; + + private int age; + + private Date birthday; + + @Column(tenantId = true) + private Long tenantId; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } + + public Long getTenantId() { + return tenantId; + } + + public void setTenantId(Long tenantId) { + this.tenantId = tenantId; + } +} diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TenantTester.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TenantTester.java new file mode 100644 index 00000000..1b9d9678 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TenantTester.java @@ -0,0 +1,88 @@ +/** + * 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.audit.AuditManager; +import com.mybatisflex.core.audit.ConsoleMessageCollector; +import com.mybatisflex.core.tenant.TenantFactory; +import com.mybatisflex.core.tenant.TenantManager; +import com.mybatisflex.mapper.TenantAccountMapper; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +import javax.sql.DataSource; +import java.util.List; + +import static com.mybatisflex.test.table.Tables.TENANT_ACCOUNT; + +public class TenantTester { + + public static void main(String[] args) { + + DataSource dataSource = new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2) + .addScript("schema03.sql") + .addScript("data03.sql") + .build(); + + MybatisFlexBootstrap.getInstance() + .setDataSource(dataSource) + .addMapper(TenantAccountMapper.class) + .start(); + + + //输出日志 + AuditManager.setAuditEnable(true); + AuditManager.setMessageCollector(new ConsoleMessageCollector()); + + //配置 tenantFactory + TenantManager.setTenantFactory(new TenantFactory() { + @Override + public Object[] getTenantIds() { + return new Object[]{1}; + } + }); + + TenantAccountMapper mapper = MybatisFlexBootstrap.getInstance().getMapper(TenantAccountMapper.class); + + //SELECT * FROM `tb_account` WHERE `tenant_id` = 1 + List tenantAccounts = mapper.selectAll(); + System.out.println(tenantAccounts); + + //SELECT * FROM `tb_account` WHERE `id` = 1 AND `tenant_id` IN (1) + TenantAccount tenantAccount1 = mapper.selectOneById(1); + System.out.println(tenantAccount1); + + //SELECT * FROM `tb_account` WHERE `id` = 2 AND `tenant_id` IN (1) + TenantAccount tenantAccount2 = mapper.selectOneById(2); + System.out.println(tenantAccount2); + + //DELETE FROM `tb_account` WHERE `id` >= 100 AND `tenant_id` = 1 + mapper.deleteByCondition(TENANT_ACCOUNT.ID.ge(100)); + + // UPDATE `tb_account` SET `age` = 10 WHERE `id` >= 100 AND `tenant_id` = 1 + TenantAccount updateAccount = new TenantAccount(); + updateAccount.setAge(10); + mapper.updateByCondition(updateAccount,TENANT_ACCOUNT.ID.ge(100)); + + //SELECT * FROM `tb_account` WHERE `id` >= 100 AND `tenant_id` = 1 LIMIT 1 + mapper.selectOneByCondition(TENANT_ACCOUNT.ID.ge(100)); + + //SELECT * FROM `tb_account` WHERE `id` >= 100 AND `tenant_id` = 1 LIMIT 10 + mapper.selectListByCondition(TENANT_ACCOUNT.ID.ge(100),10); + } +} diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data03.sql b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data03.sql new file mode 100644 index 00000000..007707a8 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data03.sql @@ -0,0 +1,3 @@ +INSERT INTO tb_account +VALUES (1, 'zhang', 18, '2020-01-11', 1), + (2, 'wang', 19, '2021-03-21', 2); \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema03.sql b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema03.sql new file mode 100644 index 00000000..2d9f7202 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema03.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS `tb_account` +( + `id` INTEGER PRIMARY KEY auto_increment, + `user_name` VARCHAR(100), + `age` INTEGER, + `birthday` DATETIME, + `tenant_id` INTEGER(11) +); \ No newline at end of file