org.onetwo.dbm.jdbc.DbmNamedJdbcTemplate Maven / Gradle / Ivy
The newest version!
package org.onetwo.dbm.jdbc;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.lang3.ArrayUtils;
import org.onetwo.dbm.jdbc.internal.SpringStatementParameterSetter;
import org.onetwo.dbm.jdbc.spi.JdbcStatementParameterSetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterUtils;
import org.springframework.jdbc.core.namedparam.ParsedSql;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
/***
* 对jdbc查询的扩展,查询的sql里可包含命名的参数
* @author weishao
*
*/
public class DbmNamedJdbcTemplate extends NamedParameterJdbcTemplate {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private JdbcStatementParameterSetter jdbcParameterSetter = new SpringStatementParameterSetter();
public DbmNamedJdbcTemplate(DataSource dataSource) {
super(dataSource);
}
public DbmNamedJdbcTemplate(JdbcOperations classicJdbcTemplate) {
super(classicJdbcTemplate);
}
public void setJdbcParameterSetter(JdbcStatementParameterSetter jdbcParameterSetter) {
this.jdbcParameterSetter = jdbcParameterSetter;
}
public Object execute(String sql, Map paramMap) throws DataAccessException {
PreparedStatementCreator pstCreator = getPreparedStatementCreator(sql, new MapSqlParameterSource(paramMap));
final PreparedStatementSetter setter = (PreparedStatementSetter) pstCreator;
// System.out.println("sql: " + ((SqlProvider)setter).getSql());
return execute(sql, new MapSqlParameterSource(paramMap), new PreparedStatementCallback
© 2015 - 2025 Weber Informatics LLC | Privacy Policy