commons-dbutils Helper VS JDBCTemplate

这两个JDBC轻量分装框架的确都是刚刚的。
但是相对来说commons-dbutils缺少封装, 接下来就测试上一篇的help.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>ENTITY event, TABLE events</comment>
<entry key="eventsSequnce">
EVENTS_SEQUENCE
</entry>
<entry key="queryCounts">
SELECT count(*) FROM events
</entry>
<entry key="queryEvents">
SELECT event_id,event_date,title FROM events
</entry>
<entry key="queryEventById">
SELECT event_id id,event_date,title FROM events WHERE event_id = ?
</entry>
<entry key="deleteEvent">
DELETE events WHERE event_id = ?
</entry>
<entry key="updateEvent">
UPDATE events SET event_date = ?, title = ? WHERE event_id = ?
</entry>
<entry key="insertEvent">
INSERT into events(event_id,event_date,title) VALUES(?,?,?)
</entry>
</properties>




[b]1. 查询对象返回(单列单行)[/b]

Spring JDBC queryForObject.

int rowCount = this.jdbcTemplate.queryForObject("select count(*) from t_actor", Integer.class);


用起来真的方便,但是commons-dbutils没有这个方法,而且我感觉这个命名真的很棒.(抄袭下)

DbutilHelper helper = new DbutilHelper("/dbUtils.xml");
//query for Object
Long counts = helper.queryForObject(helper.getSql("queryCounts"), Long.class);


这里的getSql和上面的语句一致,连传递的参数都一致。 是我抄袭了吗?不是的,而是dbutils和Spring这里的设计撞衫了.

Spring JDBC 使用如下借口


public interface RowMapper<T> {

/**
* Implementations must implement this method to map each row of data
* in the ResultSet. This method should not call {@code next()} on
* the ResultSet; it is only supposed to map values of the current row.
* @param rs the ResultSet to map (pre-initialized for the current row)
* @param rowNum the number of the current row
* @return the result object for the current row
* @throws SQLException if a SQLException is encountered getting
* column values (that is, there's no need to catch SQLException)
*/
T mapRow(ResultSet rs, int rowNum) throws SQLException;


而commons dbutils,使用如下借口


public interface ResultSetHandler<T> {

/**
* Turn the <code>ResultSet</code> into an Object.
*
* @param rs The <code>ResultSet</code> to handle. It has not been touched
* before being passed to this method.
*
* @return An Object initialized with <code>ResultSet</code> data. It is
* legal for implementations to return <code>null</code> if the
* <code>ResultSet</code> contained 0 rows.
*
* @throws SQLException if a database access error occurs
*/
T handle(ResultSet rs) throws SQLException;


其实我更喜欢commons-dbutils的设计更能让人理解. 原理都一样,处理结果集到对象.

[b]2. 查询对象(单行)[/b]

Spring JDBC queryForObject.

Actor actor = this.jdbcTemplate.queryForObject(
"select first_name, last_name from t_actor where id = ?",
new Object[]{1212L},
new RowMapper<Actor>() {
public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
actor.setLastName(rs.getString("last_name"));
return actor;
}
});


方法名没变, 不过是用对象数组做参数,还加了RowMapper.


//query bean Object
Event count = helper.queryBean(helper.getSql("queryEventById"), Event.class,161L);


我这里换了个名字,因为要做Bean的转化,如果column名称和对象的property名称对应,那么以上就可以了。 如果不对应怎么办?可以使用column Lable,因为这个优先级比较高
用法如下,修改sql是column与property对应.

SELECT event_id id,event_date date,title FROM events WHERE event_id = ?

一般情况下是没有问题的,但是遇上关键字,你就煞笔了吧(date is the keyword in database).[[b]其实对象的属性命名也很重要,不要用关键字[/b]]

于是乎,我这里加了个转化方法,Map<String,String> columntopropertyoverrride.


Map<String,String> columntopropertyoverrides = new HashMap<String,String>();
columntopropertyoverrides.put("EVENT_DATE", "date");
Event count1 = helper.queryBean(helper.getSql("queryEventById"), Event.class,columntopropertyoverrides,161L);

这样就能绕过关键字了。

[b]2. 查询对象列表(多行)[/b]

Spring JDBC query.

List<Actor> actors = this.jdbcTemplate.query(
"select first_name, last_name from t_actor",
new RowMapper<Actor>() {
public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
actor.setLastName(rs.getString("last_name"));
return actor;
}
});


其实spring很狡猾的, query查询的是list, queryForObject只是调用query去第一条. 所以看起来很简单,还是很清晰明白的,比使用框架好多了。[[b]no configuration[/b]]


//query List<bean> Object
List<Event> events = helper.queryBeanList(helper.getSql("queryEvents"), Event.class);
System.out.println(events);


这个功能,其实就是把一个对象放到list,两个没区别.


[b]3. 添加[/b]

this.jdbcTemplate.update(
"insert into t_actor (first_name, last_name) values (?, ?)",
"Leonor", "Watling");




Long id = helper.insert(helper.getSql("insertEvent"), helper.getSql("eventsSequnce"), new java.sql.Date(new Date().getTime()),"good mood");


[b]4. 更新[/b]


this.jdbcTemplate.update(
"update t_actor set last_name = ? where id = ?",
"Banjo", 5276L);




int effect = helper.update(helper.getSql("updateEvent"),new java.sql.Date(new Date().getTime()),"okok",150L);


[b]5. 删除[/b]


this.jdbcTemplate.update(
"delete from actor where id = ?",
Long.valueOf(actorId));




int effect1 = helper.update(helper.getSql("deleteEvent"),170L);



这真的太像了。 如果commons-dbutils封装一下,还是一个不错的轻量级ORM. 和Spring JDBC差不多,不过如果使用Spring开发的同学,使用Spring JDBC就可以了,自己封装一下就可以了,就像我封装commons-dbutils一样

因为轻量,基本上使用JDBC操作,所以适合批量操作。

Object[][] params = new Object[5][];
params[0] = new Object[]{"cao",140L};
params[1] = new Object[]{"cao",141L};
params[2] = new Object[]{"cao",142L};
params[3] = new Object[]{"cao",161};
params[4] = new Object[]{"cao",162};

//batch update in batchSize
int[] effect2 = helper.batch("update events set title=? where event_id=?", params, 2);
for(int effect2s:effect2){
System.out.println("batch:"+effect2s);
}

//batch update in params.length.
int[] effectall = helper.batch("update events set title=? where event_id=?", params);
for(int effectalls:effectall){
System.out.println("batchall:"+effectalls);
}
/*
* Indicates that a batch statement was executed with a successful result,
* but a count of the number of rows it affected is unavailable.
* public static final int SUCCESS_NO_INFO = -2;
*/



以上是批量更新,还有批量插入,批量查询。
可能有人会问批量查询????

if query large Datas, please set FecthSize, this will be improve performance.

一般情况下,我们select * from table, 默认一次fecth 10条,因为FecthSize默认为10, 如果我们要获取1000条,10000条?

if you increase this number, will be reduce client and oracle's response. but suggest not over than 100, or will be spend middle component's memory.

如果获取大数据的话,把这个值设置大一点,但是不要超过100,否这中间件压力很大。不过我没有测试过。但是调到1000来获取5W的数据和使用10来获取5W条数据,时间相差100倍。
commons-dbutils-1.7 jar是Apache Commons项目中的一个Java库,主要用于简化和优化数据库操作。该jar包提供了一组实用工具类,可以减少编写JDBC代码的工作量,同时提供了一种简单而强大的方式来处理数据库连接和查询。 commons-dbutils-1.7 jar主要包括以下几个核心类和接口: 1. QueryRunner:用于执行SQL语句,提供了一系列的查询方法,可以方便地执行查询操作并返回结果。 2. ResultSetHandler:用于处理SQL查询结果集,提供了多种实现类如BeanHandler、BeanListHandler、ScalarHandler等,方便处理不同类型的查询结果。 3. ResultSetExtractor:用于提取结果集中的数据,可以通过自定义的方式从结果集中获取数据并进行处理。 4. BatchExecutor:用于执行批量SQL语句,可以一次性执行多个SQL语句,提高数据库操作的效率。 通过使用commons-dbutils-1.7 jar,可以简化数据库操作代码的编写过程,减少重复劳动。它隐藏了许多JDBC的细节,提供了一种更高级别的抽象,使开发者可以更加专注于业务逻辑的实现,而不是过多关注数据库操作的细节。 另外,commons-dbutils-1.7 jar还具备良好的可扩展性和灵活性,可以与其他开源框架(如Spring、Hibernate)和数据库(如MySQL、Oracle)无缝集成,以满足不同项目的需求。 总之,commons-dbutils-1.7 jar是一个功能强大且易于使用的Java库,可以简化和优化数据库操作,提高开发效率和代码质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值