🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 保存 ~~~ jt.update("insert into account (name,money) values (?,?)","Milan",200); ~~~ ## 更新 ~~~ jt.update("update account set name = ? where id = ?","jack",1); ~~~ ## 删除 ~~~ jt.update("delete from account where id = ?",1); ~~~ ## 查询所有 写一个累实现RowMapper接口,并实现方法. ~~~ class AccountRowMapper implements RowMapper<Account> { public Account mapRow(ResultSet resultSet, int i) throws SQLException { Account account = new Account(); account.setId(resultSet.getInt("id")); account.setName(resultSet.getString("name")); account.setMoney(resultSet.getFloat("money")); return account; } } ~~~ 查询: ~~~ //获取容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取数据源 JdbcTemplate jt = (JdbcTemplate) context.getBean("jdbcTemplate"); //执行SQL List<Account> list = jt.query("select * from account where money > ?", new AccountRowMapper(), 1000f); System.out.println(list); ~~~ 每次自己写实现接口太麻烦了,spring给我也提供了和queryRunner一样的结果集处理的封装. ~~~ //获取容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取数据源 JdbcTemplate jt = (JdbcTemplate) context.getBean("jdbcTemplate"); //执行SQL List<Account> list = jt.query("select * from account where money > ?", new BeanPropertyRowMapper<Account>(Account.class), 1000f); System.out.println(list); ~~~ ## 查询一个 ~~~ List<Account> list = jt.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), 3); ~~~ ## 查询返回一行一列 ~~~ Integer count = jt.queryForObject("select count(*) from account", Integer.class); ~~~