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

cn.featherfly.persistence.jdbc.JdbcPersistenceImpl Maven / Gradle / Ivy

There is a newer version: 1.4.2
Show newest version
package cn.featherfly.persistence.jdbc;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import cn.featherfly.common.db.PaginationWrapper;
import cn.featherfly.common.db.SqlUtils;
import cn.featherfly.common.db.builder.ConditionBuildUtils;
import cn.featherfly.common.db.builder.ConditionBuilder;
import cn.featherfly.common.db.data.Execution;
import cn.featherfly.common.db.dialect.Dialect;
import cn.featherfly.common.lang.ArrayUtils;
import cn.featherfly.common.lang.ClassUtils;
import cn.featherfly.common.lang.LangUtils;
import cn.featherfly.common.structure.page.Pagination;
import cn.featherfly.common.structure.page.PaginationResults;
import cn.featherfly.common.structure.page.SimplePagination;
import cn.featherfly.component.sorm.SimpleORMFactory;
import cn.featherfly.persistence.PersistenceObserver;
import cn.featherfly.persistence.PersistentException;

/**
 * 

* jdbc持久化实现类 *

* * @author 钟冀 */ public class JdbcPersistenceImpl extends PersistenceObserver implements JdbcPersistence { /** */ public JdbcPersistenceImpl() { } private static final Logger LOGGER = LoggerFactory .getLogger(JdbcPersistenceImpl.class); // ******************************************************************** // PersistenceObserver impl // ******************************************************************** /** * {@inheritDoc} */ @Override protected E doGet(Serializable id, Class type) { if (id != null) { return simpleORMFactory.getSimpleORM((Class) type).get(id); } return null; } /** * {@inheritDoc} */ @Override protected E doLoad(E entity) { if (entity != null) { return simpleORMFactory.getSimpleORM( ClassUtils.castGenericType(entity.getClass(), entity)) .load(entity); } return null; } /** *

* 删除. 具体的表名从传入对象类上的注解@Table的name属性获取 具体的条件以传入对象类属性上的标注了@Pk和@Column的name获取 *

* * @param * 对象类型 * @param entity * 对象 */ @Override protected void doDelete(E entity) { if (entity != null) { simpleORMFactory.getSimpleORM( ClassUtils.castGenericType(entity.getClass(), entity)) .delete(entity); } } /** * {@inheritDoc} */ @Override protected void doDeleteBatch(List entityList) { if (LangUtils.isEmpty(entityList)) { return; } for (Object entity : entityList) { doDelete(entity); } } /** * {@inheritDoc} */ @Override protected void doMerge(E entity) { if (entity != null) { simpleORMFactory.getSimpleORM( ClassUtils.castGenericType(entity.getClass(), entity)) .merge(entity); } } /** * {@inheritDoc} */ @Override protected void doMergeBatch(List entityList) { if (LangUtils.isEmpty(entityList)) { return; } for (Object entity : entityList) { doMerge(entity); } } /** *

* 插入,如果传入对象为null,忽略 将指定对象的值插入指定的表中, 具体的表名从传入对象类上的注解@Table的name属性获取 *

* * @param * 对象类型 * @param entity * 对象 */ @Override protected void doPersist(E entity) { if (entity != null) { simpleORMFactory.getSimpleORM( ClassUtils.castGenericType(entity.getClass(), entity)) .save(entity); } } /** * {@inheritDoc} */ @Override protected void doPersistBatch(List entityList) { if (LangUtils.isEmpty(entityList)) { return; } for (Object entity : entityList) { doPersist(entity); } } /** * {@inheritDoc} */ @Override protected void doSave(E entity) { if (entity != null) { simpleORMFactory.getSimpleORM( ClassUtils.castGenericType(entity.getClass(), entity)) .save(entity); } } /** * {@inheritDoc} */ @Override protected void doSaveBatch(List entityList) { if (LangUtils.isEmpty(entityList)) { return; } for (Object entity : entityList) { doSave(entity); } } /** * {@inheritDoc} */ @Override protected void doSaveOrMerge(E entity) { if (entity == null) { return; } E e = load(entity); if (e == null) { doSave(entity); } else { doMerge(entity); } } /** * {@inheritDoc} */ @Override protected void doSaveOrMergeBatch(List entityList) { if (LangUtils.isEmpty(entityList)) { return; } for (Object entity : entityList) { doSaveOrMerge(entity); } } /** * {@inheritDoc} */ @Override protected void doSaveOrUpdate(E entity) { if (entity == null) { return; } E e = load(entity); if (e == null) { doSave(entity); } else { doUpdate(entity); } } /** * {@inheritDoc} */ @Override protected void doSaveOrUpdateBatch(List entityList) { if (LangUtils.isEmpty(entityList)) { return; } for (Object entity : entityList) { doSaveOrUpdate(entity); } } /** *

* 更新. 具体的表名从传入对象类上的注解@Table的name属性获取 具体的条件以传入对象类属性上的标注了@PK和@Column的name获取 *

* * @param * 对象类型 * @param entity * 对象 */ @Override protected void doUpdate(E entity) { if (entity != null) { simpleORMFactory.getSimpleORM( ClassUtils.castGenericType(entity.getClass(), entity)) .update(entity); } } /** * {@inheritDoc} */ @Override protected void doUpdateBatch(List entityList) { if (LangUtils.isEmpty(entityList)) { return; } for (Object entity : entityList) { doUpdate(entity); } } // ******************************************************************** // JdbcPersistence impl // ******************************************************************** /** * {@inheritDoc} */ @Override public int execute(String sql, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.update(sql, params); } /** * {@inheritDoc} */ @Override public int execute(String sql, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.update(sql, params); } /** * {@inheritDoc} */ @Override public int execute(String sql, List params) { logger.debug("sql : {}", sql); return execute(sql, toArray(params)); } /** * {@inheritDoc} */ @Override public int insert(String tableName, Object object) { SimpleJdbcInsert jdbcInsert = getSimpleJdbcInsert(tableName); SqlParameterSource source = new BeanPropertySqlParameterSource(object); return jdbcInsert.execute(source); } /** * {@inheritDoc} */ @Override public Number insert(String tableName, Object object, String[] pkColumnNames) { SimpleJdbcInsert jdbcInsert = getSimpleJdbcInsert(tableName, pkColumnNames); SqlParameterSource source = new BeanPropertySqlParameterSource(object); return jdbcInsert.executeAndReturnKey(source); } /** * {@inheritDoc} */ @Override public Number insert(String tableName, Object object, List pkColumnNames) { if (pkColumnNames == null) { pkColumnNames = new ArrayList(); } return insert(tableName, object, pkColumnNames.toArray(new String[] {})); } /** * {@inheritDoc} */ @Override public int insert(String tableName, Map map) { SimpleJdbcInsert jdbcInsert = getSimpleJdbcInsert(tableName); SqlParameterSource source = new MapSqlParameterSource(map); return jdbcInsert.execute(source); } /** * {@inheritDoc} */ @Override public Number insert(String tableName, Map map, String[] pkColumnNames) { SimpleJdbcInsert jdbcInsert = getSimpleJdbcInsert(tableName, pkColumnNames); SqlParameterSource source = new MapSqlParameterSource(map); return jdbcInsert.executeAndReturnKey(source); } /** * {@inheritDoc} */ @Override public int update(String tableName, Map params, Map conditions) { StringBuilder sql = new StringBuilder(); Map newParams = new HashMap<>(); sql.append("update ").append(tableName); if (LangUtils.isNotEmpty(params)) { sql.append(" set "); int index = 0; for (String key : params.keySet()) { if (index > 0) { sql.append(", "); } sql.append(key).append(" = :").append(key); index++; } } Map newCondition = new HashMap<>(); if (LangUtils.isNotEmpty(conditions)) { sql.append(" where "); int index = 0; for (String key : conditions.keySet()) { if (index > 0) { sql.append(" and "); } String conditionNamedParam = "_condition_" + key; sql.append(key).append(" = :").append(conditionNamedParam); Object value = conditions.get(key); newCondition.put(conditionNamedParam, value); index++; } } newParams.putAll(params); newParams.putAll(newCondition); logger.debug("sql : {}", sql); logger.debug("params : {}", newParams); return namedParameterJdbcTemplate.update(sql.toString(), newParams); } /** * {@inheritDoc} */ @Override public int delete(String tableName, Map columnKeyAndValue) { StringBuilder sql = new StringBuilder(); sql.append("delete from ").append(tableName); if (LangUtils.isNotEmpty(columnKeyAndValue)) { sql.append(" where "); int index = 0; for (Entry entry : columnKeyAndValue.entrySet()) { if (index > 0) { sql.append(" and "); } sql.append(entry.getKey()).append(" = :") .append(entry.getKey()); index++; } } logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.update(sql.toString(), columnKeyAndValue); } /** * {@inheritDoc} */ @Override public Map find(String sql, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.queryForMap(sql, params); } /** * {@inheritDoc} */ @Override public Map find(String sql, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.queryForMap(sql, params); } /** * {@inheritDoc} */ @Override public Map find(String sql, List params) { return find(sql, toArray(params)); } /** * {@inheritDoc} */ @Override public E find(Class mappingType, ConditionBuilder builder) { builder.setDialect(dialect); return simpleORMFactory.getSimpleORM(mappingType).unique(builder); } /** * {@inheritDoc} */ @Override public E find(String sql, Class mappingType) { return find(sql, mappingType, new Object[] {}); } /** * {@inheritDoc} */ @Override public E find(String sql, Class mappingType, Map params) { logger.debug("sql : {}", sql); try { return namedParameterJdbcTemplate.queryForObject(sql, params, getParameterizedBeanPropertyRowMapper(mappingType)); } catch (EmptyResultDataAccessException e) { return null; } } /** * {@inheritDoc} */ @Override public E find(String sql, Class mappingType, Object[] params) { logger.debug("sql : {}", sql); try { return jdbcTemplate.queryForObject(sql, getParameterizedBeanPropertyRowMapper(mappingType), params); } catch (EmptyResultDataAccessException e) { return null; } } /** * {@inheritDoc} */ @Override public E find(String sql, Class mappingType, List params) { return find(sql, mappingType, toArray(params)); } /** * {@inheritDoc} */ @Override public E find(String sql, RowMapper rowMapper) { return find(sql, rowMapper, new Object[] {}); } /** * {@inheritDoc} */ @Override public E find(String sql, final RowMapper rowMapper, Map params) { logger.debug("sql : {}", sql); try { return namedParameterJdbcTemplate.queryForObject(sql, params, new org.springframework.jdbc.core.RowMapper() { @Override public E mapRow(ResultSet rs, int rowNum) throws SQLException { return rowMapper.mapRow(rs, rowNum); } }); } catch (EmptyResultDataAccessException e) { return null; } } /** * {@inheritDoc} */ @Override public E find(String sql, final RowMapper rowMapper, Object[] params) { logger.debug("sql : {}", sql); try { return jdbcTemplate.queryForObject(sql, new org.springframework.jdbc.core.RowMapper() { @Override public E mapRow(ResultSet rs, int rowNum) throws SQLException { return rowMapper.mapRow(rs, rowNum); } }, params); } catch (EmptyResultDataAccessException e) { return null; } } /** * {@inheritDoc} */ @Override public E find(String sql, RowMapper rowMapper, List params) { return find(sql, rowMapper, toArray(params)); } /** * {@inheritDoc} */ @Override public Integer countForInt(String tableName, ConditionBuilder conditionBuilder) { conditionBuilder.setBuildWithWhere(true); conditionBuilder.setDialect(dialect); String condition = conditionBuilder.build(); StringBuilder sql = new StringBuilder("select count(*) from"); ConditionBuildUtils.appendCondition(sql, tableName); ConditionBuildUtils.appendCondition(sql, condition); logger.debug("tableName : {}, condition : {}", tableName, condition); return findForInt(sql.toString(), conditionBuilder.getParams()); } /** * {@inheritDoc} */ @Override public Long countForLong(String tableName, ConditionBuilder conditionBuilder) { conditionBuilder.setBuildWithWhere(true); conditionBuilder.setDialect(dialect); String condition = conditionBuilder.build(); StringBuilder sql = new StringBuilder("select count(*) from"); ConditionBuildUtils.appendCondition(sql, tableName); ConditionBuildUtils.appendCondition(sql, condition); logger.debug("tableName : {}, condition : {}", tableName, condition); return findForLong(sql.toString(), conditionBuilder.getParams()); } /** * {@inheritDoc} */ @Override public T findForType(String sql, Class type, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.queryForObject(sql, params, type); } /** * {@inheritDoc} */ @Override public T findForType(String sql, Class type, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.queryForObject(sql, params, type); } /** * {@inheritDoc} */ @Override public T findForType(String sql, Class type, List params) { logger.debug("sql : {}", sql); return findForType(sql, type, toArray(params)); } /** * {@inheritDoc} */ @Override public List findForList(String sql, Class type, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.queryForList(sql, params, type); } /** * {@inheritDoc} */ @Override public List findForList(String sql, Class type, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.queryForList(sql, params, type); } /** * {@inheritDoc} */ @Override public List findForList(String sql, Class type, List params) { return findForList(sql, type, toArray(params)); } /** * {@inheritDoc} */ @Override public Integer findForInt(String sql, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.queryForObject(sql, params, Integer.class); } /** * {@inheritDoc} */ @Override public Integer findForInt(String sql, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.queryForObject(sql, params, Integer.class); } /** * {@inheritDoc} */ @Override public Integer findForInt(String sql, List params) { return findForInt(sql, toArray(params)); } /** * {@inheritDoc} */ @Override public Long findForLong(String sql, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.queryForObject(sql, params, Long.class); } /** * {@inheritDoc} */ @Override public Long findForLong(String sql, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.queryForObject(sql, params, Long.class); } /** * {@inheritDoc} */ @Override public Long findForLong(String sql, List params) { return findForLong(sql, toArray(params)); } /** * {@inheritDoc} */ @Override public BigDecimal findForBigDecimal(String sql, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.queryForObject(sql, params, BigDecimal.class); } /** * {@inheritDoc} */ @Override public BigDecimal findForBigDecimal(String sql, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.queryForObject(sql, params, BigDecimal.class); } /** * {@inheritDoc} */ @Override public BigDecimal findForBigDecimal(String sql, List params) { return findForBigDecimal(sql, toArray(params)); } /** * {@inheritDoc} */ @Override public String findForString(String sql, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.queryForObject(sql, params, String.class); } /** * {@inheritDoc} */ @Override public String findForString(String sql, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.queryForObject(sql, params, String.class); } /** * {@inheritDoc} */ @Override public String findForString(String sql, List params) { return findForString(sql, toArray(params)); } /** * {@inheritDoc} */ @Override public List> findList(String sql) { return findList(sql, new Object[] {}); } /** * {@inheritDoc} */ @Override public List> findList(String sql, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.queryForList(sql, params); } /** * {@inheritDoc} */ @Override public List> findList(String sql, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.queryForList(sql, params); } /** * {@inheritDoc} */ @Override public List> findList(String sql, List params) { return findList(sql, toArray(params)); } /** * {@inheritDoc} */ @Override public List> findList(String sql, Pagination pagination, Map params) { // Integer total = findForInt(SqlUtils.convertSelectToCount(sql), // params); // if (total != null) { // pagination.setTotal(total); // } PaginationWrapper> wrapper = new PaginationWrapper>( pagination); return findList(sql, wrapper.getStart(), wrapper.getLimit(), params); } /** * {@inheritDoc} */ @Override public List> findList(String sql, Pagination pagination, Object[] params) { // Integer total = findForInt(SqlUtils.convertSelectToCount(sql), // params); // if (total != null) { // pagination.setTotal(total); // } PaginationWrapper> wrapper = new PaginationWrapper>( pagination); return findList(sql, wrapper.getStart(), wrapper.getLimit(), params); } /** * {@inheritDoc} */ @Override public List> findList(String sql, Pagination pagination, List params) { // Integer total = findForInt(SqlUtils.convertSelectToCount(sql), // params); // if (total != null) { // pagination.setTotal(total); // } PaginationWrapper> wrapper = new PaginationWrapper>( pagination); return findList(sql, wrapper.getStart(), wrapper.getLimit(), params); } /** * {@inheritDoc} */ @Override public PaginationResults> findPage(String sql, Pagination pagination, Map params) { List> list = findList(sql, pagination, params); Integer total = findForInt(SqlUtils.convertSelectToCount(sql), params); // if (total != null) { // pagination.setTotal(total); // } return createPaginationResults(list, pagination, total); } /** * {@inheritDoc} */ @Override public PaginationResults> findPage(String sql, Pagination pagination, Object[] params) { Integer total = findForInt(SqlUtils.convertSelectToCount(sql), params); // if (total != null) { // pagination.setTotal(total); // } List> list = findList(sql, pagination, params); return createPaginationResults(list, pagination, total); } /** * {@inheritDoc} */ @Override public PaginationResults> findPage(String sql, Pagination pagination, List params) { return findPage(sql, pagination, toArray(params)); } /** * {@inheritDoc} */ @Override public PaginationResults findPage(String sql, Class mappingType, Pagination pagination, Map params) { Integer total = findForInt(SqlUtils.convertSelectToCount(sql), params); // if (total != null) { // pagination.setTotal(total); // } List list = findList(sql, mappingType, pagination, params); return createPaginationResults(list, pagination, total); } /** * {@inheritDoc} */ @Override public PaginationResults findPage(String sql, Class mappingType, Pagination pagination, Object[] params) { Integer total = findForInt(SqlUtils.convertSelectToCount(sql), params); // if (total != null) { // pagination.setTotal(total); // } List list = findList(sql, mappingType, pagination, params); return createPaginationResults(list, pagination, total); } /** * {@inheritDoc} */ @Override public PaginationResults findPage(String sql, Class mappingType, Pagination pagination, List params) { return findPage(sql, mappingType, pagination, toArray(params)); } /** * {@inheritDoc} */ @Override public List> findList(String sql, int start, int limit, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.queryForList( dialect.getParamNamedPaginationSql(sql, start, limit), dialect.getPaginationSqlParameter(params, start, limit)); } /** * {@inheritDoc} */ @Override public PaginationResults> findPage(String sql, int start, int limit, Map params) { checkStartAndLimit(start, limit); SimplePagination> pagination = new SimplePagination>(); List> results = findList(sql, start, limit, params); pagination.setPageResults(results); pagination.setPageSize(limit); pagination.setPageNumber((start + limit - 1) / limit); Integer total = findForInt(SqlUtils.convertSelectToCount(sql), params); pagination.setTotal(total); return pagination; } /** * {@inheritDoc} */ @Override public List> findList(String sql, int start, int limit, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.queryForList( dialect.getPaginationSql(sql, start, limit), dialect.getPaginationSqlParameter(params, start, limit)); } /** * {@inheritDoc} */ @Override public List> findList(String sql, int start, int limit, List params) { return findList(sql, start, limit, toArray(params)); } /** * {@inheritDoc} */ @Override public PaginationResults> findPage(String sql, int start, int limit, Object[] params) { checkStartAndLimit(start, limit); SimplePagination> pagination = new SimplePagination>(); List> results = findList(sql, start, limit, params); pagination.setPageResults(results); pagination.setPageSize(limit); pagination.setPageNumber((start + limit - 1) / limit); Integer total = findForInt(SqlUtils.convertSelectToCount(sql), params); pagination.setTotal(total); return pagination; } /** * {@inheritDoc} */ @Override public PaginationResults> findPage(String sql, int start, int limit, List params) { return findPage(sql, start, limit, toArray(params)); } /** * {@inheritDoc} */ @Override public List findList(Class mappingType, ConditionBuilder builder) { builder.setDialect(dialect); return simpleORMFactory.getSimpleORM(mappingType).list(builder); } /** * {@inheritDoc} */ @Override public PaginationResults findPage(Class mappingType, ConditionBuilder builder) { List list = findList(mappingType, builder); // 查询完了以后把转换count语句的干扰去掉 builder.clearOrders(); builder.setDialect(dialect); Pagination p = builder.getPagination(); builder.setPagination(null); Execution execution = simpleORMFactory.getSimpleORM(mappingType).getQueryExecution(builder); Integer total = findForInt(SqlUtils.convertSelectToCount(execution.getSql()), execution.getParams()); return createPaginationResults(list, p, total); } /** * {@inheritDoc} */ @Override public List findList(String sql, Class mappingType) { return findList(sql, mappingType, new Object[] {}); } /** * {@inheritDoc} */ @Override public List findList(String sql, Class mappingType, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.query(sql, params, getParameterizedBeanPropertyRowMapper(mappingType)); } /** * {@inheritDoc} */ @Override public List findList(String sql, Class mappingType, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.query(sql, getParameterizedBeanPropertyRowMapper(mappingType), params); } /** * {@inheritDoc} */ @Override public List findList(String sql, Class mappingType, List params) { return findList(sql, mappingType, toArray(params)); } /** * {@inheritDoc} */ @Override public List findList(String sql, Class mappingType, Pagination pagination, Map params) { // Integer total = findForInt(SqlUtils.convertSelectToCount(sql), // params); // if (total != null) { // pagination.setTotal(total); // } PaginationWrapper wrapper = new PaginationWrapper(pagination); return findList(sql, mappingType, wrapper.getStart(), wrapper.getLimit(), params); } /** * {@inheritDoc} */ @Override public List findList(String sql, Class mappingType, Pagination pagination, Object[] params) { // Integer total = findForInt(SqlUtils.convertSelectToCount(sql), // params); // if (total != null) { // pagination.setTotal(total); // } PaginationWrapper wrapper = new PaginationWrapper(pagination); return findList(sql, mappingType, wrapper.getStart(), wrapper.getLimit(), params); } /** * {@inheritDoc} */ @Override public List findList(String sql, Class mappingType, Pagination pagination, List params) { // Integer total = findForInt(SqlUtils.convertSelectToCount(sql), // params); // if (total != null) { // pagination.setTotal(total); // } PaginationWrapper wrapper = new PaginationWrapper(pagination); return findList(sql, mappingType, wrapper.getStart(), wrapper.getLimit(), params); } /** * {@inheritDoc} */ @Override public List findList(String sql, Class mappingType, int start, int limit, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.query( dialect.getParamNamedPaginationSql(sql, start, limit), dialect.getPaginationSqlParameter(params, start, limit), getParameterizedBeanPropertyRowMapper(mappingType)); } /** * {@inheritDoc} */ @Override public PaginationResults findPage(String sql, Class mappingType, int start, int limit, Map params) { checkStartAndLimit(start, limit); SimplePagination pagination = new SimplePagination(); List results = findList(sql, mappingType, start, limit, params); pagination.setPageResults(results); pagination.setPageSize(limit); pagination.setPageNumber((start + limit - 1) / limit); Integer total = findForInt(SqlUtils.convertSelectToCount(sql), params); pagination.setTotal(total); return pagination; } /** * {@inheritDoc} */ @Override public List findList(String sql, Class mappingType, int start, int limit, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.query(dialect.getPaginationSql(sql, start, limit), getParameterizedBeanPropertyRowMapper(mappingType), dialect.getPaginationSqlParameter(params, start, limit)); } /** * {@inheritDoc} */ @Override public List findList(String sql, Class mappingType, int start, int limit, List params) { return findList(sql, mappingType, start, limit, toArray(params)); } /** * {@inheritDoc} */ @Override public PaginationResults findPage(String sql, Class mappingType, int start, int limit, Object[] params) { checkStartAndLimit(start, limit); SimplePagination pagination = new SimplePagination(); List results = findList(sql, mappingType, start, limit, params); pagination.setPageResults(results); pagination.setPageSize(limit); pagination.setPageNumber((start + limit - 1) / limit); Integer total = findForInt(SqlUtils.convertSelectToCount(sql), params); pagination.setTotal(total); return pagination; } /** * {@inheritDoc} */ @Override public PaginationResults findPage(String sql, Class mappingType, int start, int limit, List params) { return findPage(sql, mappingType, start, limit, toArray(params)); } /** * {@inheritDoc} */ @Override public List findList(String sql, RowMapper rowMapper) { return findList(sql, rowMapper, new Object[] {}); } /** * {@inheritDoc} */ @Override public List findList(String sql, final RowMapper rowMapper, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.query(sql, params, new org.springframework.jdbc.core.RowMapper() { @Override public E mapRow(ResultSet rs, int rowNum) throws SQLException { return rowMapper.mapRow(rs, rowNum); } }); } /** * {@inheritDoc} */ @Override public List findList(String sql, final RowMapper rowMapper, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.query(sql, new org.springframework.jdbc.core.RowMapper() { @Override public E mapRow(ResultSet rs, int rowNum) throws SQLException { return rowMapper.mapRow(rs, rowNum); } }, params); } /** * {@inheritDoc} */ @Override public List findList(String sql, RowMapper rowMapper, List params) { return findList(sql, rowMapper, toArray(params)); } /** * {@inheritDoc} */ @Override public List findList(String sql, RowMapper rowMapper, Pagination pagination, Map params) { // Integer total = findForInt(SqlUtils.convertSelectToCount(sql), // params); // if (total != null) { // pagination.setTotal(total); // } PaginationWrapper wrapper = new PaginationWrapper(pagination); return findList(sql, rowMapper, wrapper.getStart(), wrapper.getLimit(), params); } /** * {@inheritDoc} */ @Override public List findList(String sql, RowMapper rowMapper, Pagination pagination, Object[] params) { // Integer total = findForInt(SqlUtils.convertSelectToCount(sql), // params); // if (total != null) { // pagination.setTotal(total); // } PaginationWrapper wrapper = new PaginationWrapper(pagination); return findList(sql, rowMapper, wrapper.getStart(), wrapper.getLimit(), params); } /** * {@inheritDoc} */ @Override public List findList(String sql, RowMapper rowMapper, Pagination pagination, List params) { // Integer total = findForInt(SqlUtils.convertSelectToCount(sql), // params); // if (total != null) { // pagination.setTotal(total); // } PaginationWrapper wrapper = new PaginationWrapper(pagination); return findList(sql, rowMapper, wrapper.getStart(), wrapper.getLimit(), params); } /** * {@inheritDoc} */ @Override public List findList(String sql, final RowMapper rowMapper, int start, int limit, Map params) { logger.debug("sql : {}", sql); return namedParameterJdbcTemplate.query( dialect.getParamNamedPaginationSql(sql, start, limit), dialect.getPaginationSqlParameter(params, start, limit), new org.springframework.jdbc.core.RowMapper() { @Override public E mapRow(ResultSet rs, int rowNum) throws SQLException { return rowMapper.mapRow(rs, rowNum); } }); } /** * {@inheritDoc} */ @Override public PaginationResults findPage(String sql, RowMapper rowMapper, int start, int limit, Map params) { checkStartAndLimit(start, limit); SimplePagination pagination = new SimplePagination(); List results = findList(sql, rowMapper, start, limit, params); pagination.setPageResults(results); pagination.setPageSize(limit); pagination.setPageNumber((start + limit - 1) / limit); Integer total = findForInt(SqlUtils.convertSelectToCount(sql), params); pagination.setTotal(total); return pagination; } /** * {@inheritDoc} */ @Override public List findList(String sql, final RowMapper rowMapper, int start, int limit, Object[] params) { logger.debug("sql : {}", sql); return jdbcTemplate.query(dialect.getPaginationSql(sql, start, limit), new org.springframework.jdbc.core.RowMapper() { @Override public E mapRow(ResultSet rs, int rowNum) throws SQLException { return rowMapper.mapRow(rs, rowNum); } }, dialect.getPaginationSqlParameter(params, start, limit)); } /** * {@inheritDoc} */ @Override public List findList(String sql, RowMapper rowMapper, int start, int limit, List params) { return findList(sql, rowMapper, start, limit, toArray(params)); } /** * {@inheritDoc} */ @Override public PaginationResults findPage(String sql, RowMapper rowMapper, int start, int limit, Object[] params) { checkStartAndLimit(start, limit); SimplePagination pagination = new SimplePagination(); List results = findList(sql, rowMapper, start, limit, params); pagination.setPageResults(results); pagination.setPageSize(limit); pagination.setPageNumber((start + limit - 1) / limit); Integer total = findForInt(SqlUtils.convertSelectToCount(sql), params); pagination.setTotal(total); return pagination; } /** * {@inheritDoc} */ @Override public PaginationResults findPage(String sql, RowMapper rowMapper, int start, int limit, List params) { return findPage(sql, rowMapper, start, limit, toArray(params)); } /** * {@inheritDoc} */ @Override public Serializable getIdentity(E entity) { return simpleORMFactory.getSimpleORM( ClassUtils.castGenericType(entity.getClass(), entity)) .getIdentity(entity); } // ******************************************************************** // private method // ******************************************************************** // private ParameterizedBeanPropertyRowMapper // getParameterizedBeanPropertyRowMapper(Class mappingType) { // ParameterizedBeanPropertyRowMapper rowMapper = new // ParameterizedBeanPropertyRowMapper(); // rowMapper.setMappedClass(mappingType); // return rowMapper; // } private org.springframework.jdbc.core.RowMapper getParameterizedBeanPropertyRowMapper( Class mappingType) { final BeanPropertyRowMapper mapper = new BeanPropertyRowMapper<>( mappingType); return new org.springframework.jdbc.core.RowMapper() { @Override public E mapRow(ResultSet rs, int rowNum) throws SQLException { return mapper.mapRow(rs, rowNum); } }; } // 获取指定表名称的insert private SimpleJdbcInsert getSimpleJdbcInsert(String tableName) { SimpleJdbcInsert jdbcInsert = simpleJdbcInserts.get(tableName); if (jdbcInsert == null) { jdbcInsert = new SimpleJdbcInsert(jdbcTemplate) .withTableName(tableName); simpleJdbcInserts.put(tableName, jdbcInsert); } return jdbcInsert; } // 获取指定表名称主键列名的insert private SimpleJdbcInsert getSimpleJdbcInsert(String tableName, String... pkColumnNames) { SimpleJdbcInsert jdbcInsert = getSimpleJdbcInsert(tableName); if (LangUtils.isEmpty(jdbcInsert.getGeneratedKeyNames())) { logger.debug("表{}设置主键列{}", tableName, ArrayUtils.toString(pkColumnNames)); jdbcInsert.usingGeneratedKeyColumns(pkColumnNames); } else { logger.debug("表{}已经设置了主键列{},新的设置被忽略", tableName, ArrayUtils.toString(pkColumnNames)); } return jdbcInsert; } // 设置起始和上限 private void checkStartAndLimit(int start, int limit) { if (start <= 0) { LOGGER.error("start参数必须大于0"); throw new PersistentException("start参数必须大于0"); } if (limit <= 0) { LOGGER.error("start参数必须大于0"); throw new PersistentException("limit参数必须大于0"); } } // ******************************************************************** // property // ******************************************************************** private JdbcTemplate jdbcTemplate; private NamedParameterJdbcTemplate namedParameterJdbcTemplate; private DataSource dataSource; private Dialect dialect; private SimpleORMFactory simpleORMFactory; private Map simpleJdbcInserts = new HashMap( 0); /** * @param dialect * 设置dialect */ public void setDialect(Dialect dialect) { this.dialect = dialect; } /** * @return 返回dialect */ public Dialect getDialect() { return dialect; } /** * @return 返回jdbcTemplate */ public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } /** * {@inheritDoc} */ @Override public DataSource getDataSource() { return dataSource; } /** * @param dataSource * 设置dataSource */ public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; jdbcTemplate = new JdbcTemplate(dataSource); namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); if (simpleORMFactory == null) { simpleORMFactory = new SimpleORMFactory(this.dataSource); } this.simpleORMFactory.setDataSource(dataSource); } /** * 设置simpleORMFactory * * @param simpleORMFactory * simpleORMFactory */ public void setSimpleORMFactory(SimpleORMFactory simpleORMFactory) { this.simpleORMFactory = simpleORMFactory; if (dataSource != null) { simpleORMFactory.setDataSource(dataSource); } } /** * 返回simpleORMFactory * * @return simpleORMFactory */ public SimpleORMFactory getSimpleORMFactory() { return simpleORMFactory; } private PaginationResults createPaginationResults(Iterable results, Pagination pagination, Integer total) { SimplePagination paginationResult = new SimplePagination(); paginationResult.setPageResults(results); paginationResult.setPageSize(pagination.getPageSize()); paginationResult.setPageNumber(pagination.getPageNumber()); paginationResult.setTotal(total); return paginationResult; } private Object[] toArray(List params) { if (params == null) { params = new ArrayList(); } return params.toArray(); } }