mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
!328 使用lambda优化部分写法和使用try-with-resources释放Connection
Merge pull request !328 from handy/handy
This commit is contained in:
commit
757cb9c813
@ -24,7 +24,6 @@ import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
|
||||
import javax.sql.DataSource;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@ -66,19 +65,10 @@ public class DbTypeUtil {
|
||||
}
|
||||
}
|
||||
|
||||
Connection connection = null;
|
||||
try {
|
||||
connection = dataSource.getConnection();
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
return connection.getMetaData().getURL();
|
||||
} catch (Exception e) {
|
||||
throw FlexExceptions.wrap(e, LocalizedFormats.DATASOURCE_JDBC_URL);
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) { //ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@ import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author michael
|
||||
@ -96,10 +97,10 @@ public class FastjsonTypeHandler extends BaseJsonTypeHandler<Object> {
|
||||
} else {
|
||||
if (this.ownerType != null) {
|
||||
if (this.ownerType.equals(that.ownerType)) {
|
||||
return this.rawType != null ? this.rawType.equals(that.rawType) : that.rawType == null;
|
||||
return Objects.equals(this.rawType, that.rawType);
|
||||
}
|
||||
} else if (that.ownerType == null) {
|
||||
return this.rawType != null ? this.rawType.equals(that.rawType) : that.rawType == null;
|
||||
return Objects.equals(this.rawType, that.rawType);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@ -127,9 +127,7 @@ public class Db {
|
||||
* @param batchSize 每次提交的数据量
|
||||
*/
|
||||
public static int[] insertBatch(String schema, String tableName, Collection<Row> rows, int batchSize) {
|
||||
return executeBatch(rows, batchSize, RowMapper.class, (mapper, row) -> {
|
||||
mapper.insert(schema, tableName, row);
|
||||
});
|
||||
return executeBatch(rows, batchSize, RowMapper.class, (mapper, row) -> mapper.insert(schema, tableName, row));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -140,9 +138,7 @@ public class Db {
|
||||
* @param batchSize 每次提交的数据量
|
||||
*/
|
||||
public static int[] insertBatch(String tableName, Collection<Row> rows, int batchSize) {
|
||||
return executeBatch(rows, batchSize, RowMapper.class, (mapper, row) -> {
|
||||
mapper.insert(null, tableName, row);
|
||||
});
|
||||
return executeBatch(rows, batchSize, RowMapper.class, (mapper, row) -> mapper.insert(null, tableName, row));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -976,7 +976,7 @@ public class TableInfo {
|
||||
|
||||
public List<QueryColumn> getDefaultQueryColumn() {
|
||||
return Arrays.stream(defaultQueryColumns)
|
||||
.map(name -> columnQueryMapping.get(name))
|
||||
.map(columnQueryMapping::get)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@ -70,12 +70,7 @@ public class AccountSqlTester {
|
||||
.where(ACCOUNT01.ID.ge(100))
|
||||
.and(ACCOUNT.SEX.eq(1));
|
||||
|
||||
TableManager.setDynamicTableProcessor(new DynamicTableProcessor() {
|
||||
@Override
|
||||
public String process(String tableName) {
|
||||
return tableName + "_01";
|
||||
}
|
||||
});
|
||||
TableManager.setDynamicTableProcessor(tableName -> tableName + "_01");
|
||||
TableManager.setDynamicTableProcessor(original -> original + "_01");
|
||||
|
||||
System.out.println(query.toSQL());
|
||||
|
||||
@ -36,16 +36,13 @@ public class DataSourceDecipherTester {
|
||||
dataSource.setUsername("root123");
|
||||
dataSource.setPassword("123456---0000");
|
||||
|
||||
DataSourceManager.setDecipher(new DataSourceDecipher() {
|
||||
@Override
|
||||
public String decrypt(DataSourceProperty property, String value) {
|
||||
if (property == DataSourceProperty.USERNAME) {
|
||||
return value.substring(0, 4);
|
||||
} else if (property == DataSourceProperty.PASSWORD) {
|
||||
return value.substring(0, 6);
|
||||
}
|
||||
return value;
|
||||
DataSourceManager.setDecipher((property, value) -> {
|
||||
if (property == DataSourceProperty.USERNAME) {
|
||||
return value.substring(0, 4);
|
||||
} else if (property == DataSourceProperty.PASSWORD) {
|
||||
return value.substring(0, 6);
|
||||
}
|
||||
return value;
|
||||
});
|
||||
|
||||
MybatisFlexBootstrap.getInstance()
|
||||
|
||||
@ -176,18 +176,15 @@ public class EntityTestStarter {
|
||||
// Page<ArticleDTO01> paginate = accountMapper.paginateAs(Page.of(1, 10), asWrapper, ArticleDTO01.class);
|
||||
// System.out.println(paginate);
|
||||
|
||||
Db.tx(new Supplier<Boolean>() {
|
||||
@Override
|
||||
public Boolean get() {
|
||||
Cursor<Account> accounts = accountMapper.selectCursorByQuery(asWrapper);
|
||||
System.out.println(accounts.isOpen());
|
||||
for (Account account : accounts) {
|
||||
System.out.println(accounts.isOpen());
|
||||
System.out.println(account);
|
||||
}
|
||||
System.out.println(accounts.isOpen());
|
||||
return true;
|
||||
Db.tx(() -> {
|
||||
Cursor<Account> accounts1 = accountMapper.selectCursorByQuery(asWrapper);
|
||||
System.out.println(accounts1.isOpen());
|
||||
for (Account account : accounts1) {
|
||||
System.out.println(accounts1.isOpen());
|
||||
System.out.println(account);
|
||||
}
|
||||
System.out.println(accounts1.isOpen());
|
||||
return true;
|
||||
});
|
||||
|
||||
// Cursor<Account> accounts = accountMapper.selectCursorByQuery(asWrapper);
|
||||
|
||||
@ -59,14 +59,11 @@ public class MultiDataSourceTester {
|
||||
MessageCollector collector = new ConsoleMessageCollector();
|
||||
AuditManager.setMessageCollector(collector);
|
||||
|
||||
Db.tx(new Supplier<Boolean>() {
|
||||
@Override
|
||||
public Boolean get() {
|
||||
Db.selectAll(null, "tb_account");
|
||||
DataSourceKey.use("ds2");
|
||||
Db.selectAll(null, "tb_account");
|
||||
return true;
|
||||
}
|
||||
Db.tx(() -> {
|
||||
Db.selectAll(null, "tb_account");
|
||||
DataSourceKey.use("ds2");
|
||||
Db.selectAll(null, "tb_account");
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
|
||||
@ -47,12 +47,7 @@ public class TenantManagerTester {
|
||||
AuditManager.setMessageCollector(new ConsoleMessageCollector());
|
||||
|
||||
//配置 tenantFactory
|
||||
TenantManager.setTenantFactory(new TenantFactory() {
|
||||
@Override
|
||||
public Object[] getTenantIds() {
|
||||
return new Object[]{1};
|
||||
}
|
||||
});
|
||||
TenantManager.setTenantFactory(() -> new Object[]{1});
|
||||
|
||||
TenantAccountMapper mapper = MybatisFlexBootstrap.getInstance().getMapper(TenantAccountMapper.class);
|
||||
List<TenantAccount> tenantAccounts = TenantManager.withoutTenantCondition(mapper::selectAll);
|
||||
|
||||
@ -50,12 +50,9 @@ public class MyConfigurationCustomizer implements ConfigurationCustomizer, MyBat
|
||||
@Override
|
||||
public void customize(FlexGlobalConfig globalConfig) {
|
||||
|
||||
DataSourceDecipher decipher = new DataSourceDecipher() {
|
||||
@Override
|
||||
public String decrypt(DataSourceProperty property, String value) {
|
||||
System.out.println(">>>>>> decipher.decrypt");
|
||||
return value;
|
||||
}
|
||||
DataSourceDecipher decipher = (property, value) -> {
|
||||
System.out.println(">>>>>> decipher.decrypt");
|
||||
return value;
|
||||
};
|
||||
DataSourceManager.setDecipher(decipher);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user