mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
build: v1.8.8 release (^.^)YYa!!
This commit is contained in:
parent
cfd48ae10a
commit
dd58df4844
@ -39,12 +39,12 @@ public class Account {
|
||||
private Long id;
|
||||
|
||||
private int sex;
|
||||
|
||||
|
||||
@Column(ignore = true) //非数据库字段,需配置忽略该属性
|
||||
private String sexString;
|
||||
|
||||
|
||||
//getter setter
|
||||
}
|
||||
```
|
||||
|
||||
更多的 `onSet` 还可以参考 [这里](./table.md)。
|
||||
更多的 `onSet` 还可以参考 [这里](./table.md)。
|
||||
|
||||
@ -57,7 +57,7 @@ public class AccountServiceImpl extends CacheableServiceImpl<AccountMapper, Acco
|
||||
@Service
|
||||
@CacheConfig(cacheNames = "account")
|
||||
public class AccountServiceImpl extends CacheableServiceImpl<AccountMapper, Account> implements AccountService {
|
||||
|
||||
|
||||
// 根据主键缓存数据
|
||||
@Override
|
||||
@Cacheable(key = "#id")
|
||||
@ -72,7 +72,7 @@ public class AccountServiceImpl extends CacheableServiceImpl<AccountMapper, Acco
|
||||
public List<Account> list(QueryWrapper query) {
|
||||
return super.list(query);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
@ -84,7 +84,7 @@ MyBatis-Flex 在 IService 接口中做了方法调用链优化,所以您只需
|
||||
@Service
|
||||
@CacheConfig(cacheNames = "account")
|
||||
public class AccountServiceImpl extends CacheableServiceImpl<MyAccountMapper, Account> {
|
||||
|
||||
|
||||
@Override
|
||||
@CacheEvict(allEntries = true)
|
||||
public boolean remove(QueryWrapper query) {
|
||||
@ -170,7 +170,7 @@ public class AccountServiceImpl extends CacheableServiceImpl<MyAccountMapper, Ac
|
||||
public <R> Page<R> pageAs(Page<R> page, QueryWrapper query, Class<R> asType) {
|
||||
return super.pageAs(page, query, asType);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@ -99,7 +99,7 @@ System.out.println(accounts);
|
||||
```java
|
||||
try {
|
||||
MaskManager.skipMask();
|
||||
|
||||
|
||||
//此处查询到的数据不会进行脱敏处理
|
||||
accountMapper.selectListByQuery(...);
|
||||
} finally {
|
||||
|
||||
@ -93,11 +93,11 @@ public class MyInsertListener implements InsertListener {
|
||||
@Override
|
||||
public void onInsert(Object entity) {
|
||||
Account account = (Account)entity;
|
||||
|
||||
|
||||
//设置 account 被新增时的一些默认数据
|
||||
account.setInsertTime(new Date());
|
||||
account.setInsertUserId("...");
|
||||
|
||||
|
||||
//多租户的场景下,设置当前 租户 ID ..
|
||||
account.setTenantId("....");
|
||||
}
|
||||
@ -139,15 +139,15 @@ public class MySetListener implements SetListener {
|
||||
public Object onSet(Object entity, String property, Object value){
|
||||
//场景1:用于检测当前账户是否拥有该字段权限,
|
||||
// 有正常返回 value,没有权限返回 null
|
||||
|
||||
|
||||
|
||||
|
||||
//场景2:entity 中可能定义某个业务值
|
||||
// 当监听到某个字段被赋值了,这
|
||||
// 里可以主动去给另外的其他字段赋值
|
||||
|
||||
|
||||
|
||||
|
||||
//场景3:内容转换和二次加工,对 value 值进行修改后返回
|
||||
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -177,4 +177,4 @@ config.registerUpdateListener(updateListener, Entity1.class, Entity2.class);
|
||||
|
||||
//为 Entity1 和 Entity2 注册 setListener
|
||||
config.registerSetListener(setListener, Entity1.class, Entity2.class);
|
||||
```
|
||||
```
|
||||
|
||||
@ -98,15 +98,15 @@ public class DbTest {
|
||||
account.setAge(1);
|
||||
List<Account> accounts = new ArrayList<>();
|
||||
accounts.add(account);
|
||||
Account account2 = UpdateEntity.of(Account.class, 2);
|
||||
account2.setAge(2);
|
||||
UpdateWrapper updateWrapper = UpdateWrapper.of(account2);
|
||||
Account account82 = UpdateEntity.of(Account.class, 2);
|
||||
account82.setAge(2);
|
||||
UpdateWrapper updateWrapper = UpdateWrapper.of(account82);
|
||||
updateWrapper.setRaw("age", "age+1");
|
||||
accounts.add(account2);
|
||||
Account account3 = new Account();
|
||||
account3.setId(3L);
|
||||
account3.setAge(4);
|
||||
accounts.add(account3);
|
||||
accounts.add(account82);
|
||||
Account account83 = new Account();
|
||||
account83.setId(3L);
|
||||
account83.setAge(4);
|
||||
accounts.add(account83);
|
||||
Db.updateEntitiesBatch(accounts);
|
||||
} catch (Exception e) {
|
||||
assert false;
|
||||
|
||||
@ -29,7 +29,6 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||
import static com.mybatisflex.test.table.ArticleTableDef.ARTICLE;
|
||||
|
||||
@ -30,6 +30,7 @@ import java.util.List;
|
||||
|
||||
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||
|
||||
|
||||
public class UpdateWrapperTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
@ -71,10 +72,10 @@ public class UpdateWrapperTest {
|
||||
|
||||
System.out.println("//////////account3");
|
||||
|
||||
Account account3 = UpdateEntity.of(Account.class, 1);
|
||||
UpdateWrapper<Account> wrapper3 = (UpdateWrapper) account3;
|
||||
Account account83 = UpdateEntity.of(Account.class, 1);
|
||||
UpdateWrapper<Account> wrapper3 = (UpdateWrapper) account83;
|
||||
wrapper3.setRaw(Account::getAge, "age + 1");
|
||||
accountMapper.update(account3);
|
||||
accountMapper.update(account83);
|
||||
|
||||
List<Account> accounts3 = accountMapper.selectAll();
|
||||
System.out.println(accounts3);
|
||||
@ -82,10 +83,10 @@ public class UpdateWrapperTest {
|
||||
|
||||
System.out.println("//////////account4");
|
||||
|
||||
Account account4 = UpdateEntity.of(Account.class, 1);
|
||||
UpdateWrapper wrapper4 = (UpdateWrapper) account4;
|
||||
Account account84 = UpdateEntity.of(Account.class, 1);
|
||||
UpdateWrapper wrapper4 = (UpdateWrapper) account84;
|
||||
wrapper4.setRaw(ACCOUNT.AGE, ACCOUNT.AGE.add(1));
|
||||
accountMapper.update(account4);
|
||||
accountMapper.update(account84);
|
||||
|
||||
List<Account> accounts4 = accountMapper.selectAll();
|
||||
System.out.println(accounts4);
|
||||
|
||||
@ -24,6 +24,7 @@ import org.nustaq.serialization.FSTConfiguration;
|
||||
|
||||
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||
|
||||
|
||||
public class WrapperSerializeTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
@ -33,7 +33,7 @@ public class Book implements Serializable {
|
||||
private String content;
|
||||
|
||||
@RelationManyToOne(selfField = "accountId", targetField = "id")
|
||||
private Account account;
|
||||
private RelationAccount account;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
@ -67,11 +67,11 @@ public class Book implements Serializable {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public Account getAccount() {
|
||||
public RelationAccount getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public void setAccount(Account account) {
|
||||
public void setAccount(RelationAccount account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
|
||||
@ -24,8 +24,8 @@ import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Table(value = "tb_account")
|
||||
public class Account implements Serializable {
|
||||
@Table(value = "tb_relation_account")
|
||||
public class RelationAccount implements Serializable {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
@ -36,7 +36,7 @@ public class Role implements Serializable {
|
||||
selfField = "id", joinSelfColumn = "role_id",
|
||||
targetField = "id", joinTargetColumn = "account_id"
|
||||
)
|
||||
private List<Account> accounts;
|
||||
private List<RelationAccount> accounts;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
@ -54,11 +54,11 @@ public class Role implements Serializable {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Account> getAccounts() {
|
||||
public List<RelationAccount> getAccounts() {
|
||||
return accounts;
|
||||
}
|
||||
|
||||
public void setAccounts(List<Account> accounts) {
|
||||
public void setAccounts(List<RelationAccount> accounts) {
|
||||
this.accounts = accounts;
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,6 @@ import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||
import com.mybatisflex.core.datasource.DataSourceKey;
|
||||
import com.mybatisflex.mapper.Account6Mapper;
|
||||
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||
import org.assertj.core.api.WithAssertions;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
@ -17,7 +16,6 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
||||
@ -23,13 +23,13 @@ import com.mybatisflex.core.datasource.DataSourceKey;
|
||||
import com.mybatisflex.core.paginate.Page;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.relation.RelationManager;
|
||||
import com.mybatisflex.test.relation.mapper.AccountMapper;
|
||||
import com.mybatisflex.test.relation.mapper.BookMapper;
|
||||
import com.mybatisflex.test.relation.mapper.MenuMapper;
|
||||
import com.mybatisflex.test.relation.onetoone.Account;
|
||||
import com.mybatisflex.test.relation.mapper.RelationAccountMapper;
|
||||
import com.mybatisflex.test.relation.onetoone.AccountDTO;
|
||||
import com.mybatisflex.test.relation.onetoone.Book;
|
||||
import com.mybatisflex.test.relation.onetoone.Menu;
|
||||
import com.mybatisflex.test.relation.onetoone.RelationAccount;
|
||||
import lombok.SneakyThrows;
|
||||
import net.javacrumbs.jsonunit.assertj.JsonAssertions;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
@ -51,7 +51,7 @@ import static com.mybatisflex.test.relation.onetoone.table.MenuTableDef.MENU;
|
||||
|
||||
public class RelationsTest implements WithAssertions {
|
||||
|
||||
private AccountMapper accountMapper;
|
||||
private RelationAccountMapper relationAccountMapper;
|
||||
private BookMapper bookMapper;
|
||||
private MenuMapper menuMapper;
|
||||
private EmbeddedDatabase dataSource;
|
||||
@ -75,14 +75,14 @@ public class RelationsTest implements WithAssertions {
|
||||
|
||||
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
|
||||
.setDataSource(DATA_SOURCE_KEY, dataSource)
|
||||
.addMapper(AccountMapper.class)
|
||||
.addMapper(RelationAccountMapper.class)
|
||||
.addMapper(BookMapper.class)
|
||||
.addMapper(MenuMapper.class)
|
||||
.start();
|
||||
|
||||
DataSourceKey.use(DATA_SOURCE_KEY);
|
||||
|
||||
accountMapper = bootstrap.getMapper(AccountMapper.class);
|
||||
relationAccountMapper = bootstrap.getMapper(RelationAccountMapper.class);
|
||||
bookMapper = bootstrap.getMapper(BookMapper.class);
|
||||
menuMapper = bootstrap.getMapper(MenuMapper.class);
|
||||
}
|
||||
@ -96,7 +96,7 @@ public class RelationsTest implements WithAssertions {
|
||||
@Test
|
||||
@SneakyThrows
|
||||
public void testOneToOne() {
|
||||
List<com.mybatisflex.test.relation.onetoone.Account> accounts = accountMapper.selectAllWithRelations();
|
||||
List<RelationAccount> accounts = relationAccountMapper.selectAllWithRelations();
|
||||
assertThat(accounts).hasSize(5);
|
||||
assertRelationResult(accounts, "relation/result/account-relation-result.json");
|
||||
}
|
||||
@ -114,18 +114,18 @@ public class RelationsTest implements WithAssertions {
|
||||
|
||||
@Test
|
||||
public void testManyToMany1() {
|
||||
List<com.mybatisflex.test.relation.onetoone.Account> accounts = accountMapper.selectAll();
|
||||
List<RelationAccount> accounts = relationAccountMapper.selectAll();
|
||||
assertThat(accounts).hasSize(5)
|
||||
.extracting(Account::getId)
|
||||
.extracting(RelationAccount::getId)
|
||||
.containsExactly(1L, 2L, 3L, 4L, 5L);
|
||||
|
||||
RelationManager.queryRelations(accountMapper, accounts);
|
||||
RelationManager.queryRelations(relationAccountMapper, accounts);
|
||||
assertRelationResult(accounts, "relation/result/account-relation-result.json");
|
||||
}
|
||||
|
||||
// @Test
|
||||
// @Test
|
||||
public void testAsDto() {
|
||||
List<com.mybatisflex.test.relation.onetoone.AccountDTO> accounts = accountMapper.selectListWithRelationsByQueryAs(QueryWrapper.create(), AccountDTO.class);
|
||||
List<com.mybatisflex.test.relation.onetoone.AccountDTO> accounts = relationAccountMapper.selectListWithRelationsByQueryAs(QueryWrapper.create(), AccountDTO.class);
|
||||
assertRelationResult(accounts, "relation/result/accountDto-relation-result.json");
|
||||
}
|
||||
|
||||
@ -150,7 +150,7 @@ public class RelationsTest implements WithAssertions {
|
||||
|
||||
@Test
|
||||
public void testPaginate() {
|
||||
Page<Account> accountPage = accountMapper.paginateWithRelations(1, 2, QueryWrapper.create());
|
||||
Page<RelationAccount> accountPage = relationAccountMapper.paginateWithRelations(1, 2, QueryWrapper.create());
|
||||
assertRelationResult(accountPage.getRecords(), "relation/result/account-page-relation-result.json");
|
||||
}
|
||||
|
||||
|
||||
@ -37,7 +37,8 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.mybatisflex.test.relation.onetoone.table.AccountTableDef.ACCOUNT;
|
||||
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||
|
||||
|
||||
public class RowTest implements WithAssertions {
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
INSERT INTO tb_account
|
||||
INSERT INTO tb_relation_account
|
||||
VALUES (1, '孙悟空', 18),
|
||||
(2, '猪八戒', 19),
|
||||
(3, '沙和尚', 19),
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
CREATE TABLE IF NOT EXISTS `tb_account`
|
||||
CREATE TABLE IF NOT EXISTS `tb_relation_account`
|
||||
(
|
||||
`id` INTEGER,
|
||||
`user_name` VARCHAR(100),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user