use of org.springframework.jdbc.core.JdbcTemplate in project libresonic by Libresonic.
the class AbstractDao method namedQueryWithLimit.
protected <T> List<T> namedQueryWithLimit(String sql, RowMapper<T> rowMapper, Map<String, Object> args, int limit) {
long t = System.nanoTime();
JdbcTemplate jdbcTemplate = new JdbcTemplate(daoHelper.getDataSource());
jdbcTemplate.setMaxRows(limit);
NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
List<T> result = namedTemplate.query(sql, args, rowMapper);
log(sql, t);
return result;
}
use of org.springframework.jdbc.core.JdbcTemplate in project SpringStepByStep by JavaProgrammerLB.
the class Employee method printEmployee.
public Employee printEmployee(Integer id) {
String SQL = "SELECT id, name, age FROM Employee";
Employee employee = new JdbcTemplate(dataSource).queryForObject(SQL, new Object[] { id }, new EmployeeMapper());
return employee;
}
use of org.springframework.jdbc.core.JdbcTemplate in project spring-boot by spring-projects.
the class DataSourceHealthIndicator method setDataSource.
/**
* Set the {@link DataSource} to use.
* @param dataSource the data source
*/
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
use of org.springframework.jdbc.core.JdbcTemplate in project spring-boot by spring-projects.
the class DataSourceInitializerTests method multipleScriptsAppliedInLexicalOrder.
@Test
public void multipleScriptsAppliedInLexicalOrder() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true", "spring.datasource.schema:" + ClassUtils.addResourcePathToPackagePath(getClass(), "lexical-schema-*.sql"), "spring.datasource.data:" + ClassUtils.addResourcePathToPackagePath(getClass(), "data.sql"));
this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
ReverseOrderResourceLoader resourceLoader = new ReverseOrderResourceLoader(new DefaultResourceLoader());
this.context.setResourceLoader(resourceLoader);
this.context.refresh();
DataSource dataSource = this.context.getBean(DataSource.class);
assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
assertThat(dataSource).isNotNull();
JdbcOperations template = new JdbcTemplate(dataSource);
assertThat(template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)).isEqualTo(1);
}
use of org.springframework.jdbc.core.JdbcTemplate in project spring-boot by spring-projects.
the class DataSourceInitializerTests method testDataSourceInitializedWithMultipleScripts.
@Test
public void testDataSourceInitializedWithMultipleScripts() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true", "spring.datasource.schema:" + ClassUtils.addResourcePathToPackagePath(getClass(), "schema.sql") + "," + ClassUtils.addResourcePathToPackagePath(getClass(), "another.sql"), "spring.datasource.data:" + ClassUtils.addResourcePathToPackagePath(getClass(), "data.sql"));
this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
DataSource dataSource = this.context.getBean(DataSource.class);
assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
assertThat(dataSource).isNotNull();
JdbcOperations template = new JdbcTemplate(dataSource);
assertThat(template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)).isEqualTo(1);
assertThat(template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class)).isEqualTo(0);
}
Aggregations