All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.featherfly.component.sorm.operate.QueryOperate Maven / Gradle / Ivy

There is a newer version: 1.3.9
Show newest version
package cn.featherfly.component.sorm.operate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;

import cn.featherfly.common.db.JdbcException;
import cn.featherfly.common.db.JdbcUtils;
import cn.featherfly.common.db.builder.ConditionBuilder;
import cn.featherfly.common.lang.CollectionUtils;
import cn.featherfly.common.lang.LangUtils;
import cn.featherfly.component.sorm.SimpleORMException;

/**
 * 

* 查询操作 *

* @param 对象类型 * @author 钟冀 * @since 1.0 * @version 1.0 */ public class QueryOperate extends AbstractQueryOperate{ /** * 使用给定数据源以及给定对象生成读取操作. * @param type 类 * @param jdbcTemplate jdbcTemplate */ public QueryOperate(Class type, JdbcTemplate jdbcTemplate) { super(type, jdbcTemplate); } /** * 使用给定数据源以及给定对象生成读取操作. * @param type 类 * @param jdbcTemplate jdbcTemplate * @param dataBase 具体库 */ public QueryOperate(Class type, JdbcTemplate jdbcTemplate, String dataBase) { super(type, jdbcTemplate, dataBase); } /** *

* 根据指定条件返回唯一结果. *

* @param conditionBuilder 查询条件构建器 * @return 唯一结果 */ public T unique(ConditionBuilder conditionBuilder) { conditionBuilder.setBuildWithWhere(false); return unique(conditionBuilder.build(), CollectionUtils.toArray(conditionBuilder.getParams(), Object.class)); } /** *

* 根据查询条件返回唯一对象. *

* @param condition 查询条件SQL(不包含where) * @param params 查询参数 * @return 指定ID的对象 */ public T unique(String condition, Object...params) { List list = list(condition, params); if (LangUtils.isNotEmpty(list)) { if (list.size() > 1) { throw new JdbcException("invoke unique method, but multy object find"); } return list.get(0); } return null; } /** *

* 根据指定条件返回查询结果集合. *

* @param conditionBuilder 查询条件构建器 * @return 结果集合 */ public List list(ConditionBuilder conditionBuilder) { conditionBuilder.setBuildWithWhere(false); return list(conditionBuilder.build(), CollectionUtils.toArray(conditionBuilder.getParams(), Object.class)); } /** *

* 根据指定条件返回查询结果集合. *

* @param condition 查询条件SQL(不包含where) * @param params 查询参数 * @return 结果集合 */ public List list(final String condition, final Object...params) { if (LangUtils.isEmpty(condition)) { throw new SimpleORMException("#list.condition.null"); // throw new SimpleORMException("condition 不能为null"); } return jdbcTemplate.execute(new ConnectionCallback>() { @Override public List doInConnection(Connection conn) throws SQLException, DataAccessException { String executeSql = getSql(condition); logger.debug("execute sql: {}" , executeSql); PreparedStatement prep = conn.prepareStatement(executeSql); JdbcUtils.setParameters(prep, params); ResultSet res = prep.executeQuery(); List list = new ArrayList<>(); int index = 0; T t = null; while (res.next()) { t = mapRow(res, index); index++; list.add(t); } prep.close(); conn.close(); return list; } }); } // /** // * {@inheritDoc} // */ // @Override // protected void initSql() { // StringBuilder getSql = new StringBuilder(); // getSql.append(getSelectSql()) // .append(" where "); // this.sql = getSql.toString(); // LOGGER.debug("sql: {}" , this.sql); // } /** * {@inheritDoc} */ @Override protected String initCondition() { return ""; } /** *

* 获取带条件的sql *

* @param condition 条件 * @return 带条件的sql */ public String getSql(String condition) { if (LangUtils.isNotEmpty(condition)) { condition = " where " + condition; } else { condition = ""; } return sql + condition; } /** *

* 获取带条件的sql *

* @param builder 条件构建器 * @return 带条件的sql */ public String getSql(ConditionBuilder builder) { builder.setBuildWithWhere(true); return sql + " " + builder.build(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy