标签: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
原文地址:http://lavasoft.blog.51cto.com/62575/1579956