/** * 查询符合条件组合的记录数 * * @param matches * @return 2013-8-26 下午3:04:02 created by wangchongjie */ public int count(List<Match> matches) { Query query = queryGenerater.getCountQuery(matches); // 执行操作 String sql = query.getSql(); return countBySQL(sql, query.getParams()); }
public ENTITY create(ENTITY entity, InsertOption option) { // 如果空值则返回 if (entity == null) { recordLog(" param entity is null!"); return null; } // 如果需要DAO层代理生成新ID if (sequenceName != null) { entity.setId(getNextId()); } // 判断是否为自动设置的ID KEY key = entity.getId(); boolean autoGeneratedKey = (Number.class.isAssignableFrom(orMapping.getKeyClass()) && (key == null || (Long.parseLong(key.toString()) == 0))); // 获取insert语句 @SuppressWarnings("unchecked") Query query = queryGenerater.getCreateQuery(autoGeneratedKey, false, option, entity); final String sql = query.getSql(); final List<Object> params = query.getParams(); // 如果非自动生成key则返回 if (!autoGeneratedKey) { int rowCount = executeSQL(sql, params); return rowCount == 1 ? entity : null; // 如果自动生成key,生成KeyHolder } else { return createAndFetchKey(entity, sql, params); } }
public boolean update(ENTITY entity) { if (entity == null) { recordLog(" param entity is null!"); return false; } Query query = queryGenerater.getUpdateQuery(entity); return executeSQL(query.getSql(), query.getParams()) == 1; }
public <T> T findValue(String column, Class<T> tClass, Match... matches) { Query query = queryGenerater.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); } }
/** * 根据查询条件获取结果集列表 * * @param matches * @param order * @param offset * @param limit * @return */ public List<ENTITY> find(List<Match> matches, List<Order> order, int offset, int limit) { // FIXME Query operate = queryGenerater.getSelectQuery( matches, (order == null) ? null : order.toArray(new Order[order.size()])); String sql = operate.getSql(); List<Object> params = operate.getParams(); if (offset != 0 || limit != 0) { sql = sql + " limit ?, ?"; params.add(offset); params.add(limit); } // 执行操作 return findBySQL(sql, operate.getParams()); }
public int create(List<ENTITY> entities, InsertOption option) { if (entities == null || entities.size() == 0) { recordLog(" param entities is empty!"); return 0; } // 如果需要DAO层设置ID if (sequenceName != null) { for (ENTITY e : entities) { e.setId(getNextId()); } } // 以第一个元素确定是否自动生成key KEY key = entities.get(0).getId(); // boolean autoGenerateKey = key == null; // 获取insert语句 Query query = queryGenerater.getCreateQuery(entities, key == null, false, option); // FIXME 先临时这样改下 int size = query.getParams().size(); int paramPerEntity = size / entities.size(); int i = 0; for (ENTITY entity : entities) { createAndFetchKey(entity, query.getSql(), query.getParams().subList(i, i += paramPerEntity)); } return entities.size(); // if(!autoGenerateKey){ // // 执行批量插入操作 // executeSQL(query.getSql(), query.getParams()); // return entities.size(); // }else{ // int size = query.getParams().size(); // int paramPerEntity = size / entities.size(); // int i = 0; // for(ENTITY entity : entities){ // createAndFetchKey(entity, query.getSql(), query.getParams().subList(i, i += // paramPerEntity)); // } // return entities.size(); // } }
public int createBatchQuick(List<ENTITY> entities) { if (entities == null || entities.size() == 0) { recordLog(" param entities is empty!"); return 0; } // 如果需要DAO层设置ID if (sequenceName != null) { for (ENTITY e : entities) { e.setId(getNextId()); } } // 以第一个元素确定是否自动生成key KEY key = entities.get(0).getId(); // 获取insert语句 Query query = queryGenerater.getCreateQuery(entities, key == null, true, null); // 执行批量插入操作 executeSQL(query.getSql(), query.getParams()); return entities.size(); }
public int update(List<Modify> modifies, Match... matches) { Query query = queryGenerater.getUpdateQuery(modifies, matches); return executeSQL(query.getSql(), query.getParams()); }
public <T> List<T> findOneColumn(String column, Class<T> tClass, Match... matches) { Query query = queryGenerater.getMiniSelectQuery(Arrays.asList(column), matches); return findOneColumn(query.getSql(), query.getParams(), 1, tClass); }
/** * 删除符合条件组合的对象列表 * * @param params 查询参数,该参数为"字段名,字段值,字段名,字段值..."的排列 * @return */ public int delete(Match... matches) { Query query = queryGenerater.getDeleteQuery(matches); // 执行操作 return executeSQL(query.getSql(), query.getParams()); }
/** * 根据条件查询若干个字段,加载到ENTITY里 * * @param matches * @param columns * @return */ protected List<ENTITY> findColumns(List<Match> matches, String[] columns) { Query query = queryGenerater.getMiniSelectQuery(Arrays.asList(columns), matches); return findBySQL(query.getSql(), query.getParams()); }