数据库表
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)character set utf8 collate utf8_general_ci;
insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);
账户实体类
/**
* 账户的实体类
*/
@Data
@ToString
public class Account implements Serializable{
private Integer id;
private String name;
private Float money;
}
账户持久层接口
/**
* 账户的持久层接口
*/
public interface AccountDao {
/**
* 查询所有
* @return
*/
List<Account> findAllAccount();
/**
* 查询一个
* @return
*/
Account findAccountById(Integer accountId);
/**
* 保存
* @param account
*/
int saveAccount(Account account);
/**
* 更新
* @param account
*/
int updateAccount(Account account);
/**
* 删除
* @param accountId
*/
int deleteAccount(Integer accountId);
}
账户持久层接口实现类
/**
* 账户的持久层实现类
*/
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired
private QueryRunner runner;
private List<Account> list;
private int res = 0;
public List<Account> findAllAccount() {
try {
list = runner.query("select * from account", new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public Account findAccountById(Integer accountId) {
try {
return runner.query("select * from account where id=?", accountId,new BeanHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public int saveAccount(Account account) {
try {
res = runner.update("insert into account (name,money) values(?,?)", account.getName(), account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
return res;
}
}
public int updateAccount(Account account) {
try {
res = runner.update("update account set name=?,money=? where id=?", account.getName(),account.getMoney(),account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
return res;
}
}
public int deleteAccount(Integer accountId) {
try {
res = runner.update("delete from account where id=?",accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
return res;
}
}
}
账户的业务层接口
/**
* 账户的业务层接口
*/
public interface AccountService {
/**
* 查询所有
* @return
*/
List<Account> findAllAccount();
/**
* 查询一个
* @return
*/
Account findAccountById(Integer accountId);
/**
* 保存
* @param account
*/
int saveAccount(Account account);
/**
* 更新
* @param account
*/
int updateAccount(Account account);
/**
* 删除
* @param accountId
*/
int deleteAccount(Integer accountId);
}
账户的业务层接口实现类
/**
* 账户的业务层实现类
*/
@ToString
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
public int saveAccount(Account account) {
return accountDao.saveAccount(account);
}
public int updateAccount(Account account) {
return accountDao.updateAccount(account);
}
public int deleteAccount(Integer accountId) {
return accountDao.deleteAccount(accountId);
}
}
测试类
/**
* 使用Junit单元测试:测试我们的配置
*/
public class AccountServiceTest {
private ClassPathXmlApplicationContext context;
private AccountService accountService;
private AccountDao accountDao;
int res = 0;
@Before
public void init(){
//1.获取核心容器对象
context = new ClassPathXmlApplicationContext("bean.xml");
//得到业务层对象
accountService = context.getBean("accountService", AccountService.class);
}
@After
public void destory(){
//手动关闭容器
context.close();
}
@Test
public void testFindAll() {
//执行方法
List<Account> accounts = accountService.findAllAccount();
for (Account account : accounts) {
System.out.println(account);
}
}
@Test
public void testFindOne() {
//执行方法
Account account = accountService.findAccountById(2);
System.out.println(account);
}
@Test
public void testSave() {
//执行方法
Account account = new Account();
account.setName("张三");
account.setMoney(1111f);
res = accountService.saveAccount(account);
if (res>0){
System.out.println("保存成功!");
}else{
System.out.println("保存失败!");
}
}
@Test
public void testUpdate() {
//执行方法
Account account = new Account();
account.setId(4);
account.setName("李四");
account.setMoney(2411f);
res = accountService.updateAccount(account);
if (res>0){
System.out.println("修改成功!");
}else{
System.out.println("修改失败!");
}
}
@Test
public void testDelete() {
//执行方法
res = accountService.deleteAccount(4);
if (res>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}
}