當前位置: 首頁>>代碼示例>>Java>>正文


Java KeyHolder.getKey方法代碼示例

本文整理匯總了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;
}
 
開發者ID:thinking-github,項目名稱:nbone,代碼行數:19,代碼來源:NamedJdbcDao.java

示例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;
		}
	}
 
開發者ID:eyupgevenim,項目名稱:SpringMvcBlog,代碼行數:27,代碼來源:PostDaoImpl.java

示例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;
    }
}
 
開發者ID:javen-hao,項目名稱:osmp,代碼行數:17,代碼來源:BaseTemplate.java

示例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();
}
 
開發者ID:thinking-github,項目名稱:nbone,代碼行數:11,代碼來源:NamedJdbcDao.java

示例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;
}
 
開發者ID:hcqt,項目名稱:dazzle,代碼行數:27,代碼來源:SpringDaoUtils.java

示例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;
}
 
開發者ID:dice-group,項目名稱:gerbil,代碼行數:19,代碼來源:ExperimentDAOImpl.java

示例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();
}
 
開發者ID:BjoernKW,項目名稱:ZenQuery,代碼行數:27,代碼來源:JdbcQueryVersionDAO.java

示例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();
}
 
開發者ID:thomas-young-2013,項目名稱:wherehowsX,代碼行數:9,代碼來源:EtlJobDao.java

示例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());
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:14,代碼來源:AbstractJdbcInsert.java

示例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());
	}
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:14,代碼來源:AbstractJdbcInsert.java

示例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();
}
 
開發者ID:linkedin,項目名稱:WhereHows,代碼行數:9,代碼來源:EtlJobDao.java

示例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;
}
 
開發者ID:oncoblocks,項目名稱:centromere,代碼行數:34,代碼來源:GenericJdbcRepository.java

示例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;
}
 
開發者ID:knightliao,項目名稱:disconf,代碼行數:36,代碼來源:GenericDao.java

示例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();
}
 
開發者ID:BjoernKW,項目名稱:ZenQuery,代碼行數:34,代碼來源:JdbcQueryDAO.java

示例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;
	}
}
 
開發者ID:eyupgevenim,項目名稱:SpringMvcBlog,代碼行數:53,代碼來源:BlogDaoImpl.java


注:本文中的org.springframework.jdbc.support.KeyHolder.getKey方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。