import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import org.slf4j.Logger; public abstract class DataBaseDaoAbstract<T> { private static Logger logger = CocoLoggerFactory.getLogger(DataBaseDaoAbstract.class); protected T execQueryObject(String sql, Object ...params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
ResultSet result = stmm.executeQuery();
if(result.next()) {
T t = this.parse(result);
return t;
}
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
return null;
} protected T execQueryObject(String sql, Collection<Object> params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
ResultSet result = stmm.executeQuery();
if(result.next()) {
T t = this.parse(result);
return t;
}
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
return null;
} protected <K> K execQueryObject(String sql, Class<K> clazz, Object ...params) throws Exception {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
ResultSet result = stmm.executeQuery();
if(result.next()) {
if(this.isSimpleClass(clazz)) {
return (K) this.getValue(result, clazz, 1);
}
K instance = clazz.newInstance();
Map<Integer, Method> columnMethodMap = this.generateMethodMap(clazz, result.getMetaData());
for(Entry<Integer, Method> entry : columnMethodMap.entrySet()) {
Object param = this.getValue(result, entry.getValue().getParameterTypes()[0], entry.getKey());
entry.getValue().invoke(instance, param);
}
return instance;
}
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
return null;
}
protected <K> K execQueryObject(String sql, Class<K> clazz, Collection<Object> params) throws Exception {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
ResultSet result = stmm.executeQuery();
if(result.next()) {
if(this.isSimpleClass(clazz)) {
return (K) this.getValue(result, clazz, 1);
}
K instance = clazz.newInstance();
Map<Integer, Method> columnMethodMap = this.generateMethodMap(clazz, result.getMetaData());
for(Entry<Integer, Method> entry : columnMethodMap.entrySet()) {
Object param = this.getValue(result, entry.getValue().getParameterTypes()[0], entry.getKey());
entry.getValue().invoke(instance, param);
}
return instance;
}
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
return null;
} protected <K> List<K> execQueryObjects(String sql, Class<K> clazz, Object ...params) throws Exception {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
List<K> returnList = Lists.newArrayList();
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
ResultSet result = stmm.executeQuery();
if(this.isSimpleClass(clazz)) {
while(result.next()) {
returnList.add((K) this.getValue(result, clazz, 1));
}
}
Map<Integer, Method> columnMethodMap = this.generateMethodMap(clazz, result.getMetaData());
while(result.next()) {
K instance = clazz.newInstance();
for(Entry<Integer, Method> entry : columnMethodMap.entrySet()) {
Object param = this.getValue(result, entry.getValue().getParameterTypes()[0], entry.getKey());
entry.getValue().invoke(instance, param);
}
returnList.add(instance);
}
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
return returnList;
} protected <K> List<K> execQueryObjects(String sql, Class<K> clazz, Collection<Object> params) throws Exception {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
List<K> returnList = Lists.newArrayList();
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
ResultSet result = stmm.executeQuery();
if(this.isSimpleClass(clazz)) {
while(result.next()) {
returnList.add((K) this.getValue(result, clazz, 1));
}
}
Map<Integer, Method> columnMethodMap = this.generateMethodMap(clazz, result.getMetaData());
while(result.next()) {
K instance = clazz.newInstance();
for(Entry<Integer, Method> entry : columnMethodMap.entrySet()) {
Object param = this.getValue(result, entry.getValue().getParameterTypes()[0], entry.getKey());
entry.getValue().invoke(instance, param);
}
returnList.add(instance);
}
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
return returnList;
} private boolean isSimpleClass(Class<?> clazz) {
if(clazz.isPrimitive()) {
return true;
}
if(clazz.equals(Integer.class) || clazz.equals(Long.class)
|| clazz.equals(Short.class) || clazz.equals(String.class)
|| clazz.equals(Float.class) || clazz.equals(Double.class)
|| clazz.equals(BigDecimal.class) || clazz.equals(Byte.class)
|| clazz.equals(Boolean.class) || Date.class.isAssignableFrom(clazz)) {
return true;
}
return false; } private Map<Integer, Method> generateMethodMap(Class<?> clazz, ResultSetMetaData metaData) throws SQLException {
Map<Integer, Method> columnMethodMap = Maps.newHashMap();
Map<String, Method> methodMap = Maps.newHashMap();
Method[] methods = clazz.getMethods();
for(int i = 0; i < methods.length; i++) {
String methodName = methods[i].getName();
if(methodName.startsWith("set") && methods[i].getParameterTypes().length == 1) {
methodName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
methodMap.put(methodName, methods[i]);
}
}
for(int i = 1; i <= metaData.getColumnCount(); i++) {
Method method = methodMap.get(metaData.getColumnLabel(i));
if(method == null) {
continue;
}
columnMethodMap.put(i, method);
}
return columnMethodMap;
} private Object getValue(ResultSet result, Class<?> paramterType, int columnIndex) throws SQLException {
if(result.getObject(columnIndex) == null && !paramterType.isPrimitive()) {
return null;
}
if(paramterType.equals(int.class) || paramterType.equals(Integer.class)) {
return result.getInt(columnIndex);
}
if(paramterType.equals(long.class) || paramterType.equals(Long.class)) {
return result.getLong(columnIndex);
}
if(paramterType.equals(Byte.class) || paramterType.equals(byte.class)) {
return result.getByte(columnIndex);
}
if(paramterType.equals(Short.class) || paramterType.equals(short.class)) {
return result.getShort(columnIndex);
}
if(paramterType.equals(Boolean.class) || paramterType.equals(boolean.class)) {
return result.getBoolean(columnIndex);
}
if(paramterType.equals(Float.class) || paramterType.equals(float.class)) {
return result.getFloat(columnIndex);
}
if(paramterType.equals(Double.class) || paramterType.equals(double.class)) {
return result.getDouble(columnIndex);
}
if(paramterType.equals(BigDecimal.class)) {
return result.getBigDecimal(columnIndex);
}
return result.getObject(columnIndex);
} private void addParamters(PreparedStatement stmm, Object ...params) throws SQLException {
if(params != null && params.length != 0) {
int i = 1;
for(Object param : params) {
if(param instanceof Integer) {
stmm.setInt(i, (Integer)param);
} else if (param instanceof Long) {
stmm.setLong(i, (Long)param);
} else if (param instanceof Byte) {
stmm.setByte(i, (Byte)param);
} else if (param instanceof Boolean) {
stmm.setBoolean(i, (Boolean)param);
} else if (param instanceof String) {
stmm.setString(i, (String) param);
} else if(param instanceof Short) {
stmm.setShort(i, (Short) param);
} else {
stmm.setObject(i, param);
}
i ++;
}
}
}
private void addParamters(PreparedStatement stmm, Collection<Object> params) throws SQLException {
if(params != null && params.size() != 0) {
int i = 1;
for(Object param : params) {
if(param instanceof Integer) {
stmm.setInt(i, (Integer)param);
} else if (param instanceof Long) {
stmm.setLong(i, (Long)param);
} else if (param instanceof Byte) {
stmm.setByte(i, (Byte)param);
} else if (param instanceof Boolean) {
stmm.setBoolean(i, (Boolean)param);
} else if (param instanceof String) {
stmm.setString(i, (String) param);
} else if(param instanceof Short) {
stmm.setShort(i, (Short) param);
} else {
stmm.setObject(i, param);
}
i ++;
}
}
} protected List<T> execQueryObjects(String sql, Object ...params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
List<T> returnList = Lists.newArrayList();
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
ResultSet result = stmm.executeQuery();
while(result.next()) {
T t = this.parse(result);
returnList.add(t);
}
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
return returnList;
} protected List<T> execQueryObjects(String sql, Collection<Object> params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
List<T> returnList = Lists.newArrayList();
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
ResultSet result = stmm.executeQuery();
while(result.next()) {
T t = this.parse(result);
returnList.add(t);
}
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
return returnList;
} protected int execInsert(String sql, Object ...params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
return stmm.executeUpdate();
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
} protected int execInsert(String sql, Collection<Object> params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
return stmm.executeUpdate();
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
} protected int execInsertReturnId(String sql, String name, Object object, Object ...params ) throws Exception {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
int effectRows = stmm.executeUpdate();
String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
Method[] methods = object.getClass().getMethods();
Method methodInvoke = null;
for(Method method : methods) {
if(method.getName().equals(methodName)) {
methodInvoke = method;
}
}
if(methodInvoke != null) {
Object id = this.getConnectionId(connection, methodInvoke.getParameterTypes()[0]);
methodInvoke.invoke(object, id);
}
return effectRows;
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
} protected int execInsertReturnId(String sql, String name, Object object, Collection<Object> params ) throws Exception {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
int effectRows = stmm.executeUpdate();
String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
Method[] methods = object.getClass().getMethods();
Method methodInvoke = null;
for(Method method : methods) {
if(method.getName().equals(methodName)) {
methodInvoke = method;
}
}
if(methodInvoke != null) {
Object id = this.getConnectionId(connection, methodInvoke.getParameterTypes()[0]);
methodInvoke.invoke(object, id);
}
return effectRows;
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
} protected int execUpdate(String sql, Object ...params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
return stmm.executeUpdate();
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
} protected int execUpdate(String sql, Collection<Object> params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
return stmm.executeUpdate();
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
} protected int execDelete(String sql, Object ...params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
return stmm.executeUpdate();
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
} protected int execDelete(String sql, Collection<Object> params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
return stmm.executeUpdate();
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
} protected int selectCount(String sql, Object ...params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
ResultSet result = stmm.executeQuery();
result.next();
return result.getInt(1);
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
} protected int selectCount(String sql, Collection<Object> params) throws SQLException {
PooledDatabaseConnection connection = this.getConnection();
PreparedStatement stmm = null;
try {
stmm = connection.prepareStatement(sql);
this.addParamters(stmm, params);
ResultSet result = stmm.executeQuery();
result.next();
return result.getInt(1);
} finally {
DBUtil.close(stmm);
if(TransactionContainer2.needAutoCommit()) {
connection.pushConnection();
}
}
} private PooledDatabaseConnection getConnection() {
PooledDatabaseConnection connection = TransactionContainer2.getConnection();
if(connection != null) {
return connection;
} DataSourceConnection dataSource = this.getClass().getAnnotation(DataSourceConnection.class);
if(dataSource == null) {
throw new RuntimeException(this.getClass().getSimpleName() + " do not has Connection Conf");
}
Class<? extends IConnectionConfiguration> connectionConf = dataSource.connection();
connection = DatabaseConnectionPool.getPooledConnection(connectionConf);
if(connection == null) {
throw new RuntimeException("can not get db connection");
} if(!TransactionContainer2.needAutoCommit()) {
try {
connection.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
TransactionContainer2.setConnection(connection);
}
return connection;
} private Object getConnectionId(PooledDatabaseConnection conn, Class<?> type) throws SQLException {
String idQuery = "select @@Identity";
PreparedStatement idQuerySttmt = conn.prepareStatement(idQuery);
try {
ResultSet idQueryRs = idQuerySttmt.executeQuery();
idQueryRs.next();
return getValue(idQueryRs, type, 1);
} finally {
DBUtil.close(idQuerySttmt);
} } protected abstract T parse(ResultSet result) throws SQLException;
}

DataBaseDaoAbstract的更多相关文章

随机推荐

  1. css设置点击态样式

    .rightMenu:active { background-color: rgba(46, 103, 222, 0.13); }

  2. JAVA的原子性和可见性,线程同步的理解

    1.原子性 (1)原子是构成物质的基本单位(当然电子等暂且不论),所以原子的意思代表着——“不可分”: (2)原子性是拒绝多线程操作的,不论是多核还是单核,具有原子性的量,同一时刻只能有一个线程来对它 ...

  3. (02) 第一个springboot程序

    1. 创建一个springboot程序 1. idea 自带的springboot插件 2. 直接从https://start.spring.io 创建好程序下载下来, 之后覆盖你的创建的项目 2. ...

  4. 防止asp马后门

    好多朋友都拿的有webshell吧,基本上都加了密的...  可是,没见到源码,很难测试它到底有没有后门, 指不定给别人打工了...  下面贴种很简单的方法,大家别扔蛋哈 (asp的哦)  在代码的最 ...

  5. 入坑deep learning 1

    想体验一下跑keras的感觉,按照这个小妹妹的教程:https://zhuanlan.zhihu.com/p/28333410 0. 大概花了十来个小时才搞定初步的小环境 1. 在linux 16.0 ...

  6. 调用 LoadLibraryEx 失败,在 ISAPI 筛选器 "C:\Program Files\php\php5isapi.dll"

    把 ISAPI 筛选器这里的php配置删掉,php改用fastcgi配置

  7. TensorFlow函数:tf.reduce_sum

    tf.reduce_sum 函数 reduce_sum ( input_tensor , axis = None , keep_dims = False , name = None , reducti ...

  8. summernote富文本编辑器

    下载summernote官方demo,解压,把文件夹中的summernote.js,summernote.css和font整个文件夹都放到服务器对应的项目目录里 引入summernote 所需要的bo ...

  9. python学习2:turtle的使用蟒蛇绘制的学习以及自己摸索的等边三角形绘制(跟随mooc学习)

    首先先放上蟒蛇的绘制程序 import turtle#引入外部库#def保留字用于 定义函数 def drawSnake(rad,angle,len,neckrad): for i in range( ...

  10. 周强 201771010141 《面向对象程序设计(java)》 第二周学习总结

    第一部分:理论知识学习部分 第三章 java的基本程序设计结构 本章主要学习数据类型.变量.运算符.类型转换.字符串.输入输出.控制流程.大数值.数组等内容. 1.基本知识 (1)标识符:由字母.下划 ...