Dbutils笔记

Dbutils是什么
  1. 定义:Java编程的工具,封装了对JDBC的一些操作
  2. 作用:使用方便,结构简单,代码量小,提升了开发效率
Dbutils产生原因

在知道这个方法之前,我本身用的是JDBC方式访问数据库,那么JDBC除了易于理解,适合初学者之外,有哪些弊端?

  1. 代码量大,对象往往会被重复定义
  2. 许多代码复用率低,被重复写了多次
  3. 效率低,大量重复造轮子,增加了无意义的工作量,开发效率受负面影响
Dbutils的 3 个核心类
  1. QueryRunner类,主要包含3个方法:
    1.1 update(),用于增删改

     		//dbutils 只是帮我们简化了CRUD 的代码, 但是连接的创建以及获取工作。 不在他的考虑范围
     		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
     		//增加
     		//queryRunner.update("insert into account values (null , ? , ? )", "aa" ,1000);
     		//删除
     		//queryRunner.update("delete from account where id = ?", 5);
     		//更新
     		//queryRunner.update("update account set money = ? where id = ?", 
     		10000000 , 6);
    

    1.2 query(),用于查
    1.2.1 通过New接口的匿名实现类方式

     QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
    
    
     Account  account =  queryRunner.query("select * from account where id = ?", new ResultSetHandler<Account>(){
    
     	@Override
     	public Account handle(ResultSet rs) throws SQLException {
     		Account account  =  new Account();
     		while(rs.next()){
     			String name = rs.getString("name");
     			int money = rs.getInt("money");
     			
     			account.setName(name);
     			account.setMoney(money);
     		}
     		return account;
     	}
     	 
      }, 6);
    

    1.2.2 使用框架直接写好的实现类

     * 查询单个对象
     
     		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
     		//查询单个对象
     		Account account = queryRunner.query("select * from account where id = ?", 
     				new BeanHandler<Account>(Account.class), 8);
     	
     	
     	* 查询多个对象
     
     		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
     		List<Account> list = queryRunner.query("select * from account ",
     				new BeanListHandler<Account>(Account.class));
    

    1.3 batch(); 用来执行批处理

  2. DbUtils类

  3. ResultSetHandle类

     BeanHandler,  查询到的单个数据封装成一个对象
     BeanListHandler, 查询到的多个数据封装 成一个List<对象>
    
      -----------------------------------------------------
     ArrayHandler,  查询到的单个数据封装成一个数组
     ArrayListHandler,  查询到的多个数据封装成一个集合 ,集合里面的元素是数组。 
     
     
     MapHandler,  查询到的单个数据封装成一个map
     MapListHandler,查询到的多个数据封装成一个集合 ,集合里面的元素是map
    
Dbutils的使用步骤
  1. 导入jar包
  2. 创建QueryRunner对象
  3. 使用QueryRunner对象的实例来调用 update 或 query 方法执行增删改 或 查 的操作
  4. 使用ResultSetHandler封装结果集
  5. 使用DbUtils类释放资源
Dbutils的使用案例
以添加为例
  1. 使用JDBC方式

     public static int add(Account account) {
         String sql = "insert into account values(?,?,?,?,?,?)";
         Connection conn = DButil.getConnection();
         try {
             PreparedStatement ps = conn.prepareStatement(sql);
             ps.setString(1, account.getId());
             ps.setString(2, account.getPassword());
             ps.setString(3, account.getName());
             ps.setString(4, account.getPhone());
             ps.setString(5, account.getCountry());
             ps.setByte(6, account.getRole());
             return ps.executeUpdate();
         } catch (SQLException e) {
             //打印输出的异常
             e.printStackTrace();
         }
         return -1;
     }
    

2.使用Dbutils方式

	public static int add1(Account account) {
	    String sql = "insert into account values(?,?,?,?,?,?)";
	    Connection conn = DButil.getConnection();
	    QueryRunner qr = new QueryRunner();
	    Object []params = {account.getId(),account.getPassword(),account.getName(),account.getPhone(),account.getCountry(),account.getRole()};
	    try {
	        return qr.update(conn,sql,params);
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return -1;
	 
	}
以查询为例
  1. 使用JDBC方式

     public static Account get(String id) {
         String sql = "select * from account where  id=?";
         Connection conn = DButil.getConnection();
         try {
             PreparedStatement ps = conn.prepareStatement(sql);
             ps.setString(1, id);
             ResultSet rs = ps.executeQuery();
             while (rs.next()) {
                 Account account = new Account();
                 account.setId(rs.getString("id"));
                 account.setPassword(rs.getString("password"));
                 account.setName(rs.getString("name"));
                 account.setPhone(rs.getString("phone"));
                 account.setCountry(rs.getString("country"));
                 account.setRole(rs.getByte("role"));
                 return account;
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return null;
     }
    
  2. 使用Dbutils方式
    2.1 查询一条语句

     public static Account get1(String id) {
         String sql = "select * from account where id=?";
         Connection conn = DButil.getConnection();
         QueryRunner qr = new QueryRunner();//创建queryRunner对象,用来操作数据库
         Object params[] = {id};//
         try {
             return qr.query(conn,sql,new BeanHandler<Account>(Account.class),params);
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return null;
      
     }
    

    2.2 查询多条语句

     public static long getPage1(int page, int rows, List<Account> accounts) {
         String sql = "select * from account limit ?,?";
         Connection conn = DButil.getConnection();
         QueryRunner qr = new QueryRunner();
         Object [] paras = {(page-1)*rows,rows};
         try {
             //得到一个集合
             List<Account> aa = qr.query(conn,sql,new BeanListHandler<Account>(Account.class),paras);
             accounts.addAll(aa);
             sql = "select COUNT(*) as num from account";
             return qr.query(conn,sql,new ScalarHandler<Long>());//<long>只能用封装类型
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return -1;
      
     }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值