您好,欢迎您访问我们燿动吧 – 知识分享,快乐你我,燿动青春!
Spring jdbcTemplate 常用操作示例
文章来源:白曼 时间:2025-03-11
注进须要的模版对于象
@ResourceprivateNamedParameterJdbcTemplatejdbcTemplate;@ResourceprivateJdbcTemplatejdbcTemplateSimple;拔出并获得自删主键
经由过程里面类去完成,传进接纳前往值的对于象:
KeyHolderkeyHolder=newGeneratedKeyHolder();jdbcTemplateSimple.update(newPreparedStatementCreator(){@OverridepublicPreparedStatementcreatePreparedStatement(Connectionconnection)throwsSQLException{PreparedStatementps=connection.prepareStatement(insert_sql,Statement.RETURN_GENERATED_KEYS);ps.setLong(1,2L);ps.setString(6,"test");returnps;}},keyHolder);longid=keyHolder.getKey().longValue();查问前往了局散List
那是1个经常使用操纵,普通正在应用JdbcTemplate时皆是前往如许的数据
List<Map<String,Object>>list=jdbcTemplateSimple.queryForList("select*fromtestwherecode=?andstate=?","X001",1);凭据Map参数去看望,并将了局散启拆成对于应的Bean
Map<String,Object>map=newHashMap();map.put("code","X001");map.put("state",1);List<TestBean>beanList=jdbcTemplate.query("select*fromtestwherecode=:codeandstate=:state",map,newRowMapper<TestBean>(){@OverridepublicBidItemBeanmapRow(ResultSetrs,inti)throwsSQLException{TestBeanb=newTestBean();b.setId(rs.getLong("id"));b.setCode(rs.getString("code"));b.setState(rs.getString("state"));returnb;}});革新减少
皆得用的操纵,复杂饬令1止弄定
jdbcTemplateSimple.update("deletefromtestwherea=?andb=?","X001",1);推举您浏览更多相关于“ 模版springJdbcTemplate自删主键 ”的作品
文章推荐
Copyright © 2024-2025 燿动吧 – 知识分享,快乐你我,燿动青春 http://www.yaodong8.com All Rights Reserved 网站地图