jdbcTemplate.update()的⼏种写法以及
NamedParameterJdb。。。
api提供了update的⼏种调⽤写法
//⽅法⼀直接在sql中拼接好了参数之后调⽤即可
@Override
public int update(final String sql) throws DataAccessException {
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 = uteUpdate(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, 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));
}
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.Password())){
sql.append(" ,password = '" + Password() +"'");
}
sql.append("WHERE user_id = ? AND del_flag = ?");
//⽅法⼀,将sql语句中的参数全部按照密码字段⼀样拼接好之后,直接调⽤此⽅法
jdbcTemplate.String());
//⽅法⼆使⽤占位符多参数依次⼊参
jdbcTemplate.String() ,
//⽅法三实现回调接⼝PreparedStatementSetter 重写⾥⾯的⽅法
jdbcTemplate.String(), new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(Account());
ps.setString(Phone());
ps.setString(Email());
ps.setString(UserName());
ps.setLong(UserId());
ps.setString(6,UserEntity.DEL_FLAG_NORMAL);sql中update什么意思
}
});
//⽅法四添加了第两个参数数组,⽤于显式的指定每个占位符所对应的字段数据类型
jdbcTemplate.String(),
new Object[]{Account(),Phone(),Email(),UserN new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.BIGINT,Types.VARCHAR,}
);
}
若使⽤参数绑定,则需要使⽤ namedParameterJdbcTemplate
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",Account());
params.put("phone",Phone());
params.put("email",Email());
params.put("user_name",UserName());
if (StringUtils.Password())){
sql.append(" ,password = :password ");
params.put("password",Password());
}
sql.append("WHERE user_id = :user_id AND del_flag = :del_flag ");
params.put("user_id",UserId());
params.put("del_flag",UserEntity.DEL_FLAG_NORMAL);
namedParameterJdbcTemplate.String(),params);
}
@Override
public List<OrgSubResponse> fetchAllOrgs() {
Long userId = PsUserId();
Long managerOrgId = orgManagerDao.fetchOrgIdByManagerId(userId);
if (managerOrgId == null) {
throw new LogicException(UomConstants.ORG_NOT_MANAGER_ERROR);
}
String sql = "SELECT org_id AS id, org_name AS name, parent_id AS pId FROM pub_org pu " +
" WHERE pu.parent_ids LIKE CONCAT('%/', :orgId, '/%') AND pu.del_flag = '0' " +
" ORDER BY IFNULL(pu.sort, 2100000000) ASC ";
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("orgId", managerOrgId);
List<OrgSubResponse> orgSubResponses = namedParameterJdbcTemplate.query(sql, paramMap, new BeanPropertyRowMapper<OrgSubResponse>(Org if (orgSubResponses == null) {
return null;
}
for (OrgSubResponse orgSubResponse : orgSubResponses) {
if (Id().equals(managerOrgId)) {
orgSubResponse.setpId(0L);
}
}
return orgSubResponses;
}
package comtop.map.store.;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(description = "取部门下所有⼦部门,和选部门组件联⽤") public class OrgSubResponse {
@ApiModelProperty(value = "code")
private String code;
@ApiModelProperty(value = "icon")
private String icon;
@ApiModelProperty(value = "name")
private String name;
@ApiModelProperty(value = "code")
private String parentIds;
@ApiModelProperty(value = "fullName")
private String fullName;
@ApiModelProperty(value = "pId")
private Long pId;
@ApiModelProperty(value = "id")
private Long id;
public String getCode() {
return code;
}
public void setCode(String code) {
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParentIds() {
return parentIds;
}
public void setParentIds(String parentIds) {
this.parentIds = parentIds;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) { this.fullName = fullName;
}
public Long getpId() {
return pId;
}
public void setpId(Long pId) {
this.pId = pId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论