jdbcTemplate.update()的幾種寫法 以及NamedParameterJdbcTemplate
阿新 • • 發佈:2018-12-28
api提供了update的幾種呼叫寫法
//方法一直接在sql中拼接好了引數之後呼叫即可 @Override public int update(final String sql) throws DataAccessException { Assert.notNull(sql, "SQL must not be null"); if (logger.isDebugEnabled()) { logger.debug("Executing SQL update [" + sql + "]"); } class UpdateStatementCallback implements StatementCallback<Integer>, SqlProvider { @Override public Integer doInStatement(Statement stmt) throws SQLException { int rows = stmt.executeUpdate(sql); if (logger.isDebugEnabled()) { logger.debug("SQL update affected " + rows + " rows"); } return rows; } @Override public String getSql() { return sql; } } return execute(new UpdateStatementCallback()); } //方法二 使用佔位符 多引數依次入參 @Override public int update(String sql, Object... args) throws DataAccessException { return update(sql, newArgPreparedStatementSetter(args)); } //方法三 實現回撥介面PreparedStatementSetter 重寫裡面的方法 @Override public int update(String sql, PreparedStatementSetter pss) throws DataAccessException { return update(new SimplePreparedStatementCreator(sql), pss); } //方法四 添加了第兩個引數陣列,用於顯式的指定每個佔位符所對應的欄位資料型別 @Override public int update(String sql, Object[] args, int[] argTypes) throws DataAccessException { return update(sql, newArgTypePreparedStatementSetter(args, argTypes)); }
@Autowired private JdbcTemplate jdbcTemplate; @Transactional(rollbackFor=Exception.class) public void updateManegerSub(UpdateManagerParam updateManagerParam){ StringBuffer sql = new StringBuffer("UPDATE pub_user SET account = ? ,phone = ? ,email = ? ," + "update_date = NOW() ,user_name = ? " ); if (StringUtils.isNotEmpty(updateManagerParam.getPassword())){ sql.append(" ,password = '" + updateManagerParam.getPassword() +"'"); } sql.append("WHERE user_id = ? AND del_flag = ?"); //方法一,將sql語句中的引數全部按照密碼欄位一樣拼接好之後,直接呼叫此方法 jdbcTemplate.update(sql.toString()); //方法二 使用佔位符 多引數依次入參 jdbcTemplate.update(sql.toString() , updateManagerParam.getAccount(),updateManagerParam.getPhone(),updateManagerParam.getEmail(), updateManagerParam.getUserName(), updateManagerParam.getUserId(),UserEntity.DEL_FLAG_NORMAL); //方法三 實現回撥介面PreparedStatementSetter 重寫裡面的方法 jdbcTemplate.update(sql.toString(), new PreparedStatementSetter() { @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1,updateManagerParam.getAccount()); ps.setString(2,updateManagerParam.getPhone()); ps.setString(3,updateManagerParam.getEmail()); ps.setString(4,updateManagerParam.getUserName()); ps.setLong(5,updateManagerParam.getUserId()); ps.setString(6,UserEntity.DEL_FLAG_NORMAL); } }); //方法四 添加了第兩個引數陣列,用於顯式的指定每個佔位符所對應的欄位資料型別 jdbcTemplate.update(sql.toString(), new Object[]{updateManagerParam.getAccount(),updateManagerParam.getPhone(),updateManagerParam.getEmail(),updateManagerParam.getUserName(),updateManagerParam.getUserId(),UserEntity.DEL_FLAG_NORMAL}, new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.BIGINT,Types.VARCHAR,} ); }
若使用引數繫結,則需要使用 namedParameterJdbcTemplate https://blog.csdn.net/u013468917/article/details/52219849
@Autowired private NamedParameterJdbcTemplate namedParameterJdbcTemplate; @Transactional(rollbackFor=Exception.class) public void updateManegerSub(UpdateManagerParam updateManagerParam,Long roleId){ Map<String ,Object> params = new HashMap<>(); StringBuffer sql = new StringBuffer("UPDATE pub_user SET account = :account ,phone = :phone ,email = :email , " + "update_date = NOW() ,user_name = :user_name " ); params.put("account",updateManagerParam.getAccount()); params.put("phone",updateManagerParam.getPhone()); params.put("email",updateManagerParam.getEmail()); params.put("user_name",updateManagerParam.getUserName()); if (StringUtils.isNotEmpty(updateManagerParam.getPassword())){ sql.append(" ,password = :password "); params.put("password",updateManagerParam.getPassword()); } sql.append("WHERE user_id = :user_id AND del_flag = :del_flag "); params.put("user_id",updateManagerParam.getUserId()); params.put("del_flag",UserEntity.DEL_FLAG_NORMAL); namedParameterJdbcTemplate.update(sql.toString(),params); }