昨天做了这么多的铺垫,当然就是为了引出今天的DBUtils框架了,它的实现原理跟我们编写的简易框架是类似的。
话不多说,进入正题。
commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能。因此dbutils成为很多不喜欢hibernate的公司的首选。
jar包下载地址:链接:https://pan.baidu.com/s/1BKtq_VoBw52qyIJr6c_CZg
提取码:skrf
复制这段内容后打开百度网盘手机App,操作更方便哦
API介绍:
- org.apache.commons.dbutils.QueryRunner — 核心操作类
- org.apache.commons.dbutils.ResultSetHandler
- org.apache.commons.dbutils.DbUtils — 工具类
QueryRunner 类简单化了SQL查询,它与ResultSetHandler组合在一起使用可以完成大部分的数据库操作,能够大大减少编码量。
QueryRunner类提供了两个构造方法:
默认的构造方法
需要一个 javax.sql.DataSource 来作参数的构造方法。
QueryRunner类有四个重载的构造方法,如果使用QueryRunner(DataSource ds)构造QueryRunner对象,数据库事务将交给DBUtils框架进行管理,默认情况下每一个sql单独一个事务。此时应该使用不带Connection参数的方法。
如果使用QueryRunner()构造QueryRunner对象,就需要自己来管理事务,因为框架没有连接池,无法获得数据库的连接,此时应该使用带Connection参数的方法。
Dbutils工具类能够加载驱动、关闭资源、控制事务,所以之前的一些繁琐工作都可以借助该工具类实现。
编写案例演示一下。
新建一个web项目,不要忘了导入jar包。
新建测试类QueryRunnerTest
先在类中定义一个成员变量,方便后续测试
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
- 1
编写插入的测试代码
@Test
public void testInsert() throws SQLException{
//1、创建QueryRunner对象
QueryRunner queryRunner = new QueryRunner(dataSource);
//2、准备方法参数
String sql ="insert into account values(null,?,?)";
Object[] param = {"fff",1000};
//3、调用方法
queryRunner.update(sql, param);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
使用该构造方法构造的QueryRunner对象是不需要手动释放资源的,因为连接交由DBUtils工具包管理了,它会帮助我们释放资源。
然后运行测试代码
插入成功。
接下来测试一下更新sql,编写测试代码
@Test
public void testUpdate() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "update account set money = ? where name = ?";
Object[] param = {2000,"fff"};
queryRunner.update(sql,param);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
运行测试代码
修改成功。
接下来测试一下删除sql,编写测试代码
@Test
public void testDelete() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "delete from account where id = ?";
queryRunner.update(sql,2);
}
- 1
- 2
- 3
- 4
- 5
- 6
运行测试代码
删除成功。
现在我们来模拟一个转账的情景。
@Test public void testTransfer() throws SQLException{ double money = 100; String outAccount = "aaa"; String inAccount = "bbb"; String sql1 = "update account set money = money - ? where name = ?"; String sql2 = "update account set money = money + ? where name = ?";
QueryRunner queryRunner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QueryRunner</span><span class="token punctuation">(</span>dataSource<span class="token punctuation">)</span><span class="token punctuation">;</span> queryRunner<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>sql1<span class="token punctuation">,</span>money<span class="token punctuation">,</span>outAccount<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//产生一个错误</span> <span class="token keyword">int</span> d <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">/</span> <span class="token number">0</span><span class="token punctuation">;</span> queryRunner<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>sql2<span class="token punctuation">,</span>money<span class="token punctuation">,</span>inAccount<span class="token punctuation">)</span><span class="token punctuation">;</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
运行测试代码
程序报错,aaa账户少了100,而bbb账户金额并没有多,这是因为你把连接交给了DBUtils管理,默认一条sql就是一个事务,所以,我们应该自己来管理事务,才能避免这种情况发生。
修改转账代码
@Test public void testTransfer() throws SQLException{ double money = 100; String outAccount = "aaa"; String inAccount = "bbb"; String sql1 = "update account set money = money - ? where name = ?"; String sql2 = "update account set money = money + ? where name = ?";
<span class="token comment">//手动事务管理</span> QueryRunner queryRunner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QueryRunner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Connection conn <span class="token operator">=</span> JDBCUtils<span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> conn<span class="token punctuation">.</span><span class="token function">setAutoCommit</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> queryRunner<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span>sql1<span class="token punctuation">,</span>money<span class="token punctuation">,</span>outAccount<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//产生一个错误</span> <span class="token keyword">int</span> d <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">/</span> <span class="token number">0</span><span class="token punctuation">;</span> queryRunner<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span>sql2<span class="token punctuation">,</span>money<span class="token punctuation">,</span>inAccount<span class="token punctuation">)</span><span class="token punctuation">;</span> DbUtils<span class="token punctuation">.</span><span class="token function">commitAndCloseQuietly</span><span class="token punctuation">(</span>conn<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> DbUtils<span class="token punctuation">.</span><span class="token function">rollbackAndCloseQuietly</span><span class="token punctuation">(</span>conn<span class="token punctuation">)</span><span class="token punctuation">;</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
此时运行代码
表数据并没有改变,说明更新操作被回滚了。
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
</div>
</article>