JdbcTemplate简单使用范例
// 查询单个键(后面的class只能是基本类型) String name = jdbcTemplate.queryForObject("select name from table where id='08'", String.class); // 查询单个对象 Map<String, Object> map = jdbcTemplate.queryForMap("select * from table where id=?", "08"); // 查询集合 List<Map<String, Object>> mlist = jdbcTemplate.queryForList("select * from table where id=? and name=?", "1", "张三"); maplist.get(0).get("password"); // 查询集合(返回对象) List<Demo> demo = jdbcTemplate.query("select * from table where type='a'", new BeanPropertyRowMapper(Demo.class)); // 增删改 /int conut = jdbcTemplate.update("update table set name=?, status=? where id=?", "安全", 0, "08"); // 批量增删改 List<Object[]> sql = new ArrayList<>(); sql.add(new Object[]{"安全风险防控", 2, "008"}); sql.add(new Object[]{"安全风险防控", 0, "008"}); sql.add(new Object[]{"安全风险防控", 2, "008"}); int[] conuts1 = jdbcTemplate.batchUpdate("update table set name=?, status=? where id=?", sql); // 批量增删改 int[] conuts2 = jdbcTemplate.batchUpdate("insert into table (id, name) values ('1', '课程一')", "insert into table (id, name) values ('2', '课程二')");
发布者:songJian 点击数:1269 发布时间:2018-07-17 11:27:48 更新时间:2022-12-14 16:05:04
正在加载评论...