一、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;
}
}