Spring JDBC模板惯用方式
Spring JDBC模板惯用方式 @Resource private NamedParameterJdbcTemplate jdbcTemplate; @Resource private JdbcTemplate jdbcTemplateSimple; 1、插入并获取自增主键 KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplateSimple.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(insert_sql, Statement.RETURN_GENERATED_KEYS); ps.setLong(1, 2L); ps.setString(6, "test"); return ps; } }, keyHolder); long id = keyHolder.getKey().longValue(); 2、查询返回结果集List<Map<String, Object>> List<Map<String, Object>> list = jdbcTemplateSimple.queryForList("select * from test where code=? and state=?", "Y001", 1); 3、根据Map参数来查询结果集,并封装成对应的Bean Map<String,Object> map = new HashMap(); map.put("code","Y001"); map.put("state",1); List<TestBean> beanList = jdbcTemplate.query("select * from test where code=:code and state=:state", map, new RowMapper<TestBean>() { @Override public BidItemBean mapRow(ResultSet rs, int i) throws SQLException { TestBean b = new TestBean(); b.setId(rs.getLong("id")); b.setCode(rs.getString("code")); b.setState(rs.getString("state")); return b; } }); 4、更新删除: jdbcTemplateSimple.update("delete from test where product_id=? and picking_center_id=?", "Y001", 1);
本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/1579956
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。