本文整理汇总了Java中org.springframework.jdbc.support.KeyHolder.getKey方法的典型用法代码示例。如果您正苦于以下问题:Java KeyHolder.getKey方法的具体用法?Java KeyHolder.getKey怎么用?Java KeyHolder.getKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.jdbc.support.KeyHolder
的用法示例。
在下文中一共展示了KeyHolder.getKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: add
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
@Override
public Object add(Object object) {
SqlModel<Object> sqlModel = sqlBuilder.insertSelectiveSql(object);
checkSqlModel(sqlModel);
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(object);
KeyHolder generatedKeyHolder = new GeneratedKeyHolder();
namedPjdbcTemplate.update(sqlModel.getSql(), paramSource, generatedKeyHolder);
Number num = generatedKeyHolder.getKey();
String[] primaryKeys = sqlModel.getPrimaryKeys();
if(primaryKeys != null && primaryKeys.length > 0){
BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(object);
beanWrapper.setPropertyValue(primaryKeys[0], num);
}
return object;
}
示例2: saveVisitorAndComment
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
public boolean saveVisitorAndComment(final Comment comment){
jdbcTemplate = new JdbcTemplate(dataSource);
final String sql = "INSERT INTO visitors(VisitorName,Email) VALUES(?,?)";
KeyHolder keyHolder = new GeneratedKeyHolder();
int result = jdbcTemplate.update( new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement pst = con.prepareStatement(sql, new String[] {"Id"});
pst.setString(1, comment.getName());
pst.setString(2, comment.getEmail());
return pst;
}
}, keyHolder);
final Long lastId = (Long)keyHolder.getKey();
if(result != 0){
comment.setCommentatorId(lastId.intValue());
return saveVisitor(comment);
} else {
return false;
}
}
示例3: insert
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
@Override
public long insert(Map<String,Object> paramMap, String sql) {
logger.debug("Insert sql : \n" + sql);
logger.debug("Paramters : \n" + paramMap);
try {
KeyHolder keyHolder = new GeneratedKeyHolder();
this.ownJdbcTemplate.getJdbcOperations().update(getPreparedStatementCreator(sql,paramMap), keyHolder);
if(keyHolder.getKey() == null) {
return -1;
}
return keyHolder.getKey().longValue();
} catch (DataAccessException ex) {
logger.error(ex.getMessage(), ex);
throw ex;
}
}
示例4: save
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
@Override
public Serializable save(Object object) {
SqlModel<Object> sqlModel = sqlBuilder.insertSelectiveSql(object);
checkSqlModel(sqlModel);
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(object);
KeyHolder generatedKeyHolder = new GeneratedKeyHolder();
namedPjdbcTemplate.update(sqlModel.getSql(), paramSource, generatedKeyHolder);
return generatedKeyHolder.getKey();
}
示例5: insert
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
/**@author [email protected]*/
public static final Long insert(
JdbcOperations jdbcOperations,
final String sql,
final List<Object> params) {
KeyHolder keyHolder = new GeneratedKeyHolder();
try {
Logger.getLogger("org.dazzle.util").debug("sql ==>" + sql.toString());
Logger.getLogger("org.dazzle.util").debug("args==>" + params);
jdbcOperations.update(
new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
setParams(ps, params);
return ps;
}
}, keyHolder);
if(null != keyHolder.getKey()) {
return keyHolder.getKey().longValue();
}
} catch (RuntimeException e) {
catchException(e);
}
return null;
}
示例6: createTask
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
@Override
public int createTask(String annotatorName, String datasetName, String experimentType, String matching,
String experimentId) {
MapSqlParameterSource params = createTaskParameters(annotatorName, datasetName, experimentType, matching);
params.addValue("state", ExperimentDAO.TASK_STARTED_BUT_NOT_FINISHED_YET);
java.util.Date today = new java.util.Date();
params.addValue("lastChanged", new java.sql.Timestamp(today.getTime()));
params.addValue("version", GerbilConfiguration.getGerbilVersion());
KeyHolder keyHolder = new GeneratedKeyHolder();
this.template.update(INSERT_TASK, params, keyHolder);
Integer generatedKey = (Integer) keyHolder.getKey();
if (experimentId != null) {
connectToExperiment(experimentId, generatedKey);
}
// FIXME remove this method and implement a better version handling
setVersion(generatedKey);
return generatedKey;
}
示例7: insert
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
public Number insert(final QueryVersion queryVersion) {
final String sql = "INSERT INTO query_versions (content, version, is_current_version, query_id) VALUES (?, ?, ?, ?)";
jdbcTemplate = new JdbcTemplate(dataSource);
KeyHolder keyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection)
throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(sql, new String[] { "id" });
preparedStatement.setString(1, queryVersion.getContent());
preparedStatement.setInt(2, queryVersion.getVersion());
preparedStatement.setBoolean(3, queryVersion.getIsCurrentVersion());
preparedStatement.setInt(4, queryVersion.getQueryId());
return preparedStatement;
}
};
jdbcTemplate.update(
preparedStatementCreator,
keyHolder
);
return keyHolder.getKey();
}
示例8: insertNewRun
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
public static long insertNewRun(int whEtlJobId) {
Map<String, Object> params = new HashMap<>();
params.put("whEtlJobId", whEtlJobId);
params.put("status", EtlJobStatus.REQUESTED.toString());
params.put("requestTime", System.currentTimeMillis() / 1000);
KeyHolder keyHolder = JdbcUtil.insertRow(INSERT_NEW_RUN, params);
return (Long) keyHolder.getKey();
}
示例9: executeInsertAndReturnKeyInternal
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
/**
* Method to execute the insert generating single key
*/
private Number executeInsertAndReturnKeyInternal(final List<Object> values) {
KeyHolder kh = executeInsertAndReturnKeyHolderInternal(values);
if (kh != null && kh.getKey() != null) {
return kh.getKey();
}
else {
throw new DataIntegrityViolationException(
"Unable to retrieve the generated key for the insert: " + getInsertString());
}
}
示例10: executeInsertAndReturnKeyInternal
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
/**
* Delegate method to execute the insert, generating a single key.
*/
private Number executeInsertAndReturnKeyInternal(final List<?> values) {
KeyHolder kh = executeInsertAndReturnKeyHolderInternal(values);
if (kh != null && kh.getKey() != null) {
return kh.getKey();
}
else {
throw new DataIntegrityViolationException(
"Unable to retrieve the generated key for the insert: " + getInsertString());
}
}
示例11: insertNewRun
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
public static long insertNewRun(String etlJobName) {
Map<String, Object> params = new HashMap<>();
params.put("whEtlJobName", etlJobName);
params.put("status", JobStatus.REQUESTED.toString());
params.put("requestTime", System.currentTimeMillis() / 1000);
KeyHolder keyHolder = JdbcUtil.insertRow(INSERT_NEW_RUN, params);
return (Long) keyHolder.getKey();
}
示例12: insert
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
/**
* {@link RepositoryOperations#insert}
*/
public <S extends T> S insert(S entity) {
SqlBuilder sqlBuilder = getSqlBuilder();
Map<String,Object> mappings = rowUnmapper.mapColumns(entity);
final String sql = sqlBuilder.insert(mappings).toSql();
final Object[] values = sqlBuilder.getQueryParameterValues().toArray();
final String[] columns = mappings.keySet().toArray(new String[]{});
sqlBuilder.insert(mappings);
S created = null;
if (entity.getId() == null){
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override public PreparedStatement createPreparedStatement(Connection connection)
throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(sql, columns);
for (int i = 0; i < values.length; i++){
preparedStatement.setObject(i+1, values[i]);
}
return preparedStatement;
}
}, keyHolder);
ID newId = (ID) keyHolder.getKey();
created = (S) findOne(newId);
} else {
jdbcTemplate.update(sql, values);
created = entity;
}
return created;
}
示例13: createAndFetchKey
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
/**
* @param entity
* @param sql
* @param params
*/
@SuppressWarnings("unchecked")
private ENTITY createAndFetchKey(ENTITY entity, final String sql, final List<Object> params) {
KeyHolder keyHolder = new GeneratedKeyHolder();
recordLog(sql);
// 执行操作
int rowCount = this.jdbcTemplate.update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql);
int index = 1;
for (Object param : params) {
ps.setObject(index++, param);
}
return ps;
}
}, keyHolder);
// 如果插入成功则获取keyHolder中的key
if (rowCount != 0 && keyHolder.getKey() != null) {
Class<KEY> keyClass = orMapping.getKeyClass();
if (keyClass.equals(Integer.class)) {
entity.setId((KEY) Integer.valueOf(keyHolder.getKey().intValue()));
} else if (keyClass.equals(Long.class)) {
entity.setId((KEY) Long.valueOf(keyHolder.getKey().longValue()));
}
}
return rowCount == 1 ? entity : null;
}
示例14: insert
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
public Number insert(final Query query) {
final String sql = "INSERT INTO queries (key, database_connection_id) VALUES (?, ?)";
query.setKey(StringUtil.hashWithSha256(new Double(Math.random()).toString() + System.currentTimeMillis()));
jdbcTemplate = new JdbcTemplate(dataSource);
KeyHolder keyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection)
throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(sql, new String[] { "id" });
preparedStatement.setString(1, query.getKey());
preparedStatement.setInt(2, query.getDatabaseConnectionId());
return preparedStatement;
}
};
jdbcTemplate.update(
preparedStatementCreator,
keyHolder
);
QueryVersion queryVersion = new QueryVersion();
queryVersion.setQueryId(keyHolder.getKey().intValue());
queryVersion.setContent(query.getContent());
queryVersion.setIsCurrentVersion(true);
queryVersion.setVersion(1);
queryVersionDAO.insert(queryVersion);
return keyHolder.getKey();
}
示例15: saveBlog
import org.springframework.jdbc.support.KeyHolder; //导入方法依赖的package包/类
@Override
public boolean saveBlog(final Blog blog) {
jdbcTemplate = new JdbcTemplate(dataSource);
/*
String sql = "INSERT INTO blogs(UserId, BlogUrl, BlogName, CategoryId) VALUES(?,?,?,?)";
Object[] args =new Object[]{blog.getUser().getId(),blog.getBlogUrl(),blog.getBlogName(),blog.getCategoryId()};
int result = jdbcTemplate.update(sql, args);
if(result != 0){
return true;
} else {
return false;
}
*/
final String sql = "INSERT INTO blogs(UserId, BlogUrl, BlogName, CategoryId) VALUES(?,?,?,?)";
KeyHolder keyHolder = new GeneratedKeyHolder();
int result = jdbcTemplate.update( new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement pst = con.prepareStatement(sql, new String[] {"Id"});
pst.setInt(1, blog.getUser().getId());
pst.setString(2, blog.getBlogUrl());
pst.setString(3, blog.getBlogName());
pst.setInt(4, blog.getCategoryId());
return pst;
}
}, keyHolder);
final Long lastId = (Long)keyHolder.getKey();
if(result != 0){
List<Menu> menus = new ArrayList<Menu>();
Menu m = new Menu(){
{
setId(1);
setBlogId(lastId.intValue());
}
};
menus.add(m);
addBlogMenus(menus);
return true;
} else {
return false;
}
}