Map<String, Object> map = new HashMap<String, Object>(); map.put("column1", "test"); map.put("column2", Boolean.TRUE); SimpleJdbcInsert insert = new SimpleJdbcInsert(template).withTableName("table").usingGeneratedKeyColumns("id"); int id = insert.executeAndReturnKey(map).intValue();
public Long add(final JobConfiguration jobConfiguration, final String applicationName) { final Map<String, ?> keyValues = this.map(jobConfiguration, applicationName); final Number key = this.simpleJdbcInsert.executeAndReturnKey(keyValues); return key.longValue(); }
@Override @Deprecated public long insertMapGetGeneratedKey(String tableName, String keyName, Map<String, Object> mapParameter) { Assert.notNull(tableName, "表名不能为空!"); Assert.notNull(keyName, "自增字段名称不能为空!"); Assert.notNull(mapParameter, "对象map不能为空!"); jdbcInsert = jdbcInsert.withTableName(tableName).usingGeneratedKeyColumns(keyName); Number number = jdbcInsert.executeAndReturnKey(mapParameter); logger.info("生成 Key 为:[{}]:", number); return number.longValue(); }
@Override public int insert(Message message) { /* Map<String, Object> values = new HashMap<>(); values.put("NAME", message.getName()); values.put("message", message.getMessage()); values.put("creationTime", new Timestamp(message.getCreationTime().getTime())); Number genKey = simpleInsert.executeAndReturnKey(values); */ BeanPropertySqlParameterSource paramSource = new BeanPropertySqlParameterSource(message); Number genKey = simpleInsert.executeAndReturnKey(paramSource); return genKey.intValue(); }
public class LoginDAOImpl implements LoginDAO { private JdbcTemplate jdbcTemplate; private SimpleJdbcInsert insertPerson; public void setDataSource(DataSource dataSource) { jdbcTemplate = new JdbcTemplate(dataSource); insertPerson = new SimpleJdbcInsert(dataSource).withTableName("PERSON") .usingGeneratedKeyColumns("PERSON_ID"); } @Override public void addPerson(Person person) { SqlParameterSource parameters = new BeanPropertySqlParameterSource( person); Number newId = insertPerson.executeAndReturnKey(parameters); String sql = "INSERT INTO PERSON_ROLE (PERSON_ID, ROLE) VALUES (?, ?)"; jdbcTemplate.update(sql, newId.longValue(), person.getPersonRole().getRole()); } }
@Override public void insert(PaymentInfo paymentInfo) { Map<String, Object> paramValueMap = new HashMap<String, Object>(); paramValueMap.put("PRICE", paymentInfo.getPrice()); Number genId = insert.executeAndReturnKey(paramValueMap); paymentInfo.setId(genId.intValue()); }
@Override @Deprecated public long insertBeanGetGeneratedKey(String tableName, String keyName, Object beanParameter) { Assert.notNull(tableName, "表名不能为空"); Assert.notNull(keyName, "自增字段名称不能为空"); Assert.notNull(beanParameter, "对象bean不能为空"); jdbcInsert = jdbcInsert.withTableName(tableName).usingGeneratedKeyColumns(keyName); Number number = jdbcInsert.executeAndReturnKey(generateBeanSqlParamSource(beanParameter)); logger.info("生成 Key 为:[{}]:", number); return number.longValue(); }
public Long go() { Map<String, Object> params = new HashMap<>(); params.put("name", conference.getName()); params.put("slug", conference.getSlug()); params.put("description", conference.getDescription()); params.put("started_date", new java.sql.Timestamp(conference .getStartedDate().getTime())); params.put("ended_date", new java.sql.Timestamp(conference .getEndedDate().getTime())); return super.executeAndReturnKey(params).longValue(); }
@Override public void insert(PurchaseOrder order) { Map<String, Object> args = new HashMap<String, Object>(); args.put("ITEM_ID", order.getItemId()); args.put("PAYMENT_INFO_ID", order.getPaymentInfoId()); args.put("ADDRESS", order.getAddress()); Number genId = insert.executeAndReturnKey(args); order.setId(genId.intValue()); }
@Override public void insert(E entity) { Number key = jdbcInsert.executeAndReturnKey(createSqlParameterSource(entity)); entityMeta.fillIdValue(entity, key); }
@Transactional public class Test { @Autowired DataSource ds; public void test1() throws Exception { Map<String, Object> params = new HashMap<String, Object>(); params.put("c1", "test"); SimpleJdbcInsert insert = new SimpleJdbcInsert(ds).withTableName("t1").usingColumns("c1") .usingGeneratedKeyColumns("id"); long id = insert.executeAndReturnKey(params).longValue(); params = new HashMap<String, Object>(); params.put("stuff", "stuff"); params.put("id_fk", id); SimpleJdbcInsert insert2 = new SimpleJdbcInsert(ds).withTableName( "table2").usingColumns("stuff", "id_fk"); insert2.execute(params); NamedParameterJdbcTemplate tmpl = new NamedParameterJdbcTemplate(ds); params = new HashMap<String, Object>(); params.put("id", id); String c1 = tmpl.queryForObject("select c1 from t1 where id = :id", params, String.class); }
public void add(final JobConfiguration jobConfiguration) { final Long jobConfigurationId = jobConfiguration.getJobConfigurationId(); final Map<String, Object> jobParameters = jobConfiguration.getJobParameters(); if (jobParameters != null) { for (final Map.Entry<String, Object> jobParameter : jobParameters.entrySet()) { final JobConfigurationParameter jobConfigurationParameter = this.createJobConfigurationParameter( jobParameter.getKey(), jobParameter.getValue()); final String key = jobConfigurationParameter.getParameterName(); final String value = jobConfigurationParameter.getParameterValue(); final Long clazzType = jobConfigurationParameter.getParameterType(); final Map<String, Object> parameters = new HashMap<>(); parameters.put(JobConfigurationParameterDomain.JOB_CONFIGURATION_ID, jobConfigurationId); parameters.put(JobConfigurationParameterDomain.PARAMETER_NAME, key); parameters.put(JobConfigurationParameterDomain.PARAMETER_TYPE, clazzType); parameters.put(JobConfigurationParameterDomain.PARAMETER_VALUE, value); this.simpleJdbcInsert.executeAndReturnKey(parameters); } } else { log.info("JobParameters null, nothing to map!"); } }
public class JdbcActorDao implements ActorDao { private SimpleJdbcTemplate simpleJdbcTemplate; private SimpleJdbcInsert insertActor; public void setDataSource(DataSource dataSource) { this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); this.insertActor = new SimpleJdbcInsert(dataSource) .withTableName("t_actor") .usingGeneratedKeyColumns("id"); } public void add(Actor actor) { Map<String, Object> parameters = new HashMap<String, Object>(2); parameters.put("first_name", actor.getFirstName()); parameters.put("last_name", actor.getLastName()); Number newId = insertActor.executeAndReturnKey(parameters); actor.setId(newId.longValue()); } // ... additional methods }
@Override public Integer insertProject(Project project) { MapSqlParameterSource params = new MapSqlParameterSource() .addValue("name", project.getName()) .addValue("datestarted", project.getDate()); return insertIntoProject.executeAndReturnKey(params).intValue(); }
@Override public Book createBook(Book bookToCreate) { final Number newBookId = this.bookInsert .executeAndReturnKey(createParameterSourceFor(bookToCreate)); bookToCreate.setId(newBookId.intValue()); this.log.infof("Book [%s] persisted for the first time", bookToCreate); return bookToCreate; }
@Override public Book createBook(Book bookToCreate) { final Number newBookId = this.bookInsert .executeAndReturnKey(createParameterSourceFor(bookToCreate)); bookToCreate.setId(newBookId.intValue()); this.log.infof("Book [%s] persisted for the first time", bookToCreate); return bookToCreate; }
@Override public Book createBook(Book bookToCreate) { final Number newBookId = this.bookInsert .executeAndReturnKey(createParameterSourceFor(bookToCreate)); bookToCreate.setId(newBookId.intValue()); this.log.infof("Book [%s] persisted for the first time", bookToCreate); return bookToCreate; }
@Override public Book createBook(Book bookToCreate) { final Number newBookId = this.bookInsert .executeAndReturnKey(createParameterSourceFor(bookToCreate)); bookToCreate.setId(newBookId.intValue()); this.log.infof("Book [%s] persisted for the first time", bookToCreate); return bookToCreate; }
@Override public Trigger addTrigger(Trigger trigger) { log.debug("addTrigger({})", trigger); try { validate(trigger); // if everything went ok so far, check access rights and perform additional custom validation if needed. final String uid = context.getAccessToken().getId(); userProfileHandler.validateTriggerAccess(uid, trigger); SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(getJdbcTemplate()); jdbcInsert.withTableName("triggers").usingGeneratedKeyColumns("ID"); Map<String, Object> parameters = new HashMap<>(); parameters.put("name", trigger.getName()); parameters.put("triggercondition", trigger.getTriggerCondition()); parameters.put("bps_id", trigger.getBusinessProcessId()); parameters.put("cnl_id", trigger.getChannelId()); parameters.put("ugo_uid", uid); Number key = jdbcInsert.executeAndReturnKey(new MapSqlParameterSource(parameters)); int id = key.intValue(); commitTransaction(); Trigger result = getTrigger(id); notifier.notifyCreate(result); return result; } catch (RegistryException e) { log.error(e.getDescription()); throw e; } catch (SQLException e) { log.error("addTrigger({})", trigger, e); throw new RegistryException(e, UNKNOWN_TECHNICAL); } }
@Override public void addAvailableResource(AvailableResource availableResource) { log.trace("addAvailableResources(availableResource: {}", availableResource); try { validate(availableResource); // if everything went ok so far, check access rights and perform additional custom validation if needed. final String uid = context.getAccessToken().getId(); userProfileHandler.validateAvailableResourceAccess(uid, availableResource); SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(getJdbcTemplate()); jdbcInsert.withTableName("available_resource").usingGeneratedKeyColumns("id"); Map<String, Object> parameters = new HashMap<>(); parameters.put("bps_id", availableResource.getBusinessProcessId()); parameters.put("uri", availableResource.getUri()); Number key = jdbcInsert.executeAndReturnKey(new MapSqlParameterSource(parameters)); // get the primary key of the inserted available resource int id = key.intValue(); log.debug("generated id {} for new availableResource", id); commitTransaction(); AvailableResource result = getAvailableResourceById(id); notifier.notifyCreate(result); } catch (RegistryException e) { log.error(e.getDescription()); throw e; } catch (SQLException e) { log.error("addAvailableResource({})", availableResource, e); throw new RegistryException("Error storing available resource " + availableResource, e, RegistryException.Type.UNKNOWN_TECHNICAL); } }