Search in sources :

Example 6 with Query

use of com.baidu.unbiz.common.genericdao.operator.Query in project disconf by knightliao.

the class QueryGenerator method getSelectQuery.

public Query getSelectQuery(Collection<Match> matches, Order... orders) {
    Query query = getSelectQuery(matches);
    if (orders == null || orders.length == 0) {
        return query;
    }
    // 加入排序
    StringBuilder sb = new StringBuilder(query.getSql());
    sb.append(" order by ");
    for (Order entry : orders) {
        sb.append(entry.getColumn()).append(entry.isAsc() ? " asc" : " desc").append(',');
    }
    sb.deleteCharAt(sb.length() - 1);
    query.setSql(sb.toString());
    return query;
}
Also used : Order(com.baidu.unbiz.common.genericdao.operator.Order) Query(com.baidu.unbiz.common.genericdao.operator.Query)

Example 7 with Query

use of com.baidu.unbiz.common.genericdao.operator.Query in project disconf by knightliao.

the class GenericDao method findValue.

public <T> T findValue(String column, Class<T> tClass, Match... matches) {
    Query query = queryGenerator.getMiniSelectQuery(Arrays.asList(column), matches);
    List<T> ts = findOneColumn(query.getSql() + " limit 1", query.getParams(), 1, tClass);
    if (CollectionUtils.isEmpty(ts)) {
        return null;
    } else {
        return ts.get(0);
    }
}
Also used : Query(com.baidu.unbiz.common.genericdao.operator.Query)

Example 8 with Query

use of com.baidu.unbiz.common.genericdao.operator.Query in project disconf by knightliao.

the class GenericDao method count.

/**
     * 查询符合条件组合的记录数
     *
     * @param matches
     *
     * @return 2013-8-26 下午3:04:02 created by wangchongjie
     */
public int count(List<Match> matches) {
    Query query = queryGenerator.getCountQuery(matches);
    // 执行操作
    String sql = query.getSql();
    return countBySQL(sql, query.getParams());
}
Also used : Query(com.baidu.unbiz.common.genericdao.operator.Query)

Example 9 with Query

use of com.baidu.unbiz.common.genericdao.operator.Query in project disconf by knightliao.

the class GenericDao method update.

/**
     * 批量更新
     *
     * @param entities
     *
     * @return
     */
public int update(List<ENTITY> entities) {
    if (entities == null || entities.size() == 0) {
        recordLog(" param entity is null!");
        return 0;
    }
    final List<Query> queries = queryGenerator.getUpdateQuery(entities);
    String sql = queries.get(0).getSql();
    jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

        public void setValues(PreparedStatement stmt, int index) throws SQLException {
            int i = 1;
            List<Object> params = queries.get(index).getParams();
            for (Object o : params) {
                stmt.setObject(i++, o);
            }
        }

        public int getBatchSize() {
            return queries.size();
        }
    });
    return entities.size();
}
Also used : Query(com.baidu.unbiz.common.genericdao.operator.Query) SQLException(java.sql.SQLException) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) BaseObject(com.github.knightliao.apollo.db.bo.BaseObject)

Example 10 with Query

use of com.baidu.unbiz.common.genericdao.operator.Query in project disconf by knightliao.

the class QueryGenerator method getPageQuery.

public Query getPageQuery(int curPage, int pageSize, Collection<Match> matches, Order... orders) {
    Query query = getSelectQuery(matches, orders);
    if (curPage < 0 || pageSize <= 0) {
        return query;
    }
    StringBuilder sb = new StringBuilder(query.getSql());
    List<Object> params = query.getParams();
    // 设置limit
    int offset = (curPage) * pageSize;
    int limit = pageSize;
    sb.append(" limit ?, ?");
    params.add(offset);
    params.add(limit);
    query.setSql(sb.toString());
    return query;
}
Also used : Query(com.baidu.unbiz.common.genericdao.operator.Query) BaseObject(com.github.knightliao.apollo.db.bo.BaseObject)

Aggregations

Query (com.baidu.unbiz.common.genericdao.operator.Query)11 BaseObject (com.github.knightliao.apollo.db.bo.BaseObject)5 ArrayList (java.util.ArrayList)2 Match (com.baidu.unbiz.common.genericdao.operator.Match)1 Modify (com.baidu.unbiz.common.genericdao.operator.Modify)1 Order (com.baidu.unbiz.common.genericdao.operator.Order)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 BatchPreparedStatementSetter (org.springframework.jdbc.core.BatchPreparedStatementSetter)1