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