合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## dao ~~~ package com.like.dao.impl; @Repository public class AccountDaoImpl implements IAccountDao { @Autowired private JdbcTemplate jt; public Account findAccountById(Integer id) { List<Account> accounts = jt.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), id); return accounts.isEmpty() ? null : accounts.get(0); } public Account findAccountByName(String name) { List<Account> accounts = jt.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), name); if (accounts.isEmpty()) { return null; } if (accounts.size() > 1) { throw new RuntimeException("结果集不唯一"); } return accounts.get(0); } public void updateAccount(Account account) { jt.update("update account set name = ? where id = ?", account.getName(), account.getId()); } } ~~~ ## 根据ID查找 ~~~ IAccountDao accountDaoImpl = (IAccountDao) context.getBean("accountDaoImpl"); Account account = accountDaoImpl.findAccountById(3); System.out.println(account); ~~~ ## 根据name查找 ~~~ System.out.println(accountDaoImpl.findAccountByName("jack")); ~~~ ## 更新 ~~~ IAccountDao accountDaoImpl = (IAccountDao) context.getBean("accountDaoImpl"); Account account = new Account(); account.setId(3); account.setName("milan"); accountDaoImpl.updateAccount(account); ~~~