簡介:該框架吸取IBatis設計思想和方法映射機制,結合模式匹配,將查詢結果直接映射成域對象模型,并支持分頁。
代碼如下:
package heroking.db;
import java.util.List;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.sql.*;
public class GeneralDao {
private Connection connection = null;
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
/**
* 執行插入,更新,刪除操作,返回值為影響記錄數。
* @param sql
* @param parameter
*/
public int executeUpdate(String sql, Object parameter) {
List result = new ArrayList();
//按模式匹配轉換sql語句
Pattern pattern = Pattern.compile("#[a-zA-Z]+#"); //模式匹配
Matcher matcher = pattern.matcher(sql);
String formatSql = matcher.replaceAll("?");
//連接數據源
PreparedStatement st = null;
try {
st = connection.prepareStatement(formatSql);
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
//添加預處理參數
int i = 1;
while (matcher.find()) {
String field = matcher.group();
String formatField = field.substring(1, field.length() - 1);
try {
st.setObject(i++, DataBaseUtil.excuteGetMethod(formatField, parameter));
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
try {
return st.executeUpdate();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return 0;
}
/**
* 查詢第pageCount頁面(每個頁面的記錄數為pageSize)的結果
* @param sql sql查詢
* @param parameter 參數對象
* @param pageSize 頁面結果集大小
* @param pageCount 頁面數
* @param resultClass 結果類
* @return List
*/
public List executeQuerys(String sql, Object parameter, int pageSize, int pageCount, Class resultClass){
List result = new ArrayList();
//按模式匹配轉換sql語句
Pattern pattern = Pattern.compile("#[a-zA-Z]+#"); //模式匹配
Matcher matcher = pattern.matcher(sql);
String formatSql = matcher.replaceAll("?");
//連接數據源
PreparedStatement st = null;
try {
//設置預處理狀態參數,滾動,只讀。
st = connection.prepareStatement(formatSql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
//添加預處理參數
int i = 1;
while (matcher.find()) {
String field = matcher.group();
String formatField = field.substring(1, field.length() - 1);
try {
st.setObject(i++, DataBaseUtil.excuteGetMethod(formatField, parameter));
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
ResultSet rs = null;
try {
rs = st.executeQuery();
//獲取總記錄數
rs.last();
int totalCount = rs.getRow();
//當前頁面第一條記錄位置
int curPagePosition = (pageCount -1)*pageSize + 1;
if (totalCount < curPagePosition) {
return null;
}
rs.beforeFirst();
rs.absolute(curPagePosition);
//封裝值對象
int k = 0;
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while (rs.next() && k < pageSize) {
Object o = resultClass.newInstance();
for (int j = 1; j <= cols; j++) {
String name = rsmd.getColumnName(j);
Object value = rs.getObject(j);//作通用類型處理,這樣row中的類型都是Object型的。
String voName = DataBaseUtil.toInValueName(name);
DataBaseUtil.executeSetMethod(voName, value, o);
}
result.add(o);
k++; //獲取數加1
}
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InstantiationException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return result;
}
/**
* 查詢所有紀錄
* @param sql
* @param parameter
* @param resultClass
* @return List
*/
public List executeQuerys(String sql, Object parameter, Class resultClass){
//默認為所有記錄
return this.executeQuerys(sql, parameter, Integer.MAX_VALUE, 1, resultClass);
}
/**
* 查詢單個記錄
* @param sql
* @param parameter
* @param resultClass
* @return Object
*/
public Object executeQuery(String sql, Object parameter, Class resultClass){
List list = executeQuerys(sql, parameter,resultClass);
if (list == null || list.size() == 0) {
return null;
}
return list.get(0);
}
}