001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jdbc.core.simple; 018 019import java.util.Arrays; 020import java.util.Map; 021 022import javax.sql.DataSource; 023 024import org.springframework.jdbc.core.JdbcTemplate; 025import org.springframework.jdbc.core.namedparam.SqlParameterSource; 026import org.springframework.jdbc.support.KeyHolder; 027 028/** 029 * A SimpleJdbcInsert is a multi-threaded, reusable object providing easy insert 030 * capabilities for a table. It provides meta-data processing to simplify the code 031 * needed to construct a basic insert statement. All you need to provide is the 032 * name of the table and a Map containing the column names and the column values. 033 * 034 * <p>The meta-data processing is based on the DatabaseMetaData provided by the 035 * JDBC driver. As long as the JDBC driver can provide the names of the columns 036 * for a specified table than we can rely on this auto-detection feature. If that 037 * is not the case, then the column names must be specified explicitly. 038 * 039 * <p>The actual insert is being handled using Spring's {@link JdbcTemplate}. 040 * 041 * <p>Many of the configuration methods return the current instance of the 042 * SimpleJdbcInsert to provide the ability to chain multiple ones together 043 * in a "fluent" interface style. 044 * 045 * @author Thomas Risberg 046 * @author Juergen Hoeller 047 * @since 2.5 048 * @see java.sql.DatabaseMetaData 049 * @see org.springframework.jdbc.core.JdbcTemplate 050 */ 051public class SimpleJdbcInsert extends AbstractJdbcInsert implements SimpleJdbcInsertOperations { 052 053 /** 054 * Constructor that takes one parameter with the JDBC DataSource to use when creating the 055 * JdbcTemplate. 056 * @param dataSource the {@code DataSource} to use 057 * @see org.springframework.jdbc.core.JdbcTemplate#setDataSource 058 */ 059 public SimpleJdbcInsert(DataSource dataSource) { 060 super(dataSource); 061 } 062 063 /** 064 * Alternative Constructor that takes one parameter with the JdbcTemplate to be used. 065 * @param jdbcTemplate the {@code JdbcTemplate} to use 066 * @see org.springframework.jdbc.core.JdbcTemplate#setDataSource 067 */ 068 public SimpleJdbcInsert(JdbcTemplate jdbcTemplate) { 069 super(jdbcTemplate); 070 } 071 072 073 @Override 074 public SimpleJdbcInsert withTableName(String tableName) { 075 setTableName(tableName); 076 return this; 077 } 078 079 @Override 080 public SimpleJdbcInsert withSchemaName(String schemaName) { 081 setSchemaName(schemaName); 082 return this; 083 } 084 085 @Override 086 public SimpleJdbcInsert withCatalogName(String catalogName) { 087 setCatalogName(catalogName); 088 return this; 089 } 090 091 @Override 092 public SimpleJdbcInsert usingColumns(String... columnNames) { 093 setColumnNames(Arrays.asList(columnNames)); 094 return this; 095 } 096 097 @Override 098 public SimpleJdbcInsert usingGeneratedKeyColumns(String... columnNames) { 099 setGeneratedKeyNames(columnNames); 100 return this; 101 } 102 103 @Override 104 public SimpleJdbcInsertOperations withoutTableColumnMetaDataAccess() { 105 setAccessTableColumnMetaData(false); 106 return this; 107 } 108 109 @Override 110 public SimpleJdbcInsertOperations includeSynonymsForTableColumnMetaData() { 111 setOverrideIncludeSynonymsDefault(true); 112 return this; 113 } 114 115 @Override 116 public int execute(Map<String, ?> args) { 117 return doExecute(args); 118 } 119 120 @Override 121 public int execute(SqlParameterSource parameterSource) { 122 return doExecute(parameterSource); 123 } 124 125 @Override 126 public Number executeAndReturnKey(Map<String, ?> args) { 127 return doExecuteAndReturnKey(args); 128 } 129 130 @Override 131 public Number executeAndReturnKey(SqlParameterSource parameterSource) { 132 return doExecuteAndReturnKey(parameterSource); 133 } 134 135 @Override 136 public KeyHolder executeAndReturnKeyHolder(Map<String, ?> args) { 137 return doExecuteAndReturnKeyHolder(args); 138 } 139 140 @Override 141 public KeyHolder executeAndReturnKeyHolder(SqlParameterSource parameterSource) { 142 return doExecuteAndReturnKeyHolder(parameterSource); 143 } 144 145 @Override 146 @SuppressWarnings("unchecked") 147 public int[] executeBatch(Map<String, ?>... batch) { 148 return doExecuteBatch(batch); 149 } 150 151 @Override 152 public int[] executeBatch(SqlParameterSource... batch) { 153 return doExecuteBatch(batch); 154 } 155 156}