本文整理了Java中org.sql2o.Query.withParams
方法的一些代码示例,展示了Query.withParams
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.withParams
方法的具体详情如下:
包路径:org.sql2o.Query
类名称:Query
方法名:withParams
暂无
代码示例来源:origin: aaberg/sql2o
public Query createQueryWithParams(String queryText, Object... paramValues){
// due to #146, creating a query will not create a statement anymore;
// the PreparedStatement will only be created once the query needs to be executed
// => there is no need to handle the query closing here anymore since there is nothing to close
return createQuery(queryText)
.withParams(paramValues);
}
代码示例来源:origin: biezhi/anima
public Query createQueryWithParams(String queryText, Object... paramValues) {
// due to #146, creating a query will not create a statement anymore;
// the PreparedStatement will only be created once the query needs to be executed
// => there is no need to handle the query closing here anymore since there is nothing to close
return createQuery(queryText)
.withParams(paramValues);
}
代码示例来源:origin: org.sql2o/sql2o
public Query createQueryWithParams(String queryText, Object... paramValues){
// due to #146, creating a query will not create a statement anymore;
// the PreparedStatement will only be created once the query needs to be executed
// => there is no need to handle the query closing here anymore since there is nothing to close
return createQuery(queryText)
.withParams(paramValues);
}
代码示例来源:origin: lets-blade/blade-jdbc
public long count(String sql, Object... args) {
int pos = 1;
while (sql.contains("?")) {
sql = sql.replaceFirst("\\?", ":p" + (pos++));
}
args = args == null ? new Object[]{} : args;
try (Connection con = getSql2o().open()) {
this.cleanParam();
log.debug(EXECUTE_SQL_PREFIX + " => {}", sql);
log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(args));
return con.createQuery(sql).withParams(args)
.executeAndFetchFirst(Long.class);
}
}
代码示例来源:origin: lets-blade/blade-jdbc
public <T> T query(Class<T> type, String sql, Object... args) {
int pos = 1;
while (sql.contains(SQL_QM)) {
sql = sql.replaceFirst("\\?", ":p" + (pos++));
}
try (Connection con = getSql2o().open()) {
log.debug(EXECUTE_SQL_PREFIX + " => {}", sql);
log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(args));
this.cleanParam();
Query query = con.createQuery(sql).withParams(args).throwOnMappingFailure(false);
QueryMeta queryMeta = SqlBuilder.buildFindAllSql(this, null);
if (queryMeta.hasColumnMapping()) {
queryMeta.getColumnMapping().forEach(query::addColumnMapping);
}
return query.executeAndFetchFirst(type);
}
}
代码示例来源:origin: lets-blade/blade-jdbc
public <T> List<T> queryAll(Class<T> type, String sql, Object... args) {
int pos = 1;
while (sql.contains(SQL_QM)) {
sql = sql.replaceFirst("\\?", ":p" + (pos++));
}
PageRow pageRow = Base.pageLocal.get();
sql = SqlBuilder.appendPageParams(sql, pageRow);
args = args == null ? new Object[]{} : args;
try (Connection con = getSql2o().open()) {
log.debug(EXECUTE_SQL_PREFIX + " => {}", sql);
log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(args));
this.cleanParam();
Query query = con.createQuery(sql).withParams(args).throwOnMappingFailure(false);
QueryMeta queryMeta = SqlBuilder.buildFindAllSql(this, null);
if (queryMeta.hasColumnMapping()) {
queryMeta.getColumnMapping().forEach(query::addColumnMapping);
}
return query.executeAndFetch(type);
}
}
代码示例来源:origin: lets-blade/blade-jdbc
private int invoke(QueryMeta queryMeta) {
log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql());
log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(queryMeta.getParams()));
Connection con = getConn();
Query query = con.createQuery(queryMeta.getSql()).withParams(queryMeta.getParams());
if (queryMeta.hasColumnMapping()) {
queryMeta.getColumnMapping().forEach(query::addColumnMapping);
}
int result = query.executeUpdate().getResult();
try {
if (null == Base.connectionThreadLocal.get() && !con.getJdbcConnection().getAutoCommit()) {
con.commit();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
代码示例来源:origin: lets-blade/blade-jdbc
private long count(boolean cleanParam) {
QueryMeta queryMeta = SqlBuilder.buildCountSql(this);
try (Connection con = getSql2o().open()) {
if (cleanParam) this.cleanParam();
log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql());
log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(queryMeta.getParams()));
return con.createQuery(queryMeta.getSql())
.withParams(queryMeta.getParams())
.executeAndFetchFirst(Long.class);
}
}
代码示例来源:origin: lets-blade/blade-jdbc
public <T extends ActiveRecord> T find() {
QueryMeta queryMeta = SqlBuilder.buildFindSql(this);
Class<T> type = (Class<T>) getClass();
try (Connection con = getSql2o().open()) {
this.cleanParam();
log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql());
log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(queryMeta.getParams()));
Query query = con.createQuery(queryMeta.getSql()).withParams(queryMeta.getParams()).throwOnMappingFailure(false);
if (queryMeta.hasColumnMapping()) {
queryMeta.getColumnMapping().forEach(query::addColumnMapping);
}
return query.executeAndFetchFirst(type);
}
}
代码示例来源:origin: biezhi/anima
/**
* Save a model
*
* @param model model instance
* @param <S>
* @return ResultKey
*/
public <S extends Model> ResultKey save(S model) {
List<Object> columnValues = AnimaUtils.toColumnValues(model, true);
String sql = this.buildInsertSQL(model, columnValues);
Connection conn = getConn();
try {
List<Object> params = columnValues.stream()
.filter(Objects::nonNull)
.collect(toList());
return new ResultKey(conn.createQuery(sql)
.withParams(params)
.executeUpdate()
.getKey());
} finally {
this.closeConn(conn);
this.clean(conn);
}
}
代码示例来源:origin: lets-blade/blade-jdbc
public <T> List<T> findAll(Class<T> type, Supplier<ConditionEnum>... conditions) {
QueryMeta queryMeta = SqlBuilder.buildFindAllSql(this, conditions);
try (Connection con = getSql2o().open()) {
log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql());
log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(queryMeta.getParams()));
this.cleanParam();
Query query = con.createQuery(queryMeta.getSql()).withParams(queryMeta.getParams()).throwOnMappingFailure(false);
if (queryMeta.hasColumnMapping()) {
queryMeta.getColumnMapping().forEach(query::addColumnMapping);
}
return query.executeAndFetch(type);
}
}
代码示例来源:origin: lets-blade/blade-jdbc
public <T> T find(Class<T> type, Serializable id) {
String sql = "SELECT * FROM " + getTableName() + " WHERE " + getPk() + " = :p1";
QueryMeta queryMeta = new QueryMeta();
SqlBuilder.mapping(queryMeta, this.getClass());
try (Connection con = getSql2o().open()) {
this.cleanParam();
log.debug(EXECUTE_SQL_PREFIX + " => {}", sql);
log.debug(PARAMETER_PREFIX + " => [{}]", id);
Query query = con.createQuery(sql).withParams(id).throwOnMappingFailure(false);
if (queryMeta.hasColumnMapping()) {
queryMeta.getColumnMapping().forEach(query::addColumnMapping);
}
return query.executeAndFetchFirst(type);
}
}
代码示例来源:origin: biezhi/anima
/**
* Execute sql statement
*
* @param sql sql statement
* @param params params
* @return affect the number of rows
*/
public int execute(String sql, Object... params) {
Connection conn = getConn();
try {
return conn.createQuery(sql)
.withParams(params)
.executeUpdate()
.getResult();
} finally {
this.closeConn(conn);
this.clean(conn);
}
}
代码示例来源:origin: biezhi/anima
/**
* Querying a List<Map>
*
* @param sql sql statement
* @param params params
* @return List<Map>
*/
public List<Map<String, Object>> queryListMap(String sql, Object[] params) {
Connection conn = getConn();
try {
return conn.createQuery(sql)
.withParams(params)
.setAutoDeriveColumnNames(true)
.throwOnMappingFailure(false)
.executeAndFetchTable()
.asList();
} finally {
this.closeConn(conn);
this.clean(null);
}
}
代码示例来源:origin: biezhi/anima
/**
* Querying a list
*
* @param type model type
* @param sql sql statement
* @param params params
* @param <S>
* @return List<S>
*/
public <S> List<S> queryList(Class<S> type, String sql, Object[] params) {
Connection conn = getConn();
try {
return conn.createQuery(sql)
.withParams(params)
.setColumnMappings(computeModelColumnMappings(type))
.throwOnMappingFailure(false)
.executeAndFetch(type);
} finally {
this.closeConn(conn);
this.clean(null);
}
}
代码示例来源:origin: biezhi/anima
/**
* Querying a model
*
* @param type model type
* @param sql sql statement
* @param params params
* @param <S>
* @return S
*/
public <S> S queryOne(Class<S> type, String sql, Object[] params) {
Connection conn = getConn();
try {
Query query = conn.createQuery(sql)
.withParams(params)
.setAutoDeriveColumnNames(true)
.throwOnMappingFailure(false);
return ifReturn(AnimaUtils.isBasicType(type),
() -> query.executeScalar(type),
() -> query.executeAndFetchFirst(type));
} finally {
this.closeConn(conn);
this.clean(null);
}
}
代码示例来源:origin: biezhi/anima
.withParams(params)
.executeAndFetchFirst(Long.class);
String pageSQL = this.buildPageSQL(sql, pageRow);
List<T> list = conn.createQuery(pageSQL)
.withParams(params)
.setAutoDeriveColumnNames(true)
.throwOnMappingFailure(false)
内容来源于网络,如有侵权,请联系作者删除!