一.DBUtils

1.什么是DBUtils

DBUtils是java编程中的数据库操作实用工具,小巧简单实用。DBUtils封装了对JDBC的操作,简化了JDBC操作,可以简化代码。Dbutils三个核心功能介绍
1.QueryRunner中提供对sql语句(insert,update,delete)操作的API;
2.ResultSetHandler接口,用于定义select操作后,怎样封装结果集.;
3.DbUtils类,它就是一个工具类,定义了关闭资源与事务处理的方法;

2.API 文档

http://commons.apache.org/proper/commons-dbutils/apidocs/org/apache/commons/dbutils/package-summary.html

二.核心API

1.QueryRunner类 和 ResultSetHandler接口

http://commons.apache.org/proper/commons-dbutils/apidocs/org/apache/commons/dbutils/QueryRunner.html

①增删改

  1. @Test
  2. public void insertDemo() throws SQLException {
  3. QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());
  4. String sql = "insert into table Values(?,?)";
  5. queryRunner.update(sql, 2, "222");
  6. }
  7. //删除
  8. @Test
  9. public void deleteDemo() throws SQLException {
  10. QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());
  11. String sql = "delete from table where id = ?";
  12. queryRunner.update(sql, 2);
  13. }
  14. //修改
  15. @Test
  16. public void updateDemo() throws SQLException {
  17. QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());
  18. String sql = "delete table set param1=?,param2=? where id = ?;
  19. queryRunner.update(sql, 2, new Date(),3);
  20. }

②.查询

BeanHandler
将结果集中的第一行数据封装到一个对应的JavaBean实例中。
BeanListHandler
将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。

  1. // 查找单条记录
  2. @Test
  3. public void find() throws SQLException {
  4. String sql = "select * from `column` where id = ?";
  5. //传入数据封装的类
  6. Column col = queryRunner.query(sql, new BeanHandler<>(Column.class),1);
  7. System.out.println(col);
  8. }
  9. // 查找所有记录
  10. @Test
  11. public void getAll() throws SQLException {
  12. List<Column> list = queryRunner.query("select * from `column`",new BeanListHandler<Column>(Column.class));
  13. for(Column c : list){
  14. System.out.println(c);
  15. }
  16. }

ArrayHandler:把结果集中的第一行数据转成对象数组。
ArrayListHandler:把结果集中的每一行数据都转成一个数组,再存放到List中。

  1. @Test
  2. //ArrayHandler
  3. public void test2() throws SQLException {
  4. String sql = "select * from `column`";
  5. //ArrayHandler
  6. Object[] col = queryRunner.query(sql, new ArrayHandler());
  7. System.out.println(Arrays.toString(col));
  8. //ArrayListHandler
  9. List list = queryRunner.query(sql,new ArrayListHandler());
  10. for(Object obj:list){
  11. System.out.println(Arrays.toString((Object[]) obj));
  12. }
  13. }

ColumnListHandler:将结果集中某一列的数据存放到List中。

  1. //ColumnHandler
  2. public void test4() throws SQLException {
  3. String sql = "select * from `column`";
  4. //
  5. List<String> list = (List) queryRunner.query(sql,new ColumnListHandler<>("name"));
  6. for(String name : list){
  7. System.out.println(name);
  8. }
  9. }

MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。
MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List

  1. //MapHandler
  2. @Test
  3. public void test3() throws SQLException {
  4. String sql = "select * from `column`";
  5. //ArrayHandler
  6. Map<String,Object> map = queryRunner.query(sql, new MapHandler());
  7. for(String key : map.keySet()){
  8. System.out.println(key);
  9. }
  10. }

ScalarHandler:用来获得聚合函数的值,返回类型是Object

  1. @Test
  2. public void test45() throws SQLException {
  3. String sql = "select Count(*) from `column`";
  4. Long count = (Long) queryRunner.query(sql,new ScalarHandler());
  5. System.out.println(count);
  6. }

③泛型实现

  1. //查询的泛型实现
  2. public static <T> T find(Class<T> cls, String sql, Object... param) {
  3. try {
  4. QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());
  5. return queryRunner.query(sql, new BeanHandler<>(cls), param);
  6. } catch (SQLException exception) {
  7. exception.printStackTrace();
  8. }
  9. return null;
  10. }
  11. public static <T> List<T> query(Class<T> cls, String sql, Object... param) {
  12. try {
  13. QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());
  14. return queryRunner.query(sql, new BeanListHandler<T>(cls), param);
  15. } catch (SQLException exception) {
  16. exception.printStackTrace();
  17. }
  18. return new ArrayList<T>();
  19. }
  20. //更新
  21. public static int update(String sql, Object... param) throws SQLException {
  22. QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());
  23. return queryRunner.update(sql, param);
  24. }
  25. public static int update(Connection conn, String sql, Object... param) throws SQLException {
  26. QueryRunner queryRunner = new QueryRunner();
  27. return queryRunner.update(conn, sql, param);
  28. }