Apache组织下的一个轻量级ORM框架
Commons DbUtils: JDBC Utility Component
两个核心方法
update方法 ==> insert,update,delete
query方法 ==> select
一个核心类
QueryRunner DbUtils的核心类
DbUtils ORM工具使用
代码示例:
student表中有id,name, age, gender, score, address
import com.qfedu.b_studentsys.entity.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.*;
import org.junit.Test;
import util.JdbcUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* DbUtils ORM框架演示
*
* @author Anonymous 2020/3/25 16:03
*/
public class DbUtilsTest {
@Test
public void testInsert() throws SQLException {
// 1. DbUtils核心类 QueryRunner对象
QueryRunner runner = new QueryRunner();
// 2. 插入数据到Student数据表中
String sql = "insert into student(name, age, gender, score, address) value(?, ?, ?, ?, ?)";
Object[] parameters = {"滴滴", 99, true, 59, "河南郑州"};
// 3. 获取数据库连接
// JdbcUtil.getConnection()自己封装的方法,为了获取connection对象
Connection connection = JdbcUtil.getConnection();
// 4. 执行Update方法
runner.update(connection, sql, parameters);
JdbcUtil.close(connection);
}
/**
* 了解操作方式, ResultSetHandler
*/
@Test
public void testQuery1() throws SQLException {
// 1. DbUtils核心类 QueryRunner对象
QueryRunner runner = new QueryRunner();
// 2. SQL语句
String sql = "select * from student where id = 1";
Connection connection = JdbcUtil.getConnection();
/*
ResultSetHandler 核心接口
ResultSet结果集 Handler处理,
核心方法 handler(ResultSet rs)
*/
Student student = runner.query(connection, sql, rs -> {
Student stu = null;
if (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
boolean gender = rs.getBoolean("gender");
float score = rs.getFloat("score");
String address = rs.getString("address");
stu = new Student(id, name, age, gender, score, address);
}
return stu;
});
System.out.println(student);
JdbcUtil.close(connection);
}
/**
* BeanHandler
*/
@Test
public void queryBean() throws SQLException {
// 1. DbUtils核心类 QueryRunner对象
QueryRunner runner = new QueryRunner();
// 2. SQL语句
String sql = "select * from student where id = 1";
Connection connection = JdbcUtil.getConnection();
/*
BeanHandler: 处理符合JavaBean规范的类对象,传入参数是对应JavaBean规范 Class对象
*/
Student student = runner.query(connection, sql, new BeanHandler(Student.class));
System.out.println(student);
JdbcUtil.close(connection);
}
/**
* BeanListHandler
*/
@Test
public void queryBeanList() throws SQLException {
// 1. DbUtils核心类 QueryRunner对象
QueryRunner runner = new QueryRunner();
// 2. SQL语句
String sql = "select * from student where id > ?";
Connection connection = JdbcUtil.getConnection();
/*
BeanListHandler: 处理符合JavaBean规范的实体类,并且返回值是一个List集合
包含制定的JavaBean实体类
*/
List list = runner.query(connection, sql, new BeanListHandler(Student.class), 2);
for (Student student : list) {
System.out.println(student);
}
JdbcUtil.close(connection);
}
/**
* ArrayHandler
*/
@Test
public void queryArray() throws SQLException {
// 1. DbUtils核心类 QueryRunner对象
QueryRunner runner = new QueryRunner();
// 2. SQL语句
String sql = "select * from student where id = 1";
Connection connection = JdbcUtil.getConnection();
/*
ArrayHandler: 查询一个数据行,数据行中的所有数据整合成一个Object类型数组返回
*/
Object[] values = runner.query(connection, sql, new ArrayHandler());
System.out.println(Arrays.toString(values));
JdbcUtil.close(connection);
}
/**
* ArrayListHandler
*/
@Test
public void queryArrayList() throws SQLException {
// 1. DbUtils核心类 QueryRunner对象
QueryRunner runner = new QueryRunner();
// 2. SQL语句
String sql = "select * from student where id > ?";
Connection connection = JdbcUtil.getConnection();
/*
ArrayListHandler: 查询结果集中所有数据行,每一行数据对应一个Object类型数组,存储在List集合中
*/
List list = runner.query(connection, sql, new ArrayListHandler(), 2);
for (Object[] values : list) {
System.out.println(Arrays.toString(values));
}
JdbcUtil.close(connection);
}
/**
* MapHandler
*/
@Test
public void queryMap() throws SQLException {
// 1. DbUtils核心类 QueryRunner对象
QueryRunner runner = new QueryRunner();
// 2. SQL语句
String sql = "select * from student where id = 1";
Connection connection = JdbcUtil.getConnection();
/*
MapHandler: 处理一个数据行,数据行中字段是对应Key,字段对应数据是value,组成一个Map双边队列
*/
Map map = runner.query(connection, sql, new MapHandler());
System.out.println(map);
}
/**
* MapListHandler
*/
@Test
public void queryMapList() throws SQLException {
// 1. DbUtils核心类 QueryRunner对象
QueryRunner runner = new QueryRunner();
// 2. SQL语句
String sql = "select * from student where id > ?";
Connection connection = JdbcUtil.getConnection();
/*
MapListHandler: 结果集中所有的数据行,每一行对应一个Map对象,字段名为Key,字段对应的数据为value,所有数据行存储
在List中
*/
List<Map> mapList = runner.query(connection, sql, new MapListHandler(), 2);
for (Map map : mapList) {
System.out.println(map);
}
}
}
1.2 ResultHandler以及其子类
ResultSetHandler 核心接口
ResultSet结果集 Handler处理,
核心方法 handler(ResultSet rs)
BeanHandler:
处理符合JavaBean规范的类对象,传入参数是对应JavaBean规范 Class对象
BeanListHandler:
处理符合JavaBean规范的实体类,并且返回值是一个List集合包含制定的JavaBean实体类
ArrayHandler:
查询一个数据行,数据行中的所有数据整合成一个Object类型数组返回
ArrayListHandler:
查询结果集中所有数据行,每一行数据对应一个Object类型数组,存储在List集合中
MapHandler:
处理一个数据行,数据行中字段是对应Key,字段对应数据是value,组成一个Map双边队列
MapListHandler:
结果集中所有的数据行,每一行对应一个Map对象,字段名为Key,字段对应的数据为value,所有数据行存储在List中