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

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

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

JdbcTemplate.queryForMap介绍

暂无

代码示例

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

@Test
public void testQueryForMapWithSingleRowAndColumn() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getObject(1)).willReturn(11);
  Map<String, Object> map = this.template.queryForMap(sql);
  assertEquals("Wow is Integer", 11, ((Integer) map.get("age")).intValue());
  verify(this.resultSet).close();
  verify(this.statement).close();
}

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

@Test
public void testQueryForMapWithArgsAndSingleRowAndColumn() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getObject(1)).willReturn(11);
  Map<String, Object> map = this.template.queryForMap(sql, new Object[] {3});
  assertEquals("Row is Integer", 11, ((Integer) map.get("age")).intValue());
  verify(this.preparedStatement).setObject(1, 3);
  verify(this.resultSet).close();
  verify(this.preparedStatement).close();
}

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

@Override
protected void doJumpToPage(int itemIndex) {
  /*
   * Normally this would be false (the startAfterValue is enough
   * information to restart from.
   */
  // TODO: this is dead code, startAfterValues is never null - see #open(ExecutionContext)
  if (startAfterValues == null && getPage() > 0) {
    String jumpToItemSql = queryProvider.generateJumpToItemQuery(itemIndex, getPageSize());
    if (logger.isDebugEnabled()) {
      logger.debug("SQL used for jumping: [" + jumpToItemSql + "]");
    }
    
    if (this.queryProvider.isUsingNamedParameters()) {
      startAfterValues = namedParameterJdbcTemplate.queryForMap(jumpToItemSql, getParameterMap(parameterValues, null));
    }
    else {
      startAfterValues = getJdbcTemplate().queryForMap(jumpToItemSql, getParameterList(parameterValues, null).toArray());
    }
  }
}

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

@Test  // SPR-16578
public void testEquallyNamedColumn() throws SQLException {
  given(this.connection.createStatement()).willReturn(this.statement);
  ResultSetMetaData metaData = mock(ResultSetMetaData.class);
  given(metaData.getColumnCount()).willReturn(2);
  given(metaData.getColumnLabel(1)).willReturn("x");
  given(metaData.getColumnLabel(2)).willReturn("X");
  given(this.resultSet.getMetaData()).willReturn(metaData);
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getObject(1)).willReturn("first value");
  given(this.resultSet.getObject(2)).willReturn("second value");
  Map<String, Object> map = this.template.queryForMap("my query");
  assertEquals(1, map.size());
  assertEquals("first value", map.get("x"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testAddClientWithNoDetails() {
  BaseClientDetails clientDetails = new BaseClientDetails();
  clientDetails.setClientId("addedClientIdWithNoDetails");
  service.addClientDetails(clientDetails);
  Map<String, Object> map = jdbcTemplate.queryForMap(SELECT_SQL,
      "addedClientIdWithNoDetails");
  assertEquals("addedClientIdWithNoDetails", map.get("client_id"));
  assertTrue(map.containsKey("client_secret"));
  assertEquals(null, map.get("client_secret"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testUpdateClientRedirectURI() {
  BaseClientDetails clientDetails = new BaseClientDetails();
  clientDetails.setClientId("newClientIdWithNoDetails");
  service.addClientDetails(clientDetails);
  String[] redirectURI = { "http://localhost:8080",
      "http://localhost:9090" };
  clientDetails.setRegisteredRedirectUri(new HashSet<String>(Arrays
      .asList(redirectURI)));
  service.updateClientDetails(clientDetails);
  Map<String, Object> map = jdbcTemplate.queryForMap(SELECT_SQL,
      "newClientIdWithNoDetails");
  assertEquals("newClientIdWithNoDetails", map.get("client_id"));
  assertTrue(map.containsKey("web_server_redirect_uri"));
  assertEquals("http://localhost:8080,http://localhost:9090",
      map.get("web_server_redirect_uri"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testDeleteClientSecret() {
  String clientId = "client_id_test_delete";
  BaseClientDetails clientDetails = new BaseClientDetails();
  clientDetails.setClientId(clientId);
  clientDetails.setClientSecret(SECRET);
  service.addClientDetails(clientDetails);
  service.addClientSecret(clientId, "new_secret", IdentityZoneHolder.get().getId());
  Map<String, Object> map = jdbcTemplate.queryForMap(SELECT_SQL, clientId);
  String clientSecretBeforeDelete = (String) map.get("client_secret");
  assertNotNull(clientSecretBeforeDelete);
  assertEquals(2, clientSecretBeforeDelete.split(" ").length);
  service.deleteClientSecret(clientId, IdentityZoneHolder.get().getId());
  map = jdbcTemplate.queryForMap(SELECT_SQL, clientId);
  String clientSecret = (String) map.get("client_secret");
  assertNotNull(clientSecret);
  assertEquals(1, clientSecret.split(" ").length);
  assertEquals(clientSecretBeforeDelete.split(" ")[1], clientSecret);
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testUpdateClientSecret() {
  BaseClientDetails clientDetails = new BaseClientDetails();
  clientDetails.setClientId("newClientIdWithNoDetails");
  service.setPasswordEncoder(new PasswordEncoder() {
    public boolean matches(CharSequence rawPassword,
        String encodedPassword) {
      return true;
    }
    public String encode(CharSequence rawPassword) {
      return "BAR";
    }
  });
  service.addClientDetails(clientDetails);
  service.updateClientSecret(clientDetails.getClientId(), "foo");
  Map<String, Object> map = jdbcTemplate.queryForMap(SELECT_SQL,
      "newClientIdWithNoDetails");
  assertEquals("newClientIdWithNoDetails", map.get("client_id"));
  assertTrue(map.containsKey("client_secret"));
  assertEquals("BAR", map.get("client_secret"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testCreateSamlServiceProviderInOtherZone() throws Exception {
  IdentityZone zone = MultitenancyFixture.identityZone(UUID.randomUUID().toString(), "myzone");
  IdentityZoneHolder.set(zone);
  SamlServiceProvider sp = createSamlServiceProvider(zone.getId());
  SamlServiceProvider createdSp = db.create(sp, sp.getIdentityZoneId());
  Map<String, Object> rawCreatedSp = jdbcTemplate.queryForMap("select * from service_provider where id = ?",
      createdSp.getId());
  assertEquals(sp.getName(), createdSp.getName());
  assertEquals(sp.getConfig(), createdSp.getConfig());
  assertEquals(sp.getName(), rawCreatedSp.get("name"));
  assertEquals(sp.getConfig(),
      JsonUtils.readValue((String) rawCreatedSp.get("config"), SamlServiceProviderDefinition.class));
  assertEquals(zone.getId(), rawCreatedSp.get("identity_zone_id"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testUpdateIdentityProviderInOtherZone() throws Exception {
  IdentityZone zone = MultitenancyFixture.identityZone(UUID.randomUUID().toString(),"myzone");
  IdentityZoneHolder.set(zone);
  String originKey = RandomStringUtils.randomAlphabetic(6);
  String idpId = RandomStringUtils.randomAlphabetic(6);
  IdentityProvider idp = MultitenancyFixture.identityProvider(originKey, zone.getId());
  idp.setId(idpId);
  idp = db.create(idp, zone.getId());
  AbstractIdentityProviderDefinition definition = new AbstractIdentityProviderDefinition();
  idp.setConfig(definition);
  IdentityProvider updatedIdp = db.update(idp, zone.getId());
  Map<String, Object> rawUpdatedIdp = jdbcTemplate.queryForMap("select * from identity_provider where id = ?",updatedIdp.getId());
  assertEquals(definition, updatedIdp.getConfig());
  assertEquals(definition, JsonUtils.readValue((String)rawUpdatedIdp.get("config"), AbstractIdentityProviderDefinition.class));
  assertEquals(zone.getId(), rawUpdatedIdp.get("identity_zone_id"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testCreateIdentityProviderInOtherZone() throws Exception {
  IdentityZone zone = MultitenancyFixture.identityZone(UUID.randomUUID().toString(), "myzone");
  IdentityZoneHolder.set(zone);
  String originKey = RandomStringUtils.randomAlphabetic(6);
  IdentityProvider idp = MultitenancyFixture.identityProvider(originKey, zone.getId());
  IdentityProvider createdIdp = db.create(idp, zone.getId());
  Map<String, Object> rawCreatedIdp = jdbcTemplate.queryForMap("select * from identity_provider where id = ?",createdIdp.getId());
  assertEquals(idp.getName(), createdIdp.getName());
  assertEquals(idp.getOriginKey(), createdIdp.getOriginKey());
  assertEquals(idp.getType(), createdIdp.getType());
  assertEquals(idp.getConfig(), createdIdp.getConfig());
  assertEquals(idp.getName(), rawCreatedIdp.get("name"));
  assertEquals(idp.getOriginKey(), rawCreatedIdp.get("origin_key"));
  assertEquals(idp.getType(), rawCreatedIdp.get("type"));
  assertEquals(idp.getConfig(), JsonUtils.readValue((String)rawCreatedIdp.get("config"), AbstractIdentityProviderDefinition.class));
  assertEquals(zone.getId(), rawCreatedIdp.get("identity_zone_id"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void canCreateUserInDefaultIdentityZone() {
  ScimUser user = new ScimUser(null, "jo@foo.com", "Jo", "User");
  user.addEmail("jo@blah.com");
  ScimUser created = db.createUser(user, "j7hyqpassX", IdentityZoneHolder.get().getId());
  assertEquals("jo@foo.com", created.getUserName());
  assertNotNull(created.getId());
  assertNotSame(user.getId(), created.getId());
  Map<String, Object> map = jdbcTemplate.queryForMap("select * from users where id=?", created.getId());
  assertEquals(user.getUserName(), map.get("userName"));
  assertEquals(user.getUserType(), map.get(UaaAuthority.UAA_USER.getUserType()));
  assertNull(created.getGroups());
  assertEquals("uaa", map.get("identity_zone_id"));
  assertNull(user.getPasswordLastModified());
  assertNotNull(created.getPasswordLastModified());
  assertTrue(Math.abs(created.getMeta().getCreated().getTime() - created.getPasswordLastModified().getTime()) < 1001); //1 second at most given MySQL fractionless timestamp
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testCreateAndUpdateSamlServiceProviderInDefaultZone() throws Exception {
  IdentityZoneHolder.set(IdentityZone.getUaa());
  String zoneId = IdentityZone.getUaa().getId();
  SamlServiceProvider sp = createSamlServiceProvider(zoneId);
  SamlServiceProvider createdSp = db.create(sp, sp.getIdentityZoneId());
  Map<String, Object> rawCreatedSp = jdbcTemplate.queryForMap("select * from service_provider where id = ?",
      createdSp.getId());
  assertEquals(sp.getName(), createdSp.getName());
  assertEquals(sp.getConfig(), createdSp.getConfig());
  assertEquals(sp.getName(), rawCreatedSp.get("name"));
  assertEquals(sp.getConfig(),
      JsonUtils.readValue((String) rawCreatedSp.get("config"), SamlServiceProviderDefinition.class));
  assertEquals(zoneId, rawCreatedSp.get("identity_zone_id").toString().trim());
  sp.setId(createdSp.getId());
  sp.setLastModified(new Timestamp(System.currentTimeMillis()));
  sp.setName("updated name");
  sp.setCreated(createdSp.getCreated());
  SamlServiceProviderDefinition updatedConfig = new SamlServiceProviderDefinition();
  updatedConfig.setMetaDataLocation(SamlTestUtils.UNSIGNED_SAML_SP_METADATA);
  sp.setConfig(updatedConfig);
  sp.setIdentityZoneId(zoneId);
  createdSp = db.update(sp, IdentityZoneHolder.get().getId());
  assertEquals(sp.getName(), createdSp.getName());
  assertEquals(sp.getConfig(), createdSp.getConfig());
  assertTrue(Math.abs(sp.getLastModified().getTime()  - createdSp.getLastModified().getTime()) < 1001);
  assertEquals(Integer.valueOf(rawCreatedSp.get("version").toString()) + 1, createdSp.getVersion());
  assertEquals(zoneId, createdSp.getIdentityZoneId());
}

代码示例来源:origin: cloudfoundry/uaa

@Test(expected = EmptyResultDataAccessException.class)
public void testUpdateSamlServiceProviderInWrongZone() throws Exception {
  IdentityZone zone = MultitenancyFixture.identityZone(UUID.randomUUID().toString(), "myzone");
  IdentityZoneHolder.set(zone);
  SamlServiceProvider sp = createSamlServiceProvider(zone.getId());
  SamlServiceProvider createdSp = db.create(sp, sp.getIdentityZoneId());
  Map<String, Object> rawCreatedSp = jdbcTemplate.queryForMap("select * from service_provider where id = ?",
      createdSp.getId());
  assertEquals(sp.getName(), createdSp.getName());
  assertEquals(sp.getConfig(), createdSp.getConfig());
  assertEquals(sp.getName(), rawCreatedSp.get("name"));
  assertEquals(sp.getConfig(),
      JsonUtils.readValue((String) rawCreatedSp.get("config"), SamlServiceProviderDefinition.class));
  assertEquals(zone.getId(), rawCreatedSp.get("identity_zone_id").toString().trim());
  sp.setId(createdSp.getId());
  sp.setLastModified(new Timestamp(System.currentTimeMillis()));
  sp.setName("updated name");
  sp.setCreated(createdSp.getCreated());
  SamlServiceProviderDefinition updatedConfig = new SamlServiceProviderDefinition();
  updatedConfig.setMetaDataLocation(SamlTestUtils.UNSIGNED_SAML_SP_METADATA);
  sp.setConfig(updatedConfig);
  sp.setIdentityZoneId(zone.getId());
  // Switch to a different zone before updating.
  IdentityZoneHolder.set(IdentityZone.getUaa());
  db.update(sp, IdentityZoneHolder.get().getId());
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void canCreateUserWithoutGivenNameAndFamilyName() {
  ScimUser user = new ScimUser(null, "jonah@foo.com", null, null);
  user.addEmail("jo@blah.com");
  ScimUser created = db.createUser(user, "j7hyqpassX", IdentityZoneHolder.get().getId());
  assertEquals("jonah@foo.com", created.getUserName());
  assertNotNull(created.getId());
  assertNotSame(user.getId(), created.getId());
  Map<String, Object> map = jdbcTemplate.queryForMap("select * from users where id=?", created.getId());
  assertEquals(user.getUserName(), map.get("userName"));
  assertEquals(user.getUserType(), map.get(UaaAuthority.UAA_USER.getUserType()));
  assertNull(created.getGroups());
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void canCreateUserInOtherIdentityZone() {
  String otherZoneId = "my-zone-id";
  createOtherIdentityZone(otherZoneId);
  String idpId = createOtherIdentityProvider(OriginKeys.UAA, otherZoneId);
  ScimUser user = new ScimUser(null, "jo@foo.com", "Jo", "User");
  user.addEmail("jo@blah.com");
  ScimUser created = db.createUser(user, "j7hyqpassX", IdentityZoneHolder.get().getId());
  assertEquals("jo@foo.com", created.getUserName());
  assertNotNull(created.getId());
  assertNotSame(user.getId(), created.getId());
  Map<String, Object> map = jdbcTemplate.queryForMap("select * from users where id=?", created.getId());
  assertEquals(user.getUserName(), map.get("userName"));
  assertEquals(user.getUserType(), map.get(UaaAuthority.UAA_USER.getUserType()));
  assertNull(created.getGroups());
  assertEquals("my-zone-id", map.get("identity_zone_id"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testUpdateIdentityProviderInDefaultZone() throws Exception {
  String zoneId = IdentityZone.getUaa().getId();
  String originKey = RandomStringUtils.randomAlphabetic(6);
  String idpId = RandomStringUtils.randomAlphabetic(6);
  IdentityProvider idp = MultitenancyFixture.identityProvider(originKey, zoneId);
  idp.setId(idpId);
  idp.setType(OriginKeys.LDAP);
  idp = db.create(idp, zoneId);
  LdapIdentityProviderDefinition definition = new LdapIdentityProviderDefinition();
  idp.setConfig(definition);
  IdentityProvider updatedIdp = db.update(idp, zoneId);
  Map<String, Object> rawUpdatedIdp = jdbcTemplate.queryForMap("select * from identity_provider where id = ?",updatedIdp.getId());
  assertEquals(definition, updatedIdp.getConfig());
  assertEquals(definition, JsonUtils.readValue((String)rawUpdatedIdp.get("config"),LdapIdentityProviderDefinition.class));
  assertEquals(IdentityZone.getUaa().getId(), rawUpdatedIdp.get("identity_zone_id"));
}

代码示例来源:origin: cloudfoundry/uaa

idp.setType(UAA);
IdentityProvider createdIdp = db.create(idp, zoneId);
Map<String, Object> rawCreatedIdp = jdbcTemplate.queryForMap("select * from identity_provider where id = ?",createdIdp.getId());

代码示例来源:origin: cloudfoundry/uaa

@Test
public void validateOriginAndExternalIDDuringCreateAndUpdate() {
  String origin = "test";
  ScimUserBootstrapTests.addIdentityProvider(jdbcTemplate, origin);
  String externalId = "testId";
  ScimUser user = new ScimUser(null, "jo@foo.com", "Jo", "User");
  user.setOrigin(origin);
  user.setExternalId(externalId);
  user.addEmail("jo@blah.com");
  ScimUser created = db.createUser(user, "j7hyqpassX", IdentityZoneHolder.get().getId());
  assertEquals("jo@foo.com", created.getUserName());
  assertNotNull(created.getId());
  assertNotSame(user.getId(), created.getId());
  Map<String, Object> map = jdbcTemplate.queryForMap("select * from users where id=?", created.getId());
  assertEquals(user.getUserName(), map.get("userName"));
  assertEquals(user.getUserType(), map.get(UaaAuthority.UAA_USER.getUserType()));
  assertNull(created.getGroups());
  assertEquals(origin, created.getOrigin());
  assertEquals(externalId, created.getExternalId());
  String origin2 = "test2";
  ScimUserBootstrapTests.addIdentityProvider(jdbcTemplate,origin2);
  String externalId2 = "testId2";
  created.setOrigin(origin2);
  created.setExternalId(externalId2);
  ScimUser updated = db.update(created.getId(), created, IdentityZoneHolder.get().getId());
  assertEquals(origin2, updated.getOrigin());
  assertEquals(externalId2, updated.getExternalId());
}

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

@Test
public void testSimpleOutboundChannelAdapter() {
  setUp("handlingWithJdbcOperationsJdbcOutboundChannelAdapterTest.xml", getClass());
  Message<?> message = MessageBuilder.withPayload("foo").setHeader("business.key", "FOO").build();
  channel.send(message);
  Map<String, Object> map = this.jdbcTemplate.queryForMap("SELECT * from FOOS");
  assertEquals("Wrong id", "FOO", map.get("ID"));
  assertEquals("Wrong id", "foo", map.get("name"));
  JdbcMessageHandler handler = context.getBean(JdbcMessageHandler.class);
  assertEquals(23, TestUtils.getPropertyValue(handler, "order"));
  assertEquals(1, adviceCalled);
}

相关文章