DbUtils要点小结

一. DbUtils核心API

1. QueryRunner

update方法

query方法

2. 各种Handler都实现ResultSetHandler接口

beanhandler

beanlisthandler

beanmaphandler

arrayhandler

arraylisthandler

maplisthandler

scalarhandler

columnlisthandler

keyedhandler

二. handler层次结构

使用IDEA生成类图

三. QueryRunner面向实体进行查询

DbUtils原理: 执行SQL->返回ResultSet->反射创建实体对象->Handler

好处:无需面对pstmt和rs对象

四. 使用threadlocal实现con对象透明化

threadlocal是一个隔离线程的容器

具体操作:

1) 获取con时, 从tl容器中查找

2) 创建con时, 存储到tl容器中

3) con使用完毕后, 从tl容器中移除, 线程结束后, tl容器就会被销毁?

五. 使用连接池

频繁创建con对象会造成大量的系统开销, 而且数据库的连接数是有限的, 可以使用dpcp连接池对数据库连接进行池化

六. 一张图理解maplisthandler

 

附:

pom.xml中添加dpcp依赖

1 <dependency>
2     <groupId>org.apache.commons</groupId>
3     <artifactId>commons-dbcp2</artifactId>
4     <version>2.0.1</version>
5 </dependency>

pom.xml添加DbUtils依赖

1 <dependency>
2     <groupId>commons-beanutils</groupId>
3     <artifactId>commons-beanutils</artifactId>
4     <version>1.6</version>
5 </dependency>

 

posted on 2018-08-27 22:11  ert999  阅读(131)  评论(0编辑  收藏  举报

导航