代码优化

pull/302/head
Elune 2019-12-10 19:17:35 +08:00
parent aa2da4e222
commit ebdf7799ca
5 changed files with 46 additions and 37 deletions

View File

@ -13,9 +13,9 @@ import java.util.*;
* @date 2019-6-4 14:59:48
*/
@Slf4j
@SuppressWarnings({"unchecked","all"})
public class QueryHelp {
@SuppressWarnings("all")
public static <R, Q> Predicate getPredicate(Root<R> root, Q query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<>();

View File

@ -32,7 +32,7 @@ import java.util.Map;
* @date 2019-01-02
*/
@Service
@SuppressWarnings("all")
@SuppressWarnings({"unchecked","all"})
public class GeneratorServiceImpl implements GeneratorService {
@PersistenceContext

View File

@ -18,43 +18,60 @@
*/
package me.zhengjie.modules.mnt.util;
import lombok.extern.slf4j.Slf4j;
/**
* @author /
*/
@Slf4j
@SuppressWarnings({"unchecked","all"})
public enum DataTypeEnum {
/** mysql */
MYSQL("mysql", "mysql", "com.mysql.jdbc.Driver", "`", "`", "'", "'"),
/** oracle */
ORACLE("oracle", "oracle", "oracle.jdbc.driver.OracleDriver", "\"", "\"", "\"", "\""),
/** sql server */
SQLSERVER("sqlserver", "sqlserver", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "\"", "\"", "\"", "\""),
/** h2 */
H2("h2", "h2", "org.h2.Driver", "`", "`", "\"", "\""),
/** phoenix */
PHOENIX("phoenix", "hbase phoenix", "org.apache.phoenix.jdbc.PhoenixDriver", "", "", "\"", "\""),
/** mongo */
MONGODB("mongo", "mongodb", "mongodb.jdbc.MongoDriver", "`", "`", "\"", "\""),
/** sql4es */
ELASTICSEARCH("sql4es", "elasticsearch", "nl.anchormen.sql4es.jdbc.ESDriver", "", "", "'", "'"),
/** presto */
PRESTO("presto", "presto", "com.facebook.presto.jdbc.PrestoDriver", "", "", "\"", "\""),
/** moonbox */
MOONBOX("moonbox", "moonbox", "moonbox.jdbc.MbDriver", "`", "`", "`", "`"),
/** cassandra */
CASSANDRA("cassandra", "cassandra", "com.github.adejanovski.cassandra.jdbc.CassandraDriver", "", "", "'", "'"),
/** click house */
CLICKHOUSE("clickhouse", "clickhouse", "ru.yandex.clickhouse.ClickHouseDriver", "", "", "\"", "\""),
/** kylin */
KYLIN("kylin", "kylin", "org.apache.kylin.jdbc.Driver", "\"", "\"", "\"", "\""),
/** vertica */
VERTICA("vertica", "vertica", "com.vertica.jdbc.Driver", "", "", "'", "'"),
/** sap */
HANA("sap", "sap hana", "com.sap.db.jdbc.Driver", "", "", "'", "'"),
/** impala */
IMPALA("impala", "impala", "com.cloudera.impala.jdbc41.Driver", "", "", "'", "'");
private String feature;
private String desc;
private String driver;
@ -63,7 +80,7 @@ public enum DataTypeEnum {
private String aliasPrefix;
private String aliasSuffix;
private static final String jdbcUrlPrefix = "jdbc:";
private static final String JDBC_URL_PREFIX = "jdbc:";
DataTypeEnum(String feature, String desc, String driver, String keywordPrefix, String keywordSuffix, String aliasPrefix, String aliasSuffix) {
this.feature = feature;
@ -78,7 +95,7 @@ public enum DataTypeEnum {
public static DataTypeEnum urlOf(String jdbcUrl) {
String url = jdbcUrl.toLowerCase().trim();
for (DataTypeEnum dataTypeEnum : values()) {
if (url.startsWith(jdbcUrlPrefix + dataTypeEnum.feature)) {
if (url.startsWith(JDBC_URL_PREFIX + dataTypeEnum.feature)) {
try {
Class<?> aClass = Class.forName(dataTypeEnum.getDriver());
if (null == aClass) {

View File

@ -5,14 +5,17 @@ import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.StringUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import javax.sql.DataSource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author /
*/
@Slf4j
public class SqlUtils {
@ -36,17 +39,17 @@ public class SqlUtils {
/**
*
*
* @param jdbcUrl
* @param userName
* @param password
* @return
* @param jdbcUrl /
* @param userName /
* @param password /
* @return DataSource
*/
private static DataSource getDataSource(String jdbcUrl, String userName, String password) {
String key = getKey(jdbcUrl, userName, password);
if (!map.containsKey(key) || null == map.get(key)) {
DruidDataSource druidDataSource = new DruidDataSource();
String className = null;
String className;
try {
className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName();
} catch (SQLException e) {
@ -98,11 +101,10 @@ public class SqlUtils {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (Exception e) {
connection = null;
}
} catch (Exception ignored) {}
try {
if (null == connection || connection.isClosed() || !connection.isValid(5)) {
int timeOut = 5;
if (null == connection || connection.isClosed() || !connection.isValid(timeOut)) {
log.info("connection is closed or invalid, retry get connection!");
connection = dataSource.getConnection();
}
@ -117,10 +119,9 @@ public class SqlUtils {
if (null != connection) {
try {
connection.close();
connection = null;
} catch (Exception e) {
e.printStackTrace();
log.error("connection close error", e.getMessage());
log.error("connection close error" + e.getMessage());
}
}
}
@ -130,7 +131,6 @@ public class SqlUtils {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception e) {
e.printStackTrace();
}
@ -145,7 +145,7 @@ public class SqlUtils {
return true;
}
} catch (Exception e) {
log.info("Get connection failed:", e.getMessage());
log.info("Get connection failed:" + e.getMessage());
} finally {
releaseConnection(connection);
}
@ -168,9 +168,8 @@ public class SqlUtils {
/**
* sql
* @param connection
* @param sqlList
* @return
* @param connection /
* @param sqlList /
*/
public static void batchExecute(Connection connection, List<String> sqlList) throws SQLException {
Statement st = connection.createStatement();
@ -185,18 +184,16 @@ public class SqlUtils {
/**
* sql
* @param sqlFile
* @return
* @throws Exception
* @param sqlFile /
* @return /
* @throws Exception e
*/
private static List<String> readSqlList(File sqlFile) throws Exception {
List<String> sqlList = Lists.newArrayList();
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(sqlFile), "UTF-8"));
String tmp = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(sqlFile), StandardCharsets.UTF_8))) {
String tmp;
while ((tmp = reader.readLine()) != null) {
log.info("line:{}", tmp);
if (tmp.endsWith(";")) {
@ -210,11 +207,6 @@ public class SqlUtils {
if (!"".endsWith(sb.toString().trim())) {
sqlList.add(sb.toString());
}
} finally {
try {
reader.close();
} catch (IOException e1) {
}
}
return sqlList;

View File

@ -39,7 +39,7 @@ public class DictDetailServiceImpl implements DictDetailService {
@Override
@Cacheable
public Map queryAll(DictDetailQueryCriteria criteria, Pageable pageable) {
public Map<String,Object> queryAll(DictDetailQueryCriteria criteria, Pageable pageable) {
Page<DictDetail> page = dictDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
return PageUtil.toPage(page.map(dictDetailMapper::toDto));
}