Search in sources :

Example 31 with JdbcTemplate

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;
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 32 with JdbcTemplate

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;
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 33 with JdbcTemplate

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);
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 34 with JdbcTemplate

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);
}
Also used : JdbcOperations(org.springframework.jdbc.core.JdbcOperations) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 35 with JdbcTemplate

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);
}
Also used : JdbcOperations(org.springframework.jdbc.core.JdbcOperations) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)124 Test (org.junit.Test)46 DataSource (javax.sql.DataSource)37 Before (org.junit.Before)19 SQLException (java.sql.SQLException)11 EmbeddedDatabaseBuilder (org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder)11 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)6 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)6 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)6 Connection (java.sql.Connection)6 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)6 TransactionStatus (org.springframework.transaction.TransactionStatus)6 Test (org.testng.annotations.Test)6 SqlTemplate (com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate)5 PreparedStatement (java.sql.PreparedStatement)5 Table (org.apache.ddlutils.model.Table)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 DataAccessException (org.springframework.dao.DataAccessException)5 AbstractDriverBasedDataSource (org.springframework.jdbc.datasource.AbstractDriverBasedDataSource)5 TransactionCallback (org.springframework.transaction.support.TransactionCallback)5