dbutiles的QueryRunner进行增删改查

在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();
        }
    }
}


好的,以下是使用QueryRunner和c3p0进行增删改查操作的示例代码: ```java import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3p0Demo { private static ComboPooledDataSource dataSource; static { dataSource = new ComboPooledDataSource(); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUser("root"); dataSource.setPassword("123456"); dataSource.setDriverClass("com.mysql.jdbc.Driver"); } public static void main(String[] args) throws SQLException { QueryRunner queryRunner = new QueryRunner(dataSource); // 查询 List<User> userList = queryRunner.query("SELECT * FROM user", new BeanListHandler<User>(User.class)); System.out.println(userList); // 新增 queryRunner.update("INSERT INTO user (name, age) VALUES (?, ?)", "张三", 20); // 修改 queryRunner.update("UPDATE user SET age = ? WHERE name = ?", 21, "张三"); // 删除 queryRunner.update("DELETE FROM user WHERE name = ?", "张三"); } public static class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } } } ``` 这里使用了c3p0的ComboPooledDataSource来获取数据库连接,并使用QueryRunner进行数据库操作。以上代码中的User类仅作为示例,你可以根据自己的需求修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值