DBUtils 框架

一、O-R Mapping
Object-Relation Mapping  对象关系映射(对象关系模型)

常用的 O-R Mapping 工具有:
Hibernate 
session.save(user)  
ibatis
sql 语句要自己写
DBUtils
简单的工具
二、 DBUtils 框架
commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装

DBUtils 核心API
org.apache.commons.dbutils.QueryRunner
提供update(cud)和query(r)方法
org.apache.commons.dbutils.ResultSetHandler
结果集处理器,接口类型
org.apache.commons.dbutils.DbUtils   
工具类,提供一系列close方法,装载驱动等

API详解
1. QueryRunner
重载的构造函数
public QueryRunner()
调用无参的构造方法,在进行 crud 操作时需要传入 Connection 对象,一般用于事务
public QueryRunner(DataSource ds)
创建对象时传入 数据源  多数情况下采用此构造函数

2. ResultSetHandler 
该接口为结果集处理器,所以对结果集进行处理的程序都需要实现该接口
DBUtils框架提供了一系列常用的结果集处理器实现类
ArrayHandler:把结果集中的第一行数据转成对象数组。
ArrayListHandler:把结果集中的每一行数据都转成一个数组,再存放到List中。
BeanHandler:将结果集中的第一行数据封装到一个对应的JavaBean实例中。
BeanListHandler:将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。
ColumnListHandler:将结果集中某一列的数据存放到List中。
KeyedHandler(name):将结果集中的每一行数据都封装到一个Map里,再把这些map再存到一个map里,其key为指定的key。
MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。
MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List
三.实例
package cn.itcast.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.itcast.exception.DaoException;
import cn.itcast.utils.JdbcUtils;
import cn.itcsat.domain.Department;
import cn.itcsat.domain.Employee;

public class EmployeeDao {

public List getAll() {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from employee";
List<Employee> list = (List) runner.query(sql, new BeanListHandler(Employee.class));
// 为每个员工查出所属的部门
for(Employee e : list) {
int employeeid = e.getId();
String sql1 = "select d.* from department d,employee e where e.departmentid=d.id and e.id=?";
Department department = (Department) runner.query(sql1, new BeanHandler(Department.class), new Object[]{employeeid});
e.setDepartment(department);
}
return list;
} catch (SQLException e) {
throw new DaoException(e);
}
}
public Employee find(int id) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from employee where id=?";
Employee employee = (Employee) runner.query(sql, new BeanHandler(Employee.class), new Object[]{id});
return employee;
} catch (SQLException e) {
throw new DaoException(e);
}
}

public boolean insert(Employee employee) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "insert into employee(name, departmentid) values(?,?)";
int num = runner.update(sql, new Object[]{employee.getName(), employee.getDepartment().getId()});
if(num>0)
return true;
return false;
} catch (SQLException e) {
throw new DaoException(e);
}
}
public boolean update(Employee employee) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "update employee set name=? where id=?";
int num = runner.update(sql, new Object[]{employee.getName(), employee.getId()});
if(num>0)
return true;
return false;
} catch (SQLException e) {
throw new DaoException(e);
}
}
public boolean delete(int id) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "delete from employee where id=?";
int num = runner.update(sql, id);
if(num>0)
return true;
return false;
} catch (SQLException e) {
throw new DaoException(e);
}
}
}



public class JdbcUtils {

private static DataSource ds;
static {
ds = new ComboPooledDataSource();
}
public static DataSource getDataSource() {
return ds;
}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值