要想重復(fù)造輪子就要造得好用一些才行:
我也造了一個(gè)輪子,下面這樣的效果。
代碼片段:
// 構(gòu)建DAO工廠
AbstractDAOFactory<AccountPOJO, Long> factory = AbstractDAOFactory.newInstance();
// 構(gòu)建DAO實(shí)例
IGenericsDAO<AccountPOJO, Long> dao = factory.buildGenericsDAO();
// 按條件檢索數(shù)據(jù)并自動(dòng)映射成POJO有序集合
List<AccountPOJO> pojos = dao.find(
AccountPOJO.class,
Condition.valueOf("accountid", Condition.LE, 10),
Condition.and("birthday", Condition.ISNULL),
Condition.or("accountname", Condition.LIKE, "鄧%"));
// 在控制臺(tái)輸出結(jié)果
for (AccountPOJO account : pojos) {
System.out.println(account);
}
在控制臺(tái)輸出的調(diào)試信息為:
2010-04-14 00:48:58,281 main DEBUG [com.china.codingmouse.cmsdk4j.dao.sql.generator.SQLGenerator]
[CmSdk4j Auto SQL Generator In 2010-04-14 00:48:58.281]
SELECT accountid, accountname, onlinecount, birthday, loginname, loginpassword, email, accountstate FROM account WHERE accountid <= ? AND birthday IS NULL OR accountname LIKE ?
------------------------------
2010-04-14 00:48:58,328 main DEBUG [com.china.codingmouse.cmsdk4j.dao.converter.DataTypeConverter]
[SQL Parameter List Information In 2010-04-14 00:48:58.328]
No:1
Type:java.lang.Integer
Value:10
------------------------------
2010-04-14 00:48:58,343 main DEBUG [com.china.codingmouse.cmsdk4j.dao.converter.DataTypeConverter]
[SQL Parameter List Information In 2010-04-14 00:48:58.343]
No:2
Type:java.lang.String
Value:鄧%
------------------------------
在控制臺(tái)輸出的程序結(jié)果為:
[ accountid = 1, accountname = 鄧超, onlinecount = 2203, birthday = 1984-12-26, loginname = CodingMouse, loginpassword = cmsdk4j, email = CodingMouse@gmail.com, accountstate = true ]
[ accountid = 3, accountname = 李四, onlinecount = 932, birthday = null, loginname = LiSi, loginpassword = lisi123, email = lisi123@126.com, accountstate = false ]
[ accountid = 7, accountname = 鄧遠(yuǎn)辰, onlinecount = 429, birthday = null, loginname = ChengCheng, loginpassword = chengchengpwd, email = chengchengdream@gmail.com, accountstate = true ]
我也非常支持重復(fù)造輪子的行為,我上面那個(gè)泛型DAO的find方法實(shí)現(xiàn)是這樣的:
/**
* 獲取該類型匹配的模型有序集合。<br><br>
*
* @param clz 注冊(cè)返回類型。
* @param conditions SQL條件列表。
* @return 該類型匹配的全部模型有序集合。
* @throws DataAccessException 數(shù)據(jù)訪問異常。
*/
public List<T> find(Class<? extends Object> clz, Condition... conditions)
throws DataAccessException {
return this.findAction.find(clz, conditions);
}
這段時(shí)間一直也在自己琢磨動(dòng)態(tài)SQL的拼裝問題,但總是顯得對(duì)復(fù)雜SQL的支持還不夠。也許還是只有單獨(dú)封裝個(gè)高內(nèi)聚的SQL查詢條件包裝器才能達(dá)到效果。
由于我的DAO抽象基類還是采用的短事務(wù)處理方式,所以,總感覺還是不能完全滿足需求:
/**
* 執(zhí)行查詢SQL命令并返回模型有序集合。<br><br>
*
* @param clz 注冊(cè)模型類型。
* @param sql 要執(zhí)行的帶占位符SQL命令字串。
* @param param SQL參數(shù)列表。
* @return 泛型模型集合。
* @throws DataAccessException 數(shù)據(jù)訪問異常。
*/
public List<Object> executeQuery(
Class<? extends Object> clz,
String sql,
Parameter... param)
throws DataAccessException {
// 數(shù)據(jù)庫連接對(duì)象
Connection conn = null;
// SQL命令執(zhí)行對(duì)象
PreparedStatement ps = null;
// 結(jié)果集對(duì)象
ResultSet rs = null;
// JDBC事務(wù)管理對(duì)象
DBTransaction trans = null;
try {
conn = DB_CONNECTION_POOL.getConnection();
trans = DBTransaction.begin(conn);
ps = DataTypeConverter.java2Jdbc(
conn.prepareStatement(sql),
param);
rs = ps.executeQuery();
List<Object> modelList = new Vector<Object>();
while(rs.next()) {
Object model = DataTypeConverter.jdbc2Java(
clz,
rs);
modelList.add(model);
}
trans.commit();
return modelList;
} catch (Throwable e) {
trans.rollback();
logger.error(
this.getClass().getName(),
Logger.DIR_INTERNAL,
null,
Logger.getStackTrace(e));
throw new DataAccessException(e);
} finally {
this.closeAll(rs, ps, conn, trans);
}
}
回復(fù) 更多評(píng)論