dbutils的环境搭建以及使用

第一步、创建Java工程【此处使用的是maven工程】并导入jar包

<!--导入mysql数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!--导入dbutils包-->
        <dependency>
            <groupId>commons‐dbutils</groupId>
            <artifactId>commons‐dbutils</artifactId>
            <version>1.6</version>
        </dependency>
        <!--导入连接池包-->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!--导入spring包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <!--导入junit包用于测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

第二步、创建数据库

CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) DEFAULT NULL,
  `money` float DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8

第三步、创建JavaBean(又称Pojo对象)

public class Account {
   private Integer id;
   private String name;
   private Double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

第四步、创建Dao接口

import java.util.List;

public interface AccountDao {

    public Account findById(Integer id);

    public List<Account> findAll();

    public void save(Account account);

    public void update(Account account);

    public void deleteById(Integer id);

}

第五步、编写实现类

public class AccountDaoImp implements AccountDao{


    private QueryRunner queryRunner;

    //依赖注入
    public void setQueryRunner(QueryRunner queryRunner){
        this.queryRunner=queryRunner;
    }

    @Override
    public Account findById(Integer id) {
        Account account=new Account();
        //此处要求如下
        //第二个 参数是*接口ResultSetHandler的实现类
        // 如果返回一个对象用BeanResultSetHandler
        //如果返回多个对象使用BeanListHandler
        //要求数据库字段和实体类字段要要一致
        //如果不一致需要实现ResultSetHandler接口
        try{
        account=queryRunner.query("select * from account where id= ?",new BeanHandler<Account>(Account.class),id);
        }        catch (Exception e){
            e.printStackTrace();
        }
        return account;
    }

    @Override
    public List<Account> findAll() {
        List<Account> list=new ArrayList<>();
        try{
            list=queryRunner.query("select * from account where id= ?",new BeanListHandler<Account>(Account.class));
        }        catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void save(Account account) {

    }

    @Override
    public void update(Account account) {

    }

    @Override
    public void deleteById(Integer id) {

    }
}

第六步、测试

使用Junit调用实现类进行测试!

上一篇:QueryRunner(DBUtils) 结果集实例


下一篇:预编译、DBUtils、事务重点整理