一、maven依赖 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <dependency > <groupId > commons-dbutils</groupId > <artifactId > commons-dbutils</artifactId > <version > 1.7</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 8.0.16</version > </dependency > <dependency > <groupId > org.apache.commons</groupId > <artifactId > commons-dbcp2</artifactId > <version > 2.7.0</version > </dependency >
二、概述 DBUtils是Java编程中的数据库操作实用工具,小巧简单实用。
DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。
并不是一个ORM框架。
三、核心类 QueryRunner 类
提供对sql语句操作的API。
是一个线程安全的类,可以复用这一个对象。
可以在构造的时候传入一个数据源,以后操作SQL语句的时候就不需要传入连接。
ResultSetHandler 接口
用于定义select操作后,怎样封装结果集。
已经内置了一些常用的 handler,也可以自己定义自己的 handler。
DBUtils 类
一个工具类,定义了关闭资源与事务处理的方法。
四、使用 使用 queryRunner 增删改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class TestQueryRunner extends TestApplication { @Test public void testInsert () throws SQLException { String sql = "insert into user value(?,?,?,?,?,?,?)" ; int res = queryRunner.update(sql, new Object[]{1001 , new Date(), new Date(), "阿三" , 1 , new Date(), 1002 }); log.info("受影响的行数: " + res); } @Test public void testUpdate () throws SQLException { String sql = "update user set name=? where id=5" ; int res = queryRunner.update(sql, "刘翔" ); log.info("受影响的行数: " + res); } @Test public void testDelete () throws SQLException { String sql = "delete from user where name=?" ; int res = queryRunner.update(sql, "阿三" ); log.info("受影响的行数: " + res); } }
使用 queyRunner 查询并使用 resultSetHandler 处理查询结果
使用到了 javaBean 的概念:
JavaBean 是一种JAVA语言写成的可重用组件。为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器 。JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性,set和get方法获取。众所周知,属性名称符合这种模式,其他Java 类可以通过自省机制(反射机制)发现和操作这些JavaBean 的属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 public class TestResultHandler extends TestApplication { @Test public void testSelectByBeanListHandler () throws SQLException { String sql = "select * from user" ; List<User> res = queryRunner.query(sql, new BeanListHandler<User>(User.class)); res.forEach(val -> log.info(val.toString())); } @Test public void testSelectByBeanHandler () throws SQLException { String sql = "select * from user" ; User res = queryRunner.query(sql, new BeanHandler<>(User.class)); log.info(res.toString()); } @Test public void testSelectByArrayHandler () throws SQLException { String sql = "select * from user" ; Object[] res = queryRunner.query(sql, new ArrayHandler()); for (Object val : res) { log.info(val.toString()); } } @Test public void testSelectByArrayListHandler () throws SQLException { String sql = "select * from user" ; List<Object[]> res = queryRunner.query(sql, new ArrayListHandler()); for (Object[] val : res) { String str = "" ; for (Object obj : val) { str += obj + "\t" ; } log.info(str); } } @Test public void testSelectByBeanMapHandler () throws SQLException { String sql = "select * from user" ; Map<String, User> res = queryRunner.query(sql, new BeanMapHandler<String, User>(User.class)); log.info(res.toString()); } @Test public void testSelectByColumnListHandler () throws SQLException { String sql = "select * from user" ; List<String> res = queryRunner.query(sql, new ColumnListHandler<String>(2 )); System.out.println(res); } @Test public void testSelectByKeyedHandler () throws SQLException { String sql = "select * from user" ; Map<String, Map<String, Object>> res = queryRunner.query(sql, new KeyedHandler<String>()); log.info(res.toString()); } @Test public void testSelectByMapHandler () throws SQLException { String sql = "select * from user" ; Map<String, Object> res = queryRunner.query(sql, new MapHandler()); log.info(res.toString()); } @Test public void testSelectByMapListHandler () throws SQLException { String sql = "select * from user" ; List<Map<String, Object>> res = queryRunner.query(sql, new MapListHandler()); log.info(res.toString()); } @Test public void testSelectByScalarHandler () throws SQLException { String sql = "select * from user" ; Object res = queryRunner.query(sql, new ScalarHandler<>(4 )); log.info(res.toString()); } }
使用 Dbutils
1 2 3 4 5 6 7 8 9 10 11 12 13 public class TestDBUtils extends TestApplication { @Test public void test () { } }