在spring中使用dbutiles
配置文件中的配置
引用文本
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
增删改查的代码
package dao.Impl;
import dao.IAccountDao;
import domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.List;
public class AccountDao implements IAccountDao {
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> findAll() {
try {
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
} catch (Exception e) {
throw new RuntimeException();
}
}
public Account findById(Integer accountId) {
try {
return runner.query("select * from account where id = ?", new BeanHandler<Account>(Account.class),accountId);
} catch (Exception e) {
throw new RuntimeException();
}
}
public void deleteAccount(Integer accountId) {
try {
runner.update("delete from account where id = ?",accountId);
} catch (Exception e) {
throw new RuntimeException();
}
}
public void updateAccount(Account account) {
try {
runner.update("update account set name = ?,money = ?where id = ?",account.getName(),account.getMoney(),account.getId());
**这就个get要按照顺序,否则会失败*
} catch (Exception e) {
throw new RuntimeException();
}
}
public void saveAccount(Account account) {
try {
runner.update("insert into account (name,money) values (?,?)",account.getName(),account.getMoney());
} catch (Exception e) {
throw new RuntimeException();
}
}
}