Apache 开源JDBC工具类库
commons-dbutils jar:下载
package com.jdbc.tools; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.sql.SQLException; import java.util.List; public abstract class BasicDAOImpl2 <T> { //type代表T的实际类型 private Class<T> type; private QueryRunner qr=new QueryRunner(); //在创建子类对象时,一定会调用父类构造器,默认调用父类无参构造 public BasicDAOImpl2(){ //this是正在new的对象 //clazz就是正在new对象的那个子类的类型的Class对象 Class<? extends BasicDAOImpl2> clazz = this.getClass(); Type t=clazz.getGenericSuperclass(); ParameterizedType pt=(ParameterizedType) t; Type[] types=pt.getActualTypeArguments(); type= (Class) types[0]; } public int update(String sql, Object...args) throws SQLException { return qr.update(JDBCToolsV3.getConnection(),sql,args); } public T getBean(String sql, Object...args) throws SQLException { return qr.query(JDBCToolsV3.getConnection(),sql,new BeanHandler<>(type),args); } public List<T> getBeanList(String sql, Object... args) throws SQLException { return qr.query(JDBCToolsV3.getConnection(),sql,new BeanListHandler<>(type),args); } }
相关推荐
登峰小蚁 2020-06-10
ASoc 2020-11-14
Andrea0 2020-09-18
Cherishyuu 2020-08-19
dongtiandeyu 2020-08-18
CoderYYN 2020-08-16
大黑牛 2020-08-15
Dullonjiang 2020-08-11
gaozhennan 2020-08-03
mcvsyy 2020-08-02
zbcaicai 2020-07-29
AscaryBird 2020-07-27
liulin0 2020-07-26
ldcwang 2020-07-26
helloxusir 2020-07-25
娜娜 2020-07-20
pengpengflyjhp 2020-07-19
点滴技术生活 2020-07-19
人可 2020-07-18