首页 > 代码库 > Spring JDBC模板惯用方式
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
Spring JDBC模板惯用方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。