public class Account {
private int id ;
private String name ;
private double money;
....省略了getter. setter
}
dao层
public interface IAccountDAO {
int add(Account account);
int update(Account account);
int delete(int id);
Account findAccountById(int id);
List<Account> findAccountList();
}
dao具体的实现类
@Repository // 注意加这个注解
public class AccountDaoImpl implements IAccountDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int add(Account account) {
return jdbcTemplate.update("insert into account(name, money) values(?, ?)",
account.getName(),account.getMoney());
}
@Override
public int update(Account account) {
return jdbcTemplate.update("UPDATE account SET NAME=? ,money=? WHERE id=?",
account.getName(),account.getMoney(),account.getId());
}
@Override
public int delete(int id) {
return jdbcTemplate.update("DELETE from TABLE account where id=?",id);
}
@Override
public Account findAccountById(int id) {
List<Account> list = jdbcTemplate.query("select * from account where id = ?", new Object[]{id}, new BeanPropertyRowMapper(Account.class));
if(list!=null && list.size()>0){
Account account = list.get(0);
return account;
}else{
return null;
}
}
@Override
public List<Account> findAccountList() {
List<Account> list = jdbcTemplate.query("select * from account", new Object[]{}, new BeanPropertyRowMapper(Account.class));
if(list!=null && list.size()>0){
return list;
}else{
return null;
}
}
}
service层
public interface IAccountService {
int add(Account account);
int update(Account account);
int delete(int id);
Account findAccountById(int id);
List<Account> findAccountList();
}
service具体实现类
@Service
public class AccountService implements IAccountService {
@Autowired
IAccountDAO accountDAO;
@Override
public int add(Account account) {
return accountDAO.add(account);
}
@Override
public int update(Account account) {
return accountDAO.update(account);
}
@Override
public int delete(int id) {
return accountDAO.delete(id);
}
@Override
public Account findAccountById(int id) {
return accountDAO.findAccountById(id);
}
@Override
public List<Account> findAccountList() {
return accountDAO.findAccountList();
}
}