Browse Source

add query method

Looly 6 years ago
parent
commit
cc74ae34a2

+ 2 - 0
CHANGELOG.md

@@ -18,6 +18,8 @@
 * 【core  】    QrCodeUtil增加背景透明支持(pr#89@Gitee)
 * 【core  】    增加农历ChineseDate(pr#90@Gitee)
 * 【core  】    ZipUtil增加zip方法写出到流(issue#I17SCT@Gitee)
+* 【db    】    Db.use().query的方法中增加Map参数接口(issue#709@Github)
+* 【db    】    getDialect使用数据源作为锁(issue#720@Github)
 
 ### Bug修复
 * 【core 】     修复NumberUtil.mul中null的结果错误问题(issue#I17Y4J@Gitee)

+ 2 - 0
hutool-crypto/src/main/java/cn/hutool/crypto/asymmetric/AsymmetricCrypto.java

@@ -45,6 +45,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
 	 * 
 	 * @param algorithm {@link SymmetricAlgorithm}
 	 */
+	@SuppressWarnings("RedundantCast")
 	public AsymmetricCrypto(AsymmetricAlgorithm algorithm) {
 		this(algorithm, (byte[]) null, (byte[]) null);
 	}
@@ -54,6 +55,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
 	 * 
 	 * @param algorithm 算法
 	 */
+	@SuppressWarnings("RedundantCast")
 	public AsymmetricCrypto(String algorithm) {
 		this(algorithm, (byte[]) null, (byte[]) null);
 	}

+ 22 - 0
hutool-db/src/main/java/cn/hutool/db/AbstractDb.java

@@ -5,6 +5,7 @@ import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 import javax.sql.DataSource;
 
@@ -155,6 +156,27 @@ public abstract class AbstractDb implements Serializable {
 	}
 
 	/**
+	 * 支持占位符的查询,例如:select * from table where field1=:name1
+	 *
+	 * @param <T>    结果集需要处理的对象类型
+	 * @param sql    查询语句,使用参数名占位符,例如:name
+	 * @param rsh    结果集处理对象
+	 * @param paramMap 参数
+	 * @return 结果对象
+	 * @throws SQLException SQL执行异常
+	 * @since 5.1.1
+	 */
+	public <T> T query(String sql, RsHandler<T> rsh, Map<String, Object> paramMap) throws SQLException {
+		Connection conn = null;
+		try {
+			conn = this.getConnection();
+			return SqlExecutor.query(conn, sql, rsh, paramMap);
+		} finally {
+			this.closeConnection(conn);
+		}
+	}
+
+	/**
 	 * 执行非查询语句<br>
 	 * 语句包括 插入、更新、删除
 	 *

+ 3 - 2
hutool-db/src/main/java/cn/hutool/db/dialect/DialectFactory.java

@@ -55,7 +55,6 @@ public class DialectFactory {
 	public final static String DRIVER_DM7 = "dm.jdbc.driver.DmDriver";
 	
 	private static Map<DataSource, Dialect> dialectPool = new ConcurrentHashMap<>();
-	private static final Object lock = new Object();
 
 	private DialectFactory() {
 	}
@@ -153,7 +152,9 @@ public class DialectFactory {
 	public static Dialect getDialect(DataSource ds) {
 		Dialect dialect = dialectPool.get(ds);
 		if(null == dialect) {
-			synchronized (lock) {
+			// 数据源作为锁的意义在于:不同数据源不会导致阻塞,相同数据源获取方言时可保证互斥
+			//noinspection SynchronizationOnLocalVariableOrMethodParameter
+			synchronized (ds) {
 				dialect = dialectPool.get(ds);
 				if(null == dialect) {
 					dialect = newDialect(ds);

+ 1 - 1
hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java

@@ -220,7 +220,7 @@ public class SqlExecutor {
 	}
 
 	/**
-	 * 执行查询语句<br>
+	 * 执行查询语句,例如:select * from table where field1=:name1 <br>
 	 * 此方法不会关闭Connection
 	 * 
 	 * @param <T> 处理结果类型