use of org.springframework.jdbc.core.PreparedStatementCreator in project disconf by knightliao.
the class GenericDao method createAndFetchUpdateRow.
// FIXME
@SuppressWarnings("unchecked")
private int createAndFetchUpdateRow(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) {
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;
}
use of org.springframework.jdbc.core.PreparedStatementCreator in project dal by ctripcorp.
the class DaoOfLoginUser method insertUser.
public int insertUser(final LoginUser user) {
if (user == null || user.getUserNo() == null) {
return -1;
}
KeyHolder holder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement("INSERT INTO login_users ( user_no, user_name, user_email, password ) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE user_no = ?", Statement.RETURN_GENERATED_KEYS);
ps.setString(1, user.getUserNo());
ps.setString(2, user.getUserName());
ps.setString(3, user.getUserEmail());
ps.setString(4, user.getPassword());
ps.setString(5, user.getUserNo());
return ps;
}
}, holder);
return holder.getKey().intValue();
}
use of org.springframework.jdbc.core.PreparedStatementCreator in project dal by ctripcorp.
the class DaoOfUserProject method insertUserProject.
public int insertUserProject(final UserProject data) {
KeyHolder holder = new GeneratedKeyHolder();
this.jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement("INSERT INTO user_project (project_id, user_no ) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, data.getProject_id());
ps.setString(2, data.getUserNo());
return ps;
}
}, holder);
return holder.getKey().intValue();
}
Aggregations