mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
add @ColumnMask() annotation support
This commit is contained in:
parent
93aa7ca751
commit
0ea45c0d99
@ -25,7 +25,7 @@ public class MaskFactory {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 脱敏处理器
|
* 脱敏处理器,type : processer
|
||||||
*/
|
*/
|
||||||
private static Map<String, MaskProcesser> processerMap = new HashMap<>();
|
private static Map<String, MaskProcesser> processerMap = new HashMap<>();
|
||||||
|
|
||||||
@ -43,16 +43,46 @@ public class MaskFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册处理器,用户可以注册新的脱敏处理器 或者 覆盖内置的处理器
|
||||||
|
*
|
||||||
|
* @param type 处理器类型
|
||||||
|
* @param processer 脱敏处理器
|
||||||
|
*/
|
||||||
public static void registerMaskProcesser(String type, MaskProcesser processer) {
|
public static void registerMaskProcesser(String type, MaskProcesser processer) {
|
||||||
processerMap.put(type, processer);
|
processerMap.put(type, processer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static ThreadLocal<Boolean> skipFlags = new ThreadLocal<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 跳过脱敏处理
|
||||||
|
*/
|
||||||
|
public static void skipMask() {
|
||||||
|
skipFlags.set(Boolean.TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 恢复脱敏处理
|
||||||
|
*/
|
||||||
|
public static void restoreMask() {
|
||||||
|
skipFlags.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Object mask(String type, Object data) {
|
public static Object mask(String type, Object data) {
|
||||||
|
Boolean skipMask = skipFlags.get();
|
||||||
|
if (skipMask != null && skipMask) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
MaskProcesser maskProcesser = processerMap.get(type);
|
MaskProcesser maskProcesser = processerMap.get(type);
|
||||||
if (maskProcesser == null) {
|
if (maskProcesser == null) {
|
||||||
throw new IllegalStateException("Can not get mask processer for by type: " + type);
|
throw new IllegalStateException("Can not get mask processer for by type: " + type);
|
||||||
}
|
}
|
||||||
|
|
||||||
return maskProcesser.mask(data);
|
return maskProcesser.mask(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,15 +16,11 @@
|
|||||||
package com.mybatisflex.test;
|
package com.mybatisflex.test;
|
||||||
|
|
||||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||||
import com.mybatisflex.core.paginate.Page;
|
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
|
||||||
import com.mybatisflex.core.util.UpdateEntity;
|
|
||||||
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class EntityTestStarter {
|
public class EntityTestStarter {
|
||||||
|
|
||||||
@ -47,92 +43,92 @@ public class EntityTestStarter {
|
|||||||
accountMapper.selectOneById(1));
|
accountMapper.selectOneById(1));
|
||||||
System.out.println(account);
|
System.out.println(account);
|
||||||
|
|
||||||
|
//
|
||||||
List<Account> allAccount = bootstrap.execute(AccountMapper.class, accountMapper ->
|
// List<Account> allAccount = bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.selectListByQuery(QueryWrapper.create()));
|
// accountMapper.selectListByQuery(QueryWrapper.create()));
|
||||||
System.out.println(allAccount);
|
// System.out.println(allAccount);
|
||||||
|
//
|
||||||
|
//
|
||||||
Account newAccount = new Account();
|
// Account newAccount = new Account();
|
||||||
newAccount.setUserName("lisi");
|
// newAccount.setUserName("lisi");
|
||||||
newAccount.setAge(18);
|
// newAccount.setAge(18);
|
||||||
newAccount.setBirthday(new Date());
|
// newAccount.setBirthday(new Date());
|
||||||
bootstrap.execute(AccountMapper.class, accountMapper ->
|
// bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.insert(newAccount));
|
// accountMapper.insert(newAccount));
|
||||||
|
//
|
||||||
//新增后自动回填主键
|
// //新增后自动回填主键
|
||||||
System.out.println("newAccount.id >>>>>> " + newAccount.getId());
|
// System.out.println("newAccount.id >>>>>> " + newAccount.getId());
|
||||||
|
//
|
||||||
|
//
|
||||||
List<Account> newAccountList = new ArrayList<>();
|
// List<Account> newAccountList = new ArrayList<>();
|
||||||
for (int i = 0; i < 5; i++) {
|
// for (int i = 0; i < 5; i++) {
|
||||||
Account insertAccount = new Account();
|
// Account insertAccount = new Account();
|
||||||
insertAccount.setUserName("new_user_" + i);
|
// insertAccount.setUserName("new_user_" + i);
|
||||||
insertAccount.setAge(22);
|
// insertAccount.setAge(22);
|
||||||
insertAccount.setBirthday(new Date());
|
// insertAccount.setBirthday(new Date());
|
||||||
newAccountList.add(insertAccount);
|
// newAccountList.add(insertAccount);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
//批量插入数据
|
// //批量插入数据
|
||||||
bootstrap.execute(AccountMapper.class, accountMapper ->
|
// bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.insertBatch(newAccountList));
|
// accountMapper.insertBatch(newAccountList));
|
||||||
|
//
|
||||||
|
//
|
||||||
bootstrap.execute(AccountMapper.class, accountMapper ->
|
// bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.deleteById(1));
|
// accountMapper.deleteById(1));
|
||||||
|
//
|
||||||
|
//
|
||||||
bootstrap.execute(AccountMapper.class, accountMapper ->
|
// bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.deleteBatchByIds(Arrays.asList(1, 2, 3)));
|
// accountMapper.deleteBatchByIds(Arrays.asList(1, 2, 3)));
|
||||||
|
//
|
||||||
|
//
|
||||||
Map<String, Object> where = new HashMap<>();
|
// Map<String, Object> where = new HashMap<>();
|
||||||
where.put("id", 2);
|
// where.put("id", 2);
|
||||||
bootstrap.execute(AccountMapper.class, accountMapper ->
|
// bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.deleteByMap(where));
|
// accountMapper.deleteByMap(where));
|
||||||
|
//
|
||||||
|
//
|
||||||
Account updateAccount1 = UpdateEntity.wrap(Account.class);
|
// Account updateAccount1 = UpdateEntity.wrap(Account.class);
|
||||||
updateAccount1.setId(5L);
|
// updateAccount1.setId(5L);
|
||||||
updateAccount1.setUserName(null);
|
// updateAccount1.setUserName(null);
|
||||||
updateAccount1.setAge(60);
|
// updateAccount1.setAge(60);
|
||||||
bootstrap.execute(AccountMapper.class, accountMapper ->
|
// bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.update(updateAccount1, false));
|
// accountMapper.update(updateAccount1, false));
|
||||||
|
//
|
||||||
|
//
|
||||||
Account updateAccount2 = UpdateEntity.wrap(Account.class);
|
// Account updateAccount2 = UpdateEntity.wrap(Account.class);
|
||||||
updateAccount2.setId(6L);
|
// updateAccount2.setId(6L);
|
||||||
updateAccount2.setAge(40);
|
// updateAccount2.setAge(40);
|
||||||
bootstrap.execute(AccountMapper.class, accountMapper ->
|
// bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.update(updateAccount2));
|
// accountMapper.update(updateAccount2));
|
||||||
|
//
|
||||||
|
//
|
||||||
List<Account> allAccounts = bootstrap.execute(AccountMapper.class, accountMapper ->
|
// List<Account> allAccounts = bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.selectListByQuery(QueryWrapper.create()));
|
// accountMapper.selectListByQuery(QueryWrapper.create()));
|
||||||
System.out.println(allAccounts); //count 5
|
// System.out.println(allAccounts); //count 5
|
||||||
|
//
|
||||||
|
//
|
||||||
//分页查询,第 2 页,每页 3 条数据
|
// //分页查询,第 2 页,每页 3 条数据
|
||||||
Page<Account> accountPage = bootstrap.execute(AccountMapper.class, accountMapper ->
|
// Page<Account> accountPage = bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.paginate(2, 3, QueryWrapper.create()));
|
// accountMapper.paginate(2, 3, QueryWrapper.create()));
|
||||||
System.out.println(accountPage);
|
// System.out.println(accountPage);
|
||||||
|
//
|
||||||
|
//
|
||||||
Account optionsAccount = new Account();
|
// Account optionsAccount = new Account();
|
||||||
optionsAccount.setUserName("optionstest");
|
// optionsAccount.setUserName("optionstest");
|
||||||
optionsAccount.addOption("c1", 11);
|
// optionsAccount.addOption("c1", 11);
|
||||||
optionsAccount.addOption("c2", "zhang");
|
// optionsAccount.addOption("c2", "zhang");
|
||||||
optionsAccount.addOption("c3", new Date());
|
// optionsAccount.addOption("c3", new Date());
|
||||||
|
//
|
||||||
|
//
|
||||||
bootstrap.execute(AccountMapper.class, accountMapper ->
|
// bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.insert(optionsAccount));
|
// accountMapper.insert(optionsAccount));
|
||||||
System.out.println(">>>>>>> optionsAccount: " + optionsAccount.getId());
|
// System.out.println(">>>>>>> optionsAccount: " + optionsAccount.getId());
|
||||||
|
//
|
||||||
|
//
|
||||||
Account selectOptionsAccount = bootstrap.execute(AccountMapper.class, accountMapper ->
|
// Account selectOptionsAccount = bootstrap.execute(AccountMapper.class, accountMapper ->
|
||||||
accountMapper.selectOneById(optionsAccount.getId()));
|
// accountMapper.selectOneById(optionsAccount.getId()));
|
||||||
System.out.println(selectOptionsAccount);
|
// System.out.println(selectOptionsAccount);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user