第一种:传入sql和参数值,不返回主键
public Integer save(String sql, final Object... args){
return getJdbcTemplate().update(sql,args);
}
或者
KeyHolder key = new GeneratedKeyHolder();
PreparedStatementCreator psc = new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con)
throws SQLException {
PreparedStatement ps = con.prepareStatement(addSql,PreparedStatement.RETURN_GENERATED_KEYS);
ps.setString(1, obj.getType());
ps.setString(2, obj.getUser_id());
ps.setDouble(3, obj.getCf_amt());
ps.setString(4, obj.getCreate_tm());
ps.setString(5, obj.getYbid());
ps.setString(6,obj.getPbchannel());
return ps;
}
};
getJdbcTemplate().update(psc,key);
int cf_id = key.getKey().intValue();
obj.setCf_id(cf_id);
第二种:
传入sql和参数值,返回主键
public Integer save(final String sql, final Object... args){
KeyHolder keyHolder = new GeneratedKeyHolder();
return getJdbcTemplate().update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection coon)
throws SQLException {
PreparedStatement ps = coon.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1, args[i]);
}
return ps;
}
}, keyHolder);
}
第三种:传入sql、参数值、参数类型
public Integer save(String sql,int[] argTypes,Object... args){
return getJdbcTemplate().update(sql, args, argTypes);
}
转自: https://blog.csdn.net/u011983531/article/details/48858363