使用原生的JDBC进行开发,发现代码过于冗余
为了简化开发apache commons组件诞生了一个新成员dbutils !
dbutils 的三个核心功能简介:
方法名 | 方法描述 |
---|---|
QueryRunner(DataSource) | 构造方法,提供数据源,内部自己维护Connection |
update(String sql , Object ... params) | 执行DML语句(数据操纵语言,这里指增删改) |
query(String sql , ResultSetHandler , Object ... params) | 执行DQL语句(数据查询语言) |
方法名 | 方法描述 |
---|---|
QueryRunner() | 空参构造方法 |
update(Connection conn , String sql , Object ... params) | 使用提供的Connection,完成DML语句 |
query(Connection conn , String sql , ResultSetHandler , Object ... params) | 使用提供的Connection,执行DQL语句,并将查询结果封装到对象中 |
详情查询
1.概述
ResultSetHandler是一个接口,它的所有实现类如下:
AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BeanHandler, BeanListHandler, ColumnListHandler, KeyedHandler, MapHandler, MapListHandler, ScalarHandler
2.测试准备
3.代码展示
***pom.xml
1.8
1.8
mysql
mysql-connector-java
5.1.46
c3p0
c3p0
0.9.1.2
junit
junit
4.12
javax.servlet
javax.servlet-api
3.1.0
commons-dbutils
commons-dbutils
1.7
***c3p0-config.xml
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/mysqlstudy?characterEncoding=utf8
root
root
10
***Admin.java
package com.howie.pojo;
/**
* @Author weihuanwen
* @Date 2019/8/5 19:18
* @Version 1.0
*/
public class Admin {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "admin{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
***C3P0Utils.java
package com.howie.utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class C3P0Utils {
private static ComboPooledDataSource cpds = new ComboPooledDataSource();
/*
相比于直接向AccountDao的方法中传入connection对象,
这里创建一个静态的ThreadLocal更加方便
ThreadLocal的底层是一个map,存储的是当前线程的名字和当前的connection
由于map集合是静态的,一开便加载在内存中了,想使用的话便可直接调取使用
所以说这里可以不用ThreadLocal也行,不用的话就创建一个静态的map集合也一样能达到效果
只不过ThreadLocal里面已经封装好了这个map,我们用它就好
*/
private static ThreadLocal tl = new ThreadLocal<>();
public static Connection getConnection() {
try {
Connection conn = tl.get();
if (conn == null) {
conn = cpds.getConnection();
tl.set(conn);
}
return conn;
} catch (SQLException e) {
throw new RuntimeException("获取数据库连接失败!");
}
}
public static DataSource getDataSource() {
return cpds;
}
public static void close(ResultSet rs, Statement stat, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stat != null) {
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
***Operation.java
package com.howie.resultset;
import com.howie.pojo.Admin;
import com.howie.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import org.junit.Before;
import org.junit.Test;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @Author weihuanwen
* @Date 2019/8/6 8:18
* @Version 1.0
*/
public class Operation {
private QueryRunner qr;
@Before
public void excuteBefore(){
qr = new QueryRunner(C3P0Utils.getDataSource());
}
/**
* 将结果集中第一行数据封装成对象数组
* [1, Jhon, 12345]
* @throws SQLException
*/
@Test
public void getDataAH() throws SQLException {
String sql = "SELECT * FROM `admin`";
Object[] resultArr = qr.query(sql, new ArrayHandler());
System.out.println(Arrays.toString(resultArr));
}
/**
* 将结果集中每一行数据转为对象数组并存储于List集合中
* [[Ljava.lang.Object;@2d554825, [Ljava.lang.Object;@68837a77]
* @throws SQLException
*/
@Test
public void getDataALH() throws SQLException {
String sql = "SELECT * FROM `admin`";
List
4.测试:
测试结果见代码Operation的方法注释