com.iih5.smartorm.model.DbExecutor Maven / Gradle / Ivy
package com.iih5.smartorm.model;
import com.iih5.smartorm.kit.SpringKit;
import com.iih5.smartorm.kit.StringKit;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import javax.sql.DataSource;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
/**
* DbExecutor. Professional database query and update tool.
*/
public class DbExecutor {
private static Map map = new HashMap();
private static String defaultDataSource = null;
public JdbcTemplate jdbc = null;
/**
* 选择使用数据库(默认选中第一个)
*
* @param dataSource 在spring.xml里配置的jdbc dataSource beanId
* @return 返回DbExecutor.
*/
public static DbExecutor use(String dataSource) {
DbExecutor executor = map.get(dataSource);
if (executor == null) {
executor = new DbExecutor();
executor.jdbc = SpringKit.getJdbcTemplateByDataSource(dataSource);
map.put(dataSource, executor);
}
return executor;
}
/**
* 默认第一个dataSource
*
* @return DbExecutor
*/
public static DbExecutor use() {
if (defaultDataSource == null) {
String[] dbs = SpringKit.getApplicationContext().getBeanNamesForType(DataSource.class);
defaultDataSource = dbs[0];
}
DbExecutor executor = map.get(defaultDataSource);
if (executor == null) {
executor = new DbExecutor();
executor.jdbc = SpringKit.getJdbcTemplateByDataSource(defaultDataSource);
map.put(defaultDataSource, executor);
}
return executor;
}
/**
* 返回JdbcTemplate
*
* @param dataSource
* @return
*/
public JdbcTemplate getJdbcTemplate(String dataSource) {
DbExecutor executor = map.get(dataSource);
if (executor != null) {
return executor.jdbc;
}
return null;
}
/**
* 返回JdbcTemplate
*
* @param
* @return
*/
public JdbcTemplate getJdbcTemplate() {
return getJdbcTemplate(defaultDataSource);
}
private List queryList(String sql, Object[] paras, final Class claszz) {
if (StringKit.isBaseDataType(claszz)){
return jdbc.queryForList(sql,paras,claszz);
}
final Set columnMeta = new HashSet();
final Map fieldMap = new HashMap();
return jdbc.query(sql, paras, new RowMapper() {
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
try {
if (columnMeta.size() <= 0) {
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
String column = rs.getMetaData().getColumnLabel(i + 1);
columnMeta.add(column);
}
}
Object mModel = claszz.newInstance();
Field[] fields = mModel.getClass().getDeclaredFields();
if (fields.length > 0) {
for (Field field : fields) {
String column = fieldMap.get(field.getName());
if (column == null) {
column = StringKit.toUnderscoreName(field.getName());
fieldMap.put(field.getName(), column);
}
if (columnMeta.contains(column)) {
Object value = rs.getObject(column);
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), mModel.getClass());
Method method = pd.getWriteMethod();
method.invoke(mModel, value);
}
}
}
return (T) mModel;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
}
/**
* 查找Model对象列表
*
* @param sql sql语句 参数用?代替
* @param paras 参数
* @param claszz
* @param 返回对象
* @return
* @
*/
public List findList(String sql, Object[] paras, Class claszz) {
return queryList(sql, paras, claszz);
}
/**
* 获取Map格式列表
*
* @param sql sql语句 参数用?代替
* @param paras 参数
* @return 返回 List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy