New work of mybatis plus team: mybatis mate easily learns data permissions

Java technology 2022-01-26 20:03:13 阅读数:886

new work mybatis team mybatis

brief introduction

mybatis-mate by mp Enterprise class module , Support sub database and sub table , Data audit 、 Data sensitive word filtering (AC Algorithm ), Field encryption , Dictionary write back ( Data binding ), Data access , Table structure is automatically generated SQL Maintenance, etc , Designed to process data more quickly and gracefully .

1、 The main function

  • Dictionary binding
  • Field encryption
  • Data desensitization
  • Table structure dynamic maintenance
  • Data audit records
  • Data range ( Data access )
  • Database sub database sub table 、 Dynamic data source 、 Read / write separation 、 Automatic switching of database health check .

2、 Use

2.1 Dependency import

Spring Boot Introduce automatic dependency annotation package

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-mate-starter</artifactId>
<version>1.0.8</version>
</dependency>

annotation ( Entity subcontracting uses )

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-mate-annotation</artifactId>
<version>1.0.8</version>
</dependency>

2.2 Field data binding ( Dictionary write back )

for example user_sex type sex The dictionary results are mapped to sexText attribute

@FieldDict(type = "user_sex", target = "sexText")
private Integer sex;
private String sexText;

Realization IDataDict Interface provides dictionary data source , Injection into Spring The container can be .

@Component
public class DataDict implements IDataDict {
/**
* Get... From the database or cache
*/
private Map<String, String> SEX_MAP = new ConcurrentHashMap<String, String>() {
{
put("0", " Woman ");
put("1", " male ");
}};
@Override
public String getNameByCode(FieldDict fieldDict, String code) {
System.err.println(" Field type :" + fieldDict.type() + ", code :" + code);
return SEX_MAP.get(code);
}
}

2.3 Field encryption

attribute @FieldEncrypt Annotations can be stored encrypted , Will automatically decrypt the query results , Support global configuration of encryption key algorithm , And annotation key algorithm , Can achieve IEncryptor Inject custom algorithms .

@FieldEncrypt(algorithm = Algorithm.PBEWithMD5AndDES)
private String password;

2.4 Field desensitization

attribute @FieldSensitive Annotation can automatically desensitize the source data according to the preset strategy , Default SensitiveType built-in 9 A common desensitization strategy .

for example : Chinese name 、 Bank card account number 、 Mobile phone number, etc. Desensitization strategy .

You can also customize the policy as follows :

@FieldSensitive(type = "testStrategy")
private String username;
@FieldSensitive(type = SensitiveType.mobile)
private String mobile;

Custom desensitization strategy testStrategy Add to the default policy Spring The container can be .

@Configuration
public class SensitiveStrategyConfig {
/**
* Injection desensitization strategy
*/
@Bean
public ISensitiveStrategy sensitiveStrategy() {
// Customize testStrategy Type desensitization treatment
return new SensitiveStrategy().addStrategy("testStrategy", t -> t + "***test***");
}
}

For example, article sensitive word filtering

/**
* Demonstrate article sensitive word filtering
*/
@RestController
public class ArticleController {
@Autowired
private SensitiveWordsMapper sensitiveWordsMapper;
// Test visit the following address to observe the request address 、 The interface returns data and console ( General parameter )
// No sensitive words http://localhost:8080/info?content=tom&see=1&age=18
// English sensitive words http://localhost:8080/info?content=my%20content%20is%20tomcat&see=1&age=18
// Chinese sensitive words http://localhost:8080/info?content=%E7%8E%8B%E5%AE%89%E7%9F%B3%E5%94%90%E5%AE%8B%E5%85%AB%E5%A4%A7%E5%AE%B6&see=1
// Multiple sensitive words http://localhost:8080/info?content=%E7%8E%8B%E5%AE%89%E7%9F%B3%E6%9C%89%E4%B8%80%E5%8F%AA%E7%8C%ABtomcat%E6%B1%A4%E5%A7%86%E5%87%AF%E7%89%B9&see=1&size=6
// Insert a word to become an insensitive word http://localhost:8080/info?content=%E7%8E%8B%E7%8C%AB%E5%AE%89%E7%9F%B3%E6%9C%89%E4%B8%80%E5%8F%AA%E7%8C%ABtomcat%E6%B1%A4%E5%A7%86%E5%87%AF%E7%89%B9&see=1&size=6
@GetMapping("/info")
public String info(Article article) throws Exception {
return ParamsConfig.toJson(article);
}
// Add a sensitive word and see if it works http://localhost:8080/add
// Observe 【 cat 】 The word is filtered http://localhost:8080/info?content=%E7%8E%8B%E5%AE%89%E7%9F%B3%E6%9C%89%E4%B8%80%E5%8F%AA%E7%8C%ABtomcat%E6%B1%A4%E5%A7%86%E5%87%AF%E7%89%B9&see=1&size=6
// Nested sensitive word processing http://localhost:8080/info?content=%E7%8E%8B%E7%8C%AB%E5%AE%89%E7%9F%B3%E6%9C%89%E4%B8%80%E5%8F%AA%E7%8C%ABtomcat%E6%B1%A4%E5%A7%86%E5%87%AF%E7%89%B9&see=1&size=6
// Multi nested sensitive words http://localhost:8080/info?content=%E7%8E%8B%E7%8E%8B%E7%8C%AB%E5%AE%89%E7%9F%B3%E5%AE%89%E7%9F%B3%E6%9C%89%E4%B8%80%E5%8F%AA%E7%8C%ABtomcat%E6%B1%A4%E5%A7%86%E5%87%AF%E7%89%B9&see=1&size=6
@GetMapping("/add")
public String add() throws Exception {
Long id = 3L;
if (null == sensitiveWordsMapper.selectById(id)) {
System.err.println(" Insert a sensitive word :" + sensitiveWordsMapper.insert(new SensitiveWords(id, " cat ")));
// Insert a sensitive word , Refresh algorithm engine sensitive words
SensitiveWordsProcessor.reloadSensitiveWords();
}
return "ok";
}
// Test visit the following address to observe the console ( request json Parameters )
// idea perform resources Catalog TestJson.http File test
@PostMapping("/json")
public String json(@RequestBody Article article) throws Exception {
return ParamsConfig.toJson(article);
}
}

2.5 DDL Automatic maintenance of data structure

Solve the problem of upgrading table structure initialization , Version release update SQL Maintenance issues , At present, we support MySql、

PostgreSQL.
@Component
public class PostgresDdl implements IDdl {
/**
* perform SQL Script mode
*/
@Override
public List<String> getSqlFiles() {
return Arrays.asList(
// Built in package mode
"db/tag-schema.sql",
// File absolute path mode
"D:\\db\\tag-data.sql"
);
}
}

Not just fixed execution , It can also be executed dynamically !!

ddlScript.run(new StringReader("DELETE FROM user;\n" +
"INSERT INTO user (id, username, password, sex, email) VALUES\n" +
"(20, 'Duo', '123456', 0, '[email protected]');"));

It also supports multiple data source execution !!!

@Component
public class MysqlDdl implements IDdl {
@Override
public void sharding(Consumer<IDdl> consumer) {
// Specify multiple data sources , Master library initialization and slave library automatic synchronization
String group = "mysql";
ShardingGroupProperty sgp = ShardingKey.getDbGroupProperty(group);
if (null != sgp) {
// Main library
sgp.getMasterKeys().forEach(key -> {
ShardingKey.change(group + key);
consumer.accept(this);
});
// Slave Library
sgp.getSlaveKeys().forEach(key -> {
ShardingKey.change(group + key);
consumer.accept(this);
});
}
}
/**
* perform SQL Script mode
*/
@Override
public List<String> getSqlFiles() {
return Arrays.asList("db/user-mysql.sql");
}
}

2.6 Dynamic multi data source master-slave free switching

@Sharding Annotations enable data sources to be used without restrictions , You can mapper Add annotation to layer , Hit wherever you want !!

@Mapper
@Sharding("mysql")
public interface UserMapper extends BaseMapper<User> {
@Sharding("postgres")
Long selectByUsername(String username);
}

You can also customize the strategy to uniformly deploy troops

@Component
public class MyShardingStrategy extends RandomShardingStrategy {
/**
* Decide to switch data sources key {@link ShardingDatasource}
*
* @param group Dynamic database group
* @param invocation {@link Invocation}
* @param sqlCommandType {@link SqlCommandType}
*/
@Override
public void determineDatasourceKey(String group, Invocation invocation, SqlCommandType sqlCommandType) {
// Data source group group Custom selection is enough , keys It is the master-slave multi node in the data source group , You can choose at random or control yourself
this.changeDatabaseKey(group, sqlCommandType, keys -> chooseKey(keys, invocation));
}
}

You can turn on the master-slave policy , Of course, you can also start a health examination !!!

Specific configuration :

mybatis-mate:
sharding:
health: true # Health detection
primary: mysql # Data source is selected by default
datasource:
mysql: # Database group
- key: node1
...
- key: node2
cluster: slave # Be responsible for the separation of reading and writing from the library sql Query operation , Main library master Default can not write
...
postgres:
- key: node1 # Data nodes
...

2.7 Distributed transaction log printing

Some of the configurations are as follows :


/**
* <p>
* Performance analysis interceptor , Used to output each SQL Statement and its execution time
* </p>
*/
@Slf4j
@Component
@Intercepts({@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
@Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
@Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})})
public class PerformanceInterceptor implements Interceptor {
/**
* SQL Maximum execution time , Over automatic stop , Help to find problems .
*/
private long maxTime = 0;
/**
* SQL Is it formatted
*/
private boolean format = false;
/**
* Whether to write log file <br>
* true Write log file , Do not block program execution !<br>
* If the set maximum execution time is exceeded, an exception will be prompted !
*/
private boolean writeInLog = false;
@Override
public Object intercept(Invocation invocation) throws Throwable {
Statement statement;
Object firstArg = invocation.getArgs()[0];
if (Proxy.isProxyClass(firstArg.getClass())) {
statement = (Statement) SystemMetaObject.forObject(firstArg).getValue("h.statement");
} else {
statement = (Statement) firstArg;
}
MetaObject stmtMetaObj = SystemMetaObject.forObject(statement);
try {
statement = (Statement) stmtMetaObj.getValue("stmt.statement");
} catch (Exception e) {
// do nothing
}
if (stmtMetaObj.hasGetter("delegate")) {//Hikari
try {
statement = (Statement) stmtMetaObj.getValue("delegate");
} catch (Exception e) {
}
}
String originalSql = null;
if (originalSql == null) {
originalSql = statement.toString();
}
originalSql = originalSql.replaceAll("[\\s]+", " ");
int index = indexOfSqlStart(originalSql);
if (index > 0) {
originalSql = originalSql.substring(index);
}
// Calculation execution SQL Time consuming
long start = SystemClock.now();
Object result = invocation.proceed();
long timing = SystemClock.now() - start;
// format SQL Print execution results
Object target = PluginUtils.realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(target);
MappedStatement ms = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
StringBuilder formatSql = new StringBuilder();
formatSql.append(" Time:").append(timing);
formatSql.append(" ms - ID:").append(ms.getId());
formatSql.append("\n Execute SQL:").append(sqlFormat(originalSql, format)).append("\n");
if (this.isWriteInLog()) {
if (this.getMaxTime() >= 1 && timing > this.getMaxTime()) {
log.error(formatSql.toString());
} else {
log.debug(formatSql.toString());
}
} else {
System.err.println(formatSql);
if (this.getMaxTime() >= 1 && timing > this.getMaxTime()) {
throw new RuntimeException(" The SQL execution time is too large, please optimize ! ");
}
}
return result;
}
@Override
public Object plugin(Object target) {
if (target instanceof StatementHandler) {
return Plugin.wrap(target, this);
}
return target;
}
@Override
public void setProperties(Properties prop) {
String maxTime = prop.getProperty("maxTime");
String format = prop.getProperty("format");
if (StringUtils.isNotEmpty(maxTime)) {
this.maxTime = Long.parseLong(maxTime);
}
if (StringUtils.isNotEmpty(format)) {
this.format = Boolean.valueOf(format);
}
}
public long getMaxTime() {
return maxTime;
}
public PerformanceInterceptor setMaxTime(long maxTime) {
this.maxTime = maxTime;
return this;
}
public boolean isFormat() {
return format;
}
public PerformanceInterceptor setFormat(boolean format) {
this.format = format;
return this;
}
public boolean isWriteInLog() {
return writeInLog;
}
public PerformanceInterceptor setWriteInLog(boolean writeInLog) {
this.writeInLog = writeInLog;
return this;
}
public Method getMethodRegular(Class<?> clazz, String methodName) {
if (Object.class.equals(clazz)) {
return null;
}
for (Method method : clazz.getDeclaredMethods()) {
if (method.getName().equals(methodName)) {
return method;
}
}
return getMethodRegular(clazz.getSuperclass(), methodName);
}
/**
* obtain sql The beginning of the statement
*
* @param sql
* @return
*/
private int indexOfSqlStart(String sql) {
String upperCaseSql = sql.toUpperCase();
Set<Integer> set = new HashSet<>();
set.add(upperCaseSql.indexOf("SELECT "));
set.add(upperCaseSql.indexOf("UPDATE "));
set.add(upperCaseSql.indexOf("INSERT "));
set.add(upperCaseSql.indexOf("DELETE "));
set.remove(-1);
if (CollectionUtils.isEmpty(set)) {
return -1;
}
List<Integer> list = new ArrayList<>(set);
Collections.sort(list, Integer::compareTo);
return list.get(0);
}
private final static SqlFormatter sqlFormatter = new SqlFormatter();
/**
* Format sql
*
* @param boundSql
* @param format
* @return
*/
public static String sqlFormat(String boundSql, boolean format) {
if (format) {
try {
return sqlFormatter.format(boundSql);
} catch (Exception ignored) {
}
}
return boundSql;
}
}

Use :

@RestController
@AllArgsConstructor
public class TestController {
private BuyService buyService;
// database test surface t_order Cannot insert data in case of transaction consistency , The ability to insert a description multiple data source transaction is invalid
// Test access http://localhost:8080/test
// Manufacturing transaction rollback http://localhost:8080/test?error=true You can also create errors by modifying the table structure
// notes ShardingConfig Inject dataSourceProvider Testable transaction invalidity
@GetMapping("/test")
public String test(Boolean error) {
return buyService.buy(null != error && error);
}
}

2.8 Data access

mapper Add annotation to layer :

// test test Type data permission range , Mixed paging mode
@DataScope(type = "test", value = {
// Association table user Alias u Specify Department field permissions
@DataColumn(alias = "u", name = "department_id"),
// Association table user Alias u Specify the phone number field ( Handle by your own judgment )
@DataColumn(alias = "u", name = "mobile")
})
@Select("select u.* from user u")
List<User> selectTestList(IPage<User> page, Long id, @Param("name") String username);

Simulate business processing logic :

@Bean
public IDataScopeProvider dataScopeProvider() {
return new AbstractDataScopeProvider() {
@Override
protected void setWhere(PlainSelect plainSelect, Object[] args, DataScopeProperty dataScopeProperty) {
// args Contained in the mapper Method , You can get it yourself if you need it
/*
// Test data permissions , Final execution SQL sentence
SELECT u.* FROM user u WHERE (u.department_id IN ('1', '2', '3', '5'))
AND u.mobile LIKE '%1533%'
*/
if ("test".equals(dataScopeProperty.getType())) {
// Business test type
List<DataColumnProperty> dataColumns = dataScopeProperty.getColumns();
for (DataColumnProperty dataColumn : dataColumns) {
if ("department_id".equals(dataColumn.getName())) {
// Append Department field IN Conditions , It can also be SQL sentence
Set<String> deptIds = new HashSet<>();
deptIds.add("1");
deptIds.add("2");
deptIds.add("3");
deptIds.add("5");
ItemsList itemsList = new ExpressionList(deptIds.stream().map(StringValue::new).collect(Collectors.toList()));
InExpression inExpression = new InExpression(new Column(dataColumn.getAliasDotName()), itemsList);
if (null == plainSelect.getWhere()) {
// non-existent where Conditions
plainSelect.setWhere(new Parenthesis(inExpression));
} else {
// There is where Conditions and Handle
plainSelect.setWhere(new AndExpression(plainSelect.getWhere(), inExpression));
}
} else if ("mobile".equals(dataColumn.getName())) {
// Support a custom condition
LikeExpression likeExpression = new LikeExpression();
likeExpression.setLeftExpression(new Column(dataColumn.getAliasDotName()));
likeExpression.setRightExpression(new StringValue("%1533%"));
plainSelect.setWhere(new AndExpression(plainSelect.getWhere(), likeExpression));
}
}
}
}
};
}

Final execution SQL Output :

SELECT u.* FROM user u
WHERE (u.department_id IN ('1', '2', '3', '5'))
AND u.mobile LIKE '%1533%' LIMIT 1, 10

At present, there is only a paid version , Learn more about mybatis-mate See... For use examples :

mybatis-mate-examples: mybatis-mate by mp Enterprise class module , Support sub database and sub table , Data audit 、 Data sensitive word filtering (AC Algorithm ), Field encryption , Dictionary write back ( Data binding ), Data access , Table structure is automatically generated SQL maintain etc.

copyright:author[Java technology],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201262003120461.html