使用 spring 的 IoC 的实现账户的CRUD
表中的数据如上所示
Account model
package com.kfm.spring.model;
import lombok.Data;
/**
* @author jkk
*/
@Data
public class Account {
private int id;
private int uid;
private Double money;
}
1.dao中的接口
package com.kfm.spring.dao;
import com.kfm.spring.model.Account;
import java.util.List;
/**
* @author jkk
*/
public interface IAccountDao {
int save(Account account);
int update(Account account);
int delete(int id);
Account findById(int id);
List<Account> findAll();
}
2.dao中接口的实现类
这里采用的是apache旗下的一个数据库连接的工具commons-dbutils
DbUtils 解决的最核心的问题就是结果集的映射,可以把 ResultSet 封装成JavaBean。
package com.kfm.spring.dao.impl;
import com.kfm.spring.dao.IAccountDao;
import com.kfm.spring.model.Account;
import lombok.Data;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* @author jkk
*/
public class IAccountDaoimpl implements IAccountDao {
private QueryRunner queryRunner;
public void setQueryRunner(QueryRunner queryRunner) {
this.queryRunner = queryRunner;
}
@Override
public int save(Account account) {
String sql = "insert into account(uid,money) values(?,?)";
try {
return queryRunner.update(sql,account.getUid(),account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public int update(Account account) {
return 0;
}
@Override
public int delete(int id) {
String sql = "delete from account where id = ?";
try {
return queryRunner.update(sql,id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public Account findById(int id) {
String sql = "select * from account where id = ?";
try {
Account account = queryRunner.query(sql, new BeanHandler<>(Account.class),id);
return account;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public List<Account> findAll() {
String sql = "select * from account";
try {
List<Account> accounts = queryRunner.query(sql, new BeanListHandler<>(Account.class));
return accounts;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
3.配置bean容器
因为要用到 QueryRunner queryRunner属性,所以要引入org.apache.commons.dbutils.QueryRunner,要用commons,dbutils,就要使用数据源(ds),所以再次引入数据源的内容,放到容器中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountdao" class="com.kfm.spring.dao.impl.IAccountDaoimpl">
<property name="queryRunner" ref="queryRunner"></property>
</bean>
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="datasource"></constructor-arg>
</bean>
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web?serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
</bean>
</beans>
4.测试
package com.kfm.spring.dao;
import com.kfm.spring.dao.impl.IAccountDaoimpl;
import com.kfm.spring.model.Account;
import junit.framework.TestCase;
import org.junit.Assert;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.xml.transform.Source;
import java.util.List;
public class IAccountDaoTest extends TestCase {
public void testSave() {
ApplicationContext ax = new ClassPathXmlApplicationContext("bean.xml");
IAccountDaoimpl accountDaoimpl = ax.getBean("accountdao", IAccountDaoimpl.class);
Account account = new Account();
account.setUid(3);
account.setMoney(666.66);
int i = accountDaoimpl.save(account);
Assert.assertEquals(1,i);
}
public void testDelete() {
ApplicationContext ax = new ClassPathXmlApplicationContext("bean.xml");
IAccountDaoimpl accountDaoimpl = ax.getBean("accountdao", IAccountDaoimpl.class);
int i = accountDaoimpl.delete(14);
Assert.assertEquals(1,i);
}
public void testFindById() {
ApplicationContext ax = new ClassPathXmlApplicationContext("bean.xml");
IAccountDaoimpl accountDaoimpl = ax.getBean("accountdao", IAccountDaoimpl.class);
Account a = accountDaoimpl.findById(3);
System.out.println(a);
}
public void testFindAll() {
ApplicationContext ax = new ClassPathXmlApplicationContext("bean.xml");
IAccountDaoimpl accountDao = ax.getBean("accountdao", IAccountDaoimpl.class);
List<Account> accountList = accountDao.findAll();
for (Account a : accountList){
System.out.println(a);
}
}
}