ninja.cero.sqltemplate.core.SqlTemplate Maven / Gradle / Ivy
package ninja.cero.sqltemplate.core;
import ninja.cero.sqltemplate.core.mapper.MapperBuilder;
import ninja.cero.sqltemplate.core.parameter.BeanParameter;
import ninja.cero.sqltemplate.core.parameter.MapParameter;
import ninja.cero.sqltemplate.core.parameter.ParamBuilder;
import ninja.cero.sqltemplate.core.template.TemplateEngine;
import ninja.cero.sqltemplate.core.util.TypeUtils;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SqlTemplate {
public static final Object[] EMPTY_ARGS = new Object[0];
protected final JdbcTemplate jdbcTemplate;
protected final NamedParameterJdbcTemplate namedJdbcTemplate;
protected TemplateEngine templateEngine;
protected ParamBuilder paramBuilder;
protected MapperBuilder mapperBuilder;
public SqlTemplate(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate) {
this(jdbcTemplate, namedJdbcTemplate, TemplateEngine.TEXT_FILE, new ParamBuilder(), new MapperBuilder());
}
/**
* Deprecated.
* Use {@link FreeMarkerSqlTemplate}, {@link PlainTextSqlTemplate} or original class extends {@link SqlTemplate}.
*
* @param jdbcTemplate The JdbcTemplate to use
* @param namedJdbcTemplate The NamedParameterJdbcTemplate to use
* @param templateEngine The tepmlate engine to use
*/
@Deprecated
public SqlTemplate(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate, TemplateEngine templateEngine) {
this(jdbcTemplate, namedJdbcTemplate, templateEngine, new ParamBuilder(), new MapperBuilder());
}
public SqlTemplate(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate, ZoneId zoneId) {
this(jdbcTemplate, namedJdbcTemplate, TemplateEngine.TEXT_FILE, new ParamBuilder(zoneId), new MapperBuilder(zoneId));
}
/**
* Deprecated.
* Use {@link FreeMarkerSqlTemplate}, {@link PlainTextSqlTemplate} or original class extends {@link SqlTemplate}.
*
* @param jdbcTemplate The JdbcTemplate to use
* @param namedJdbcTemplate The NamedParameterJdbcTemplate to use
* @param templateEngine The tepmlate engine to use
* @param zoneId The zoneId for zone aware date type such as ZonedDateTime, OffsetDateTime
*/
@Deprecated
public SqlTemplate(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate, TemplateEngine templateEngine, ZoneId zoneId) {
this(jdbcTemplate, namedJdbcTemplate, templateEngine, new ParamBuilder(zoneId), new MapperBuilder(zoneId));
}
public SqlTemplate(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate, TemplateEngine templateEngine, ParamBuilder paramBuilder, MapperBuilder mapperBuilder) {
this.jdbcTemplate = jdbcTemplate;
this.namedJdbcTemplate = namedJdbcTemplate;
this.templateEngine = templateEngine;
this.paramBuilder = paramBuilder;
this.mapperBuilder = mapperBuilder;
}
public T forObject(String fileName, Class clazz) {
List list = forList(fileName, clazz);
return DataAccessUtils.singleResult(list);
}
public T forObject(String fileName, Class clazz, Object... args) {
List list = forList(fileName, clazz, args);
return DataAccessUtils.singleResult(list);
}
public T forObject(String fileName, Class clazz, Map params) {
List list = forList(fileName, clazz, params);
return DataAccessUtils.singleResult(list);
}
public T forObject(String fileName, Class clazz, Object entity) {
List list = forList(fileName, clazz, entity);
return DataAccessUtils.singleResult(list);
}
public List forList(String fileName, Class clazz) {
String sql = getTemplate(fileName, EMPTY_ARGS);
return jdbcTemplate.query(sql, mapperBuilder.mapper(clazz));
}
public List forList(String fileName, Class clazz, Object... args) {
String sql = getTemplate(fileName, args);
return jdbcTemplate.query(sql, paramBuilder.byArgs(args), mapperBuilder.mapper(clazz));
}
public List forList(String fileName, Class clazz, Map params) {
String sql = getTemplate(fileName, params);
return namedJdbcTemplate.query(sql, paramBuilder.byMap(params), mapperBuilder.mapper(clazz));
}
public List forList(String fileName, Class clazz, Object entity) {
String sql = getTemplate(fileName, entity);
if (TypeUtils.isSimpleValueType(entity.getClass())) {
return jdbcTemplate.query(sql, paramBuilder.byArgs(entity), mapperBuilder.mapper(clazz));
}
return namedJdbcTemplate.query(sql, paramBuilder.byBean(entity), mapperBuilder.mapper(clazz));
}
public Map forMap(String fileName) {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy