1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| /*编写sql语句*/ String sql = "update user set password = 123456 where id = ? "; /*执行*/ /*queryForMap()*/ Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(sql,"?的值");
/*queryForList()*/ List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, "?的值");
/*queryForObject()*/ /*自定义返回值*/ jdbcTemplate.queryForObject(sql,long.class);
/*query()*/ /*直接赋值,要注意数据库查询的列名与实体类的属性名一致. Java属性的命名应遵循camelCase风格,如:myPassword * 对应的数据库命名可以为 my_password 或 mypassword*/ List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
/*如果名字不一致,定义一个新的类来重写自己的规则*/ List<User> userList1 = jdbcTemplate.query(sql, new RowMapper<User>() { @Override public User mapRow(ResultSet resultSet, int i) throws SQLException { User user = new User(); user.setId(resultSet.getInt("id")); user.setUsername(resultSet.getString("username")); user.setPassword(resultSet.getString("password")); return user; } });
|