add query method

This commit is contained in:
Looly
2020-01-11 19:37:36 +08:00
parent 33c3d12cbb
commit cc74ae34a2
5 changed files with 30 additions and 3 deletions

View File

@@ -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;
@@ -154,6 +155,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>
* 语句包括 插入、更新、删除

View File

@@ -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);

View File

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