package com.ssm.jdbc;
import com.ssm.Account;
import java.util.List;
public interface AccountDao {
// 添加
public int addAccount(Account account);
// 更新
public int updateAccount(Account account);
// 删除
public int deleteAccount(int id);
// 通过id查询
public Account findAccountById(int id);
// 查询所有账户
public List<Account> findAllAccount();
// 转账
public void transfer(String outUser,String inUser,Double money);
}
先奉上整体代码,然后一条条解释。
import那里分别import了两个,分别是Account;和List,Account;是一个已经建好的含有setting和getting还有toString方法的一个类,List是一个数列,用于装住多个数组。
public interface AccountDao,凡是含有 interface的就是一个接口。用于其他程序接进来使用方法。
public int addAccount(Account account);这个代表添加Account的意思,有些人可能不明白,为什么有些方法用int,有些方法不用int,这个要看具体的方法而定,也就是说,你的方法如果涉及到增删改这些需要数据变动的方法,就用int,但是如果只是查询,就不需要用int。
Account account这个是什么意思呢,这个是Account类,再添加进一个account参数,这个该怎么理解呢,就是到时候我只需要添加account.setusername(“xx”);
account setid(“xx”);
然后再 int num = accountDao.addAccount(account);把这个account传进去,就可以实现数据传进去了。
那还有一个问题,就是为什么添加和更新都是(Account account),而删除却是(int id);呢?因为添加和更新都不仅仅需要username,id等等参数添加进去,还需要其他的信息增加进去,而delete只需要它的ID就可以添加进去,所以会有这样的不同。
public Account findAccountById(int id);这个是查找一个客户的信息,通过ID查找,因为这是再Account里面查找,所以一开始用了Account。而下面
public List findAllAccount();,因为这个是查找一堆用户的信息,所以用了泛型,List来装住,然后查找用户,这里就不需要用int id或者Account account了,因为这是查找一群用户信息,所以后面括号为空。
public void transfer(String outUser,String inUser,Double money);
转账比较好理解,就是定义输出的客户,然后从谁那里转出,转出多少,即可