org.springframework.jdbc.core.JdbcTemplate.query()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(212)

本文整理了Java中org.springframework.jdbc.core.JdbcTemplate.query()方法的一些代码示例,展示了JdbcTemplate.query()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JdbcTemplate.query()方法的具体详情如下:
包路径:org.springframework.jdbc.core.JdbcTemplate
类名称:JdbcTemplate
方法名:query

JdbcTemplate.query介绍

[英]Query using a prepared statement, allowing for a PreparedStatementCreator and a PreparedStatementSetter. Most other query methods use this method, but application code will always work with either a creator or a setter.
[中]使用prepared语句进行查询,允许PreparedStatementCreator和PreparedStatementSetter。大多数其他查询方法都使用此方法,但应用程序代码将始终与创建者或setter一起工作。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T query(PreparedStatementCreator psc, ResultSetExtractor<T> rse) throws DataAccessException {
  return query(psc, null, rse);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T query(String sql, @Nullable PreparedStatementSetter pss, ResultSetExtractor<T> rse) throws DataAccessException {
  return query(new SimplePreparedStatementCreator(sql), pss, rse);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T queryForObject(String sql, RowMapper<T> rowMapper) throws DataAccessException {
  List<T> results = query(sql, rowMapper);
  return DataAccessUtils.nullableSingleResult(results);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T query(String sql, Object[] args, int[] argTypes, ResultSetExtractor<T> rse) throws DataAccessException {
  return query(sql, newArgTypePreparedStatementSetter(args, argTypes), rse);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T queryForObject(String sql, @Nullable Object[] args, RowMapper<T> rowMapper) throws DataAccessException {
  List<T> results = query(sql, args, new RowMapperResultSetExtractor<>(rowMapper, 1));
  return DataAccessUtils.nullableSingleResult(results);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object... args) throws DataAccessException {
  List<T> results = query(sql, args, new RowMapperResultSetExtractor<>(rowMapper, 1));
  return DataAccessUtils.nullableSingleResult(results);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
    throws DataAccessException {
  List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<>(rowMapper, 1));
  return DataAccessUtils.nullableSingleResult(results);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void preparedStatementCreatorWithRowMapper() throws SQLException {
  result = template.query(con -> preparedStatement, testRowMapper);
  verify(preparedStatement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void staticQueryWithRowMapper() throws SQLException {
  result = template.query("some SQL", testRowMapper);
  verify(statement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void preparedStatementSetterWithRowMapper() throws SQLException {
  result = template.query("some SQL", ps -> ps.setString(1, "test"), testRowMapper);
  verify(preparedStatement).setString(1, "test");
  verify(preparedStatement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testStringsWithEmptyPreparedStatementArgs() throws Exception {
  doTestStrings(null, null, null, null,
      (template, sql, rch) -> template.query(sql, (Object[]) null, rch));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testStringsWithEmptyPreparedStatementSetter() throws Exception {
  doTestStrings(null, null, null, null, (template, sql, rch) ->
      template.query(sql, (PreparedStatementSetter) null, rch));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testStringsWithPreparedStatementArgs() throws Exception {
  final Integer argument = 99;
  doTestStrings(null, null, null, argument,
      (template, sql, rch) -> template.query(sql, new Object[] {argument}, rch));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void queryWithArgsAndTypesAndRowMapper() throws SQLException {
  result = template.query("some SQL",
      new Object[] { "test1", "test2" },
      new int[] { Types.VARCHAR, Types.VARCHAR },
      testRowMapper);
  verify(preparedStatement).setString(1, "test1");
  verify(preparedStatement).setString(2, "test2");
  verify(preparedStatement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testCloseConnectionOnRequest() throws Exception {
  String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3";
  given(this.resultSet.next()).willReturn(false);
  given(this.connection.createStatement()).willReturn(this.preparedStatement);
  RowCountCallbackHandler rcch = new RowCountCallbackHandler();
  this.template.query(sql, rcch);
  verify(this.resultSet).close();
  verify(this.preparedStatement).close();
  verify(this.connection).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryWithSpaceInColumnNameAndLocalDateTime() throws Exception {
  Mock mock = new Mock(MockType.THREE);
  List<SpacePerson> result = mock.getJdbcTemplate().query(
      "select last_name as \"Last Name\", age, birth_date, balance from people",
      new BeanPropertyRowMapper<>(SpacePerson.class));
  assertEquals(1, result.size());
  verifyPerson(result.get(0));
  mock.verifyClosed();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testStaticQueryWithRowMapper() throws Exception {
  Mock mock = new Mock();
  List<Person> result = mock.getJdbcTemplate().query(
      "select name, age, birth_date, balance from people",
      new BeanPropertyRowMapper<>(Person.class));
  assertEquals(1, result.size());
  verifyPerson(result.get(0));
  mock.verifyClosed();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryWithSpaceInColumnNameAndLocalDate() throws Exception {
  Mock mock = new Mock(MockType.THREE);
  List<DatePerson> result = mock.getJdbcTemplate().query(
      "select last_name as \"Last Name\", age, birth_date, balance from people",
      new BeanPropertyRowMapper<>(DatePerson.class));
  assertEquals(1, result.size());
  verifyPerson(result.get(0));
  mock.verifyClosed();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testMappingWithNoUnpopulatedFieldsFound() throws Exception {
  Mock mock = new Mock();
  List<ConcretePerson> result = mock.getJdbcTemplate().query(
      "select name, age, birth_date, balance from people",
      new BeanPropertyRowMapper<>(ConcretePerson.class, true));
  assertEquals(1, result.size());
  verifyPerson(result.get(0));
  mock.verifyClosed();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testMappingNullValue() throws Exception {
  BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
  Mock mock = new Mock(MockType.TWO);
  thrown.expect(TypeMismatchException.class);
  mock.getJdbcTemplate().query(
      "select name, null as age, birth_date, balance from people", mapper);
}

相关文章