mirror of https://github.com/elunez/eladmin
代码优化
parent
aa2da4e222
commit
ebdf7799ca
|
@ -13,9 +13,9 @@ import java.util.*;
|
||||||
* @date 2019-6-4 14:59:48
|
* @date 2019-6-4 14:59:48
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@SuppressWarnings({"unchecked","all"})
|
||||||
public class QueryHelp {
|
public class QueryHelp {
|
||||||
|
|
||||||
@SuppressWarnings("all")
|
|
||||||
public static <R, Q> Predicate getPredicate(Root<R> root, Q query, CriteriaBuilder cb) {
|
public static <R, Q> Predicate getPredicate(Root<R> root, Q query, CriteriaBuilder cb) {
|
||||||
List<Predicate> list = new ArrayList<>();
|
List<Predicate> list = new ArrayList<>();
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ import java.util.Map;
|
||||||
* @date 2019-01-02
|
* @date 2019-01-02
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings({"unchecked","all"})
|
||||||
public class GeneratorServiceImpl implements GeneratorService {
|
public class GeneratorServiceImpl implements GeneratorService {
|
||||||
|
|
||||||
@PersistenceContext
|
@PersistenceContext
|
||||||
|
|
|
@ -18,43 +18,60 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package me.zhengjie.modules.mnt.util;
|
package me.zhengjie.modules.mnt.util;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author /
|
||||||
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@SuppressWarnings({"unchecked","all"})
|
||||||
public enum DataTypeEnum {
|
public enum DataTypeEnum {
|
||||||
|
|
||||||
|
/** mysql */
|
||||||
MYSQL("mysql", "mysql", "com.mysql.jdbc.Driver", "`", "`", "'", "'"),
|
MYSQL("mysql", "mysql", "com.mysql.jdbc.Driver", "`", "`", "'", "'"),
|
||||||
|
|
||||||
|
/** oracle */
|
||||||
ORACLE("oracle", "oracle", "oracle.jdbc.driver.OracleDriver", "\"", "\"", "\"", "\""),
|
ORACLE("oracle", "oracle", "oracle.jdbc.driver.OracleDriver", "\"", "\"", "\"", "\""),
|
||||||
|
|
||||||
|
/** sql server */
|
||||||
SQLSERVER("sqlserver", "sqlserver", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "\"", "\"", "\"", "\""),
|
SQLSERVER("sqlserver", "sqlserver", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "\"", "\"", "\"", "\""),
|
||||||
|
|
||||||
|
/** h2 */
|
||||||
H2("h2", "h2", "org.h2.Driver", "`", "`", "\"", "\""),
|
H2("h2", "h2", "org.h2.Driver", "`", "`", "\"", "\""),
|
||||||
|
|
||||||
|
/** phoenix */
|
||||||
PHOENIX("phoenix", "hbase phoenix", "org.apache.phoenix.jdbc.PhoenixDriver", "", "", "\"", "\""),
|
PHOENIX("phoenix", "hbase phoenix", "org.apache.phoenix.jdbc.PhoenixDriver", "", "", "\"", "\""),
|
||||||
|
|
||||||
|
/** mongo */
|
||||||
MONGODB("mongo", "mongodb", "mongodb.jdbc.MongoDriver", "`", "`", "\"", "\""),
|
MONGODB("mongo", "mongodb", "mongodb.jdbc.MongoDriver", "`", "`", "\"", "\""),
|
||||||
|
|
||||||
|
/** sql4es */
|
||||||
ELASTICSEARCH("sql4es", "elasticsearch", "nl.anchormen.sql4es.jdbc.ESDriver", "", "", "'", "'"),
|
ELASTICSEARCH("sql4es", "elasticsearch", "nl.anchormen.sql4es.jdbc.ESDriver", "", "", "'", "'"),
|
||||||
|
|
||||||
|
/** presto */
|
||||||
PRESTO("presto", "presto", "com.facebook.presto.jdbc.PrestoDriver", "", "", "\"", "\""),
|
PRESTO("presto", "presto", "com.facebook.presto.jdbc.PrestoDriver", "", "", "\"", "\""),
|
||||||
|
|
||||||
|
/** moonbox */
|
||||||
MOONBOX("moonbox", "moonbox", "moonbox.jdbc.MbDriver", "`", "`", "`", "`"),
|
MOONBOX("moonbox", "moonbox", "moonbox.jdbc.MbDriver", "`", "`", "`", "`"),
|
||||||
|
|
||||||
|
/** cassandra */
|
||||||
CASSANDRA("cassandra", "cassandra", "com.github.adejanovski.cassandra.jdbc.CassandraDriver", "", "", "'", "'"),
|
CASSANDRA("cassandra", "cassandra", "com.github.adejanovski.cassandra.jdbc.CassandraDriver", "", "", "'", "'"),
|
||||||
|
|
||||||
|
/** click house */
|
||||||
CLICKHOUSE("clickhouse", "clickhouse", "ru.yandex.clickhouse.ClickHouseDriver", "", "", "\"", "\""),
|
CLICKHOUSE("clickhouse", "clickhouse", "ru.yandex.clickhouse.ClickHouseDriver", "", "", "\"", "\""),
|
||||||
|
|
||||||
|
/** kylin */
|
||||||
KYLIN("kylin", "kylin", "org.apache.kylin.jdbc.Driver", "\"", "\"", "\"", "\""),
|
KYLIN("kylin", "kylin", "org.apache.kylin.jdbc.Driver", "\"", "\"", "\"", "\""),
|
||||||
|
|
||||||
|
/** vertica */
|
||||||
VERTICA("vertica", "vertica", "com.vertica.jdbc.Driver", "", "", "'", "'"),
|
VERTICA("vertica", "vertica", "com.vertica.jdbc.Driver", "", "", "'", "'"),
|
||||||
|
|
||||||
|
/** sap */
|
||||||
HANA("sap", "sap hana", "com.sap.db.jdbc.Driver", "", "", "'", "'"),
|
HANA("sap", "sap hana", "com.sap.db.jdbc.Driver", "", "", "'", "'"),
|
||||||
|
|
||||||
|
/** impala */
|
||||||
IMPALA("impala", "impala", "com.cloudera.impala.jdbc41.Driver", "", "", "'", "'");
|
IMPALA("impala", "impala", "com.cloudera.impala.jdbc41.Driver", "", "", "'", "'");
|
||||||
|
|
||||||
|
|
||||||
private String feature;
|
private String feature;
|
||||||
private String desc;
|
private String desc;
|
||||||
private String driver;
|
private String driver;
|
||||||
|
@ -63,7 +80,7 @@ public enum DataTypeEnum {
|
||||||
private String aliasPrefix;
|
private String aliasPrefix;
|
||||||
private String aliasSuffix;
|
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) {
|
DataTypeEnum(String feature, String desc, String driver, String keywordPrefix, String keywordSuffix, String aliasPrefix, String aliasSuffix) {
|
||||||
this.feature = feature;
|
this.feature = feature;
|
||||||
|
@ -78,7 +95,7 @@ public enum DataTypeEnum {
|
||||||
public static DataTypeEnum urlOf(String jdbcUrl) {
|
public static DataTypeEnum urlOf(String jdbcUrl) {
|
||||||
String url = jdbcUrl.toLowerCase().trim();
|
String url = jdbcUrl.toLowerCase().trim();
|
||||||
for (DataTypeEnum dataTypeEnum : values()) {
|
for (DataTypeEnum dataTypeEnum : values()) {
|
||||||
if (url.startsWith(jdbcUrlPrefix + dataTypeEnum.feature)) {
|
if (url.startsWith(JDBC_URL_PREFIX + dataTypeEnum.feature)) {
|
||||||
try {
|
try {
|
||||||
Class<?> aClass = Class.forName(dataTypeEnum.getDriver());
|
Class<?> aClass = Class.forName(dataTypeEnum.getDriver());
|
||||||
if (null == aClass) {
|
if (null == aClass) {
|
||||||
|
|
|
@ -5,14 +5,17 @@ import com.alibaba.druid.pool.DruidDataSource;
|
||||||
import com.alibaba.druid.util.StringUtils;
|
import com.alibaba.druid.util.StringUtils;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author /
|
||||||
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SqlUtils {
|
public class SqlUtils {
|
||||||
|
|
||||||
|
@ -36,17 +39,17 @@ public class SqlUtils {
|
||||||
/**
|
/**
|
||||||
* 获取数据源
|
* 获取数据源
|
||||||
*
|
*
|
||||||
* @param jdbcUrl
|
* @param jdbcUrl /
|
||||||
* @param userName
|
* @param userName /
|
||||||
* @param password
|
* @param password /
|
||||||
* @return
|
* @return DataSource
|
||||||
*/
|
*/
|
||||||
private static DataSource getDataSource(String jdbcUrl, String userName, String password) {
|
private static DataSource getDataSource(String jdbcUrl, String userName, String password) {
|
||||||
String key = getKey(jdbcUrl, userName, password);
|
String key = getKey(jdbcUrl, userName, password);
|
||||||
if (!map.containsKey(key) || null == map.get(key)) {
|
if (!map.containsKey(key) || null == map.get(key)) {
|
||||||
DruidDataSource druidDataSource = new DruidDataSource();
|
DruidDataSource druidDataSource = new DruidDataSource();
|
||||||
|
|
||||||
String className = null;
|
String className;
|
||||||
try {
|
try {
|
||||||
className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName();
|
className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
|
@ -98,11 +101,10 @@ public class SqlUtils {
|
||||||
Connection connection = null;
|
Connection connection = null;
|
||||||
try {
|
try {
|
||||||
connection = dataSource.getConnection();
|
connection = dataSource.getConnection();
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {}
|
||||||
connection = null;
|
|
||||||
}
|
|
||||||
try {
|
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!");
|
log.info("connection is closed or invalid, retry get connection!");
|
||||||
connection = dataSource.getConnection();
|
connection = dataSource.getConnection();
|
||||||
}
|
}
|
||||||
|
@ -117,10 +119,9 @@ public class SqlUtils {
|
||||||
if (null != connection) {
|
if (null != connection) {
|
||||||
try {
|
try {
|
||||||
connection.close();
|
connection.close();
|
||||||
connection = null;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
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) {
|
if (rs != null) {
|
||||||
try {
|
try {
|
||||||
rs.close();
|
rs.close();
|
||||||
rs = null;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ public class SqlUtils {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.info("Get connection failed:", e.getMessage());
|
log.info("Get connection failed:" + e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
releaseConnection(connection);
|
releaseConnection(connection);
|
||||||
}
|
}
|
||||||
|
@ -168,9 +168,8 @@ public class SqlUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量执行sql
|
* 批量执行sql
|
||||||
* @param connection
|
* @param connection /
|
||||||
* @param sqlList
|
* @param sqlList /
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static void batchExecute(Connection connection, List<String> sqlList) throws SQLException {
|
public static void batchExecute(Connection connection, List<String> sqlList) throws SQLException {
|
||||||
Statement st = connection.createStatement();
|
Statement st = connection.createStatement();
|
||||||
|
@ -185,18 +184,16 @@ public class SqlUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将文件中的sql语句以;为单位读取到列表中
|
* 将文件中的sql语句以;为单位读取到列表中
|
||||||
* @param sqlFile
|
* @param sqlFile /
|
||||||
* @return
|
* @return /
|
||||||
* @throws Exception
|
* @throws Exception e
|
||||||
*/
|
*/
|
||||||
private static List<String> readSqlList(File sqlFile) throws Exception {
|
private static List<String> readSqlList(File sqlFile) throws Exception {
|
||||||
List<String> sqlList = Lists.newArrayList();
|
List<String> sqlList = Lists.newArrayList();
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
BufferedReader reader = null;
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||||||
try {
|
new FileInputStream(sqlFile), StandardCharsets.UTF_8))) {
|
||||||
reader = new BufferedReader(new InputStreamReader(
|
String tmp;
|
||||||
new FileInputStream(sqlFile), "UTF-8"));
|
|
||||||
String tmp = null;
|
|
||||||
while ((tmp = reader.readLine()) != null) {
|
while ((tmp = reader.readLine()) != null) {
|
||||||
log.info("line:{}", tmp);
|
log.info("line:{}", tmp);
|
||||||
if (tmp.endsWith(";")) {
|
if (tmp.endsWith(";")) {
|
||||||
|
@ -210,11 +207,6 @@ public class SqlUtils {
|
||||||
if (!"".endsWith(sb.toString().trim())) {
|
if (!"".endsWith(sb.toString().trim())) {
|
||||||
sqlList.add(sb.toString());
|
sqlList.add(sb.toString());
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
reader.close();
|
|
||||||
} catch (IOException e1) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sqlList;
|
return sqlList;
|
||||||
|
|
|
@ -39,7 +39,7 @@ public class DictDetailServiceImpl implements DictDetailService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable
|
@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);
|
Page<DictDetail> page = dictDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
|
||||||
return PageUtil.toPage(page.map(dictDetailMapper::toDto));
|
return PageUtil.toPage(page.map(dictDetailMapper::toDto));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue