mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
v1.0.9 release (^.^)YYa!!
This commit is contained in:
parent
ca8facf1df
commit
fd9465ecd9
@ -44,6 +44,7 @@ public class MultiDataSourceTester {
|
|||||||
|
|
||||||
MybatisFlexBootstrap.getInstance()
|
MybatisFlexBootstrap.getInstance()
|
||||||
.setDataSource(dataSource)
|
.setDataSource(dataSource)
|
||||||
|
.addMapper(AccountMapper.class)
|
||||||
.addDataSource("ds2", dataSource2)
|
.addDataSource("ds2", dataSource2)
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
@ -68,9 +69,11 @@ public class MultiDataSourceTester {
|
|||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
System.out.println("tx: " + success);
|
System.out.println("tx: " + success);
|
||||||
|
DataSourceKey.clear();
|
||||||
|
|
||||||
rows = Db.selectAll("tb_account");
|
AccountMapper mapper = MybatisFlexBootstrap.getInstance().getMapper(AccountMapper.class);
|
||||||
System.out.println(rows);
|
List<Account> accounts = mapper.selectAll();
|
||||||
|
System.out.println(accounts);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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.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<TenantAccount> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
INSERT INTO tb_account
|
||||||
|
VALUES (1, 'zhang', 18, '2020-01-11', 1),
|
||||||
|
(2, 'wang', 19, '2021-03-21', 2);
|
||||||
@ -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)
|
||||||
|
);
|
||||||
Loading…
x
Reference in New Issue
Block a user