泛型(三)模拟commons-dbutils
最近在复习泛型的知识,想起以前使用commons-dbutils的时候,觉得这个工具太厉害了。所以,试着自己瞎写看能不能模拟commons-dbutils的功能。
1、commons-dbutils的使用
1.1、commons-dbutils是用来简化JDBC的代码。下面是其简单用法:
// 增删改
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());// 创建QueryRunner,需要提供数据库连接池对象
String sql = "insert into t_students values(?,?,?,?)";// 给出sql模板
Object[] params = { 1, "liSi", 20, "female" };// 给出sql模板的参数
qr.update(sql, params); // 查询
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from t_student where id = ?";
Object[] params = {1};
Stu stu = qr.query(sql, new BeanHandler<Stu>(Stu.class), params);
// 将结果集rs映射成javabean,要求结果集列名与javabean属性名一致
1.2、commons-dbutils的其他查询用法
* BeanListHandler的应用,它是多行处理器
- QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from t_stu";
List<Stu> stuList = qr.query(sql,new BeanListHandler<Stu>(Stu.class));
* MapHandler的应用,它是单行处理器,把一行转换成一个Map对象
- QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from t_stu where sid = ?";
Object[] params = {1001};
Map map = qr.query(sql,new MapHandler(), params);
* MapListHandler,它是多行处理器,把每行都转换成一个Map
- QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from t_stu";
List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler());
* ScalarHandler的应用,它是单行单列时使用,最为合适
- QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select count(*) from t_stu";
Object obj = qr.query(sql,new ScalarHandler());
2、模拟commons-dbutils

2.1、工具类
CommonUtils工具类的作用见我的博客:泛型的使用:封装工具类CommonUtils-把一个Map转换成指定类型的javabean对象(用到泛型)
package com.oy.type;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils; public class CommonUtils { // 把一个Map转换成指定类型的javabean对象
public static <T> T tobean(Map<String, ?> map, Class<T> clazz) {
try {
T bean = clazz.newInstance();
BeanUtils.populate(bean, map);
return bean;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
JdbcUtils类是用来获取数据库连接的,用到了c3p0连接池。
package com.oy.type;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtils { // 使用配置文件c3p0-config.xml, 放到src目录下
private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 返回连接
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
} // 返回连接池对象
public static DataSource getDataSource() {
return dataSource;
}
}
c3p0-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF-8</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password"></property>
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</default-config>
<named-config name="name1">
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db_test</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123</property>
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>
2.2、QR类
package com.oy.type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource; public class QR<T> {
private DataSource dataSource; public QR() {
super();
} public QR(DataSource dataSource) {
super();
this.dataSource = dataSource;
} // 可以做增删改操作
public int update(String sql, Object... params) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = dataSource.getConnection(); // 通过连接池得到连接对象
pstmt = con.prepareStatement(sql);// 使用sql模板创建preparedStatement对象
initParams(pstmt, params);// 对sql语句的?赋值
return pstmt.executeUpdate(); // 执行
} catch (Exception e) {
throw new RuntimeException(e);
} finally { // 释放资源
try {
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e1) {
}
}
} // 可以做查询操作
public T query(String sql, BeanHandler<T> rh, Object... params) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection(); // 通过连接池得到连接对象
pstmt = con.prepareStatement(sql);// 使用sql模板创建preparedStatement对象
initParams(pstmt, params);// 对sql语句的?赋值
rs = pstmt.executeQuery(); // 执行查询,返回ResultSet对象 return rh.handle(rs);// 传入结果集rs,得到T类型的对象
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
}
}
} public List<T> query(String sql, BeanListHandler<T> rh, Object... params) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection(); // 通过连接池得到连接对象
pstmt = con.prepareStatement(sql);// 使用sql模板创建preparedStatement对象
initParams(pstmt, params);// 对sql语句的?赋值
rs = pstmt.executeQuery(); // 执行查询,返回ResultSet对象 return rh.handle(rs);// 传入结果集rs,得到T类型的对象
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
}
}
} // 给sql语句的?赋值
private void initParams(PreparedStatement pstmt, Object... params) throws SQLException {
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
}
}
2.3、BeanHandler:将查询的结果集转换成javabean
package com.oy.type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class BeanHandler<T> {
private Class<T> clazz; public BeanHandler(Class<T> clazz) {
this.clazz = clazz;
} public T handle(ResultSet rs) throws SQLException {
if (!rs.next())
return null; return CommonUtils.tobean(rsToMap(rs), clazz);
} private Map<String, String> rsToMap(ResultSet rs) throws SQLException {
Map<String, String> map = new HashMap<String, String>();
int count = rs.getMetaData().getColumnCount(); // 获取结果集的列数
for (int i = 1; i <= count; i++) { // 循环列
String columnName = rs.getMetaData().getColumnName(i);// 获取列名
String value = rs.getString(i); // 获取结果集一行中的每一列的值
map.put(columnName, value);
}
return map;
}
}
2.4、BeanListHandler:将结果集转换成bean集合
package com.oy.type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class BeanListHandler<T> {
private Class<T> clazz; public BeanListHandler(Class<T> clazz) {
this.clazz = clazz;
} public List<T> handle(ResultSet rs) throws SQLException {
List<T> list = new ArrayList<>();
Map<String, String> map = null; while (rs.next()) { // 循环行
map = new HashMap<String, String>();
int count = rs.getMetaData().getColumnCount(); // 获取结果集的列数
for (int i = 1; i <= count; i++) { // 循环列
String columnName = rs.getMetaData().getColumnName(i);// 获取列名
String value = rs.getString(i); // 获取结果集一行中的每一列的值
map.put(columnName, value);
}
list.add(CommonUtils.tobean(map, clazz));
} return list;
}
}
2.5、MapHandler:将结果集转换成Map
package com.oy.type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map; public class MapHandler { public MapHandler() {} public Map<String, String> handle(ResultSet rs) throws SQLException {
if (!rs.next())
return null;
return rsToMap(rs);
} private Map<String, String> rsToMap(ResultSet rs) throws SQLException {
Map<String, String> map = new HashMap<String, String>();
int count = rs.getMetaData().getColumnCount(); // 获取结果集的列数
for (int i = 1; i <= count; i++) { // 循环列
String columnName = rs.getMetaData().getColumnName(i);// 获取列名
String value = rs.getString(i); // 获取结果集一行中的每一列的值
map.put(columnName, value);
}
return map;
}
}
3、测试
package com.oy.type;
public class Stu {
private Integer id;
private Integer age;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Stu [id=" + id + ", age=" + age + ", name=" + name + "]";
} }
package com.oy.type;
import java.util.List;
import org.junit.jupiter.api.Test; public class StuDao {
QR<Stu> qr = new QR<>(JdbcUtils.getDataSource()); public Stu getStuById(Integer id) {
String sql = "select * from stu where id = ?";
Object[] params = {id};
return qr.query(sql, new BeanHandler<Stu>(Stu.class), params);
} public List<Stu> getStus(String ids) {
String sql = "select * from stu where id in (" + ids + ")";
return qr.query(sql, new BeanListHandler<Stu>(Stu.class));
} @Test
public void test1() {
//System.out.println(getStuById(1));
System.out.println(getStus("1,2"));
}
}
泛型(三)模拟commons-dbutils的更多相关文章
- 写一个ORM框架的第一步(Apache Commons DbUtils)
新一次的内部提升开始了,如果您想写一个框架从Apache Commons DbUtils开始学习是一种不错的选择,我们先学习应用这个小“框架”再把源代码理解,然后写一个属于自己的ORM框架不是梦. 一 ...
- 高性能jdbc封装工具 Apache Commons DbUtils 1.6(转载)
转载自原文地址:http://gao-xianglong.iteye.com/blog/2166444 前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更多的 ...
- Apache Commons DbUtils 快速上手
原文出处:http://lavasoft.blog.51cto.com/62575/222771 Hibernate太复杂,iBatis不好用,JDBC代码太垃圾,DBUtils在简单与优美之间取得了 ...
- 《笔者带你剖析Apache Commons DbUtils 1.6》(转)
前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更 多的是依靠Hibernate.Ibatis.Spring JDBC.JPA等大厂提供的持久层技术解决方案, ...
- WPF案例 (三) 模拟QQ“快速换装"界面
原文:WPF案例 (三) 模拟QQ"快速换装"界面 这个小程序使用Wpf模拟QQ快速换装页面的动画特效,通过使用组合快捷键Ctrl+Left或Ctrl+Right,可实现Image ...
- java JDBC (七) org.apache.commons.dbutils 查询
package cn.sasa.demo1; import java.sql.Connection; import java.sql.SQLException; import java.util.Li ...
- java JDBC (六) org.apache.commons.dbutils 增删改
dbutils是apache封装了JDBC的工具类,比mysql-connector更方便些 下载地址:http://commons.apache.org/proper/commons-dbutils ...
- Java连接数据库 #04# Apache Commons DbUtils
索引 通过一个简单的调用看整体结构 Examples 修改JAVA连接数据库#03#中的代码 DbUtils并非是什么ORM框架,只是对原始的JDBC进行了一些封装,以便我们少写一些重复代码.就“用” ...
- commons.dbutils 的使用列子
c0p3的导入请参考前文 https://www.cnblogs.com/appium/p/10183016.html JdbcUtils: package cn.itcast.jdbc; impor ...
- java.lang.ClassNotFoundException: org.apache.commons.dbutils.QueryRunner
七月 28, 2017 11:06:33 下午 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() fo ...
随机推荐
- k-交叉验证KFold
交叉验证的原理放在后面,先看函数. 设X是一个9*3的矩阵,即9个样本,3个特征,y是一个9维列向量,即9个标签.现在我要进行3折交叉验证. 执行kFold = KFold(n_splits=3) : ...
- pandas的.columns和.index
可以通过.columns和.index着两个属性返回数据集的列索引和行索引 设data是pandas的一个DataFram类型的数据集. 则data.index返回一个index类型的行索引列表,da ...
- 爬虫六之爬取猫眼电影top100
该爬虫比较简单,代码放在github上 https://github.com/GhostSteven/Crawler/tree/master/maoyantop100
- 找工作Java面试 题搜集
面向对象的特征有哪些方面?答:面向对象的特征主要有以下几个方面: 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注这些行为的 ...
- docker mysql 容器报too many connections 引发的liunx磁盘扩容操作
症状每次删除mysql容器重启没两分钟又报标题错 df -h 命令查看各个挂载空间应用情况发现root home var 三个文件目录挂载的空间满了 网上百度了一下liunx磁盘扩容操作,fdisk ...
- java 编码设计细节
1.hibernate注解 @Validated({ APIGetsGroup.class })@NotBlank(message = "{cameraReceive.captureId.e ...
- System.InsufficientMemoryException:无法分配536870912字节的托管内存缓冲区。可用内存量可能不足
一个病人住院太久,一次性打印护理表单超过3000条时报如标题所示的错误, 个人查阅分析应该可以从如下几方面入手: 一:查看程序客户端和服务端的配置文件相关属性是否限制了缓存最大值 (应该不是这个问题, ...
- ubuntu 设置sudo 免密码
一. 修改sudoers的权限 二. 修改sudoers 文件 <1>. 在文件最后一行添加yourusername ALL=(ALL) NOPASSWD : ALL 三. 修改回sudo ...
- Forsaken给学生分组
链接:https://ac.nowcoder.com/acm/contest/1221/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- 优雅的用两种方式爬网络 txt 文件【雾
TXT 文件?? (笑 这里爬的是 74xsw (咱好像也不怎么逛的网站)的英雄再临 ... 请注意这并不是教程,只是贴个代码仅供参考而已[雾 这里 用的 getTXT 的方式有两种,一种是每个章节分 ...