1. 程式人生 > >使用泛型類簡化ibatis系統架構

使用泛型類簡化ibatis系統架構

sqlmap val imp 思路 xtend value emp dsm 很大的

jdk1.5的推出為我們帶來了枚舉、泛型、foreach循環、同步工具包等等好東西。其中,泛型的使用為我們的代碼開發提供了很大的簡便,簡化了我們的代碼。

1、設計思路

1)GenericDao泛型類提供所有的增刪改查功能;

2)所有的dao在繼承GenericDao泛型類擁有自身的增刪改查功能,不需再寫其他代碼。

2、實現GenericDao,基類

public abstract class GenericDao<T, PK extends Serializable> {

protected final Logger logger = LoggerFactory.getLogger(getClass());

public static final String POSTFIX_SELECTBYID = ".selectById";
public static final String POSTFIX_SELECTBYIDS = ".selectByIds";
public static final String POSTFIX_SELECTBYMAP = ".selectByMap";
public static final String POSTFIX_SELECTIDSLIKEBYMAP = ".selectIdsLikeByMap";
public static final String POSTFIX_PKSELECTMAP = ".pkSelectByMap";
public static final String POSTFIX_COUNT = ".count";
public static final String POSTFIX_COUNTLIKEBYMAP = ".countLikeByMap";
public static final String POSTFIX_INSERT = ".insert";
public static final String POSTFIX_DELETEBYID = ".deleteById";
public static final String POSTFIX_DELETEBYIDS = ".deleteByIds";
public static final String POSTFIX_DELETEBYIDSMAP = ".deleteByIdsMap";
public static final String POSTFIX_DELETEBYMAP = ".deleteByMap";
public static final String POSTFIX_UPDATE = ".update";
public static final String POSTFIX_UPDATEBYMAP = ".updateByMap";
public static final String POSTFIX_UPDATEBYIDSMAP = ".updateByIdsMap";

protected Class<T> clazz;

protected String clazzName;

protected T t;

// 定義主從數據庫服務器,主數據庫負責(Write op),從數據庫負責(read op)
@Autowired
protected SqlMapClientTemplate masterSqlMapClientTemplate;

@Autowired
protected SqlMapClientTemplate slaveSqlMapClientTemplate;

public GenericDao() {
// 通過範型反射,取得在子類中定義的class.
clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
clazzName = clazz.getSimpleName();
}

public Integer count(String propertyName, Object propertyValue) {
return count(new String[]{propertyName},new Object[]{propertyValue});
}

public Integer count(String[] propertyNames, Object[] propertyValues) {

Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < propertyNames.length; i++) {
map.put(propertyNames[i], propertyValues[i]);
}
return (Integer) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + POSTFIX_COUNT, map);
}

@Override
public Integer countLikeByMap(String[] propertyNames, Object[] propertyValues) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < propertyNames.length; i++) {
map.put(propertyNames[i], propertyValues[i]);
}
return (Integer) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + POSTFIX_COUNTLIKEBYMAP, map);
}

/** 根據自定義SqlMap中的條件語句查詢出記錄數量 */
public Integer countByStatementPostfix(String statementPostfix,String[] properties, Object[] propertyValues){
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
return (Integer) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + statementPostfix, map);
}
/**
* 通過id得到實體對象
*/
public T findById(PK id) {
return (T) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + POSTFIX_SELECTBYID, id);

}

/**
* 根據ids獲取實體列表
*
* @param ids
* @return
*/
public List<T> findByIds(List<PK> ids) {
return (List<T>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTBYIDS, ids);
}

/**
* 直接從數據庫查詢出ids列表(包括符合條件的所有id)
*
* @param properties
* 查詢條件字段名
* @param propertyValues
* 字段取值
* @return
*/
public List<PK> findIdsBy(String[] properties, Object[] propertyValues, String orderBy, String order) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);
}

/**
* 根據條件查詢結果
*
* @param properties
* 查詢條件名
* @param propertyValues
* 查詢條件值
* @return
*/
public List<T> findByMap(String[] properties, Object[] propertyValues, String orderBy, String order) {
return (List<T>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTBYMAP, map);
}

/**
* 根據條件查詢結果的id列表
*
* @param properties
* 查詢條件名
* @param propertyValues
* 查詢條件值
* @return
*/
public List<PK> findIdsByMap(String[] properties, Object[] propertyValues, String orderBy, String order) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);
}

/**
* 分頁查詢(未處理緩存)
*
* @param properties
* 查詢條件字段名
* @param propertyValues
* 字段取值
* @return
*/
public List<T> pageQueryBy(String[] properties, Object[] propertyValues, String orderBy, String order, int pageSize, int pageNo) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
map.put("limit", true);
map.put("start", (pageNo - 1) * pageSize );// limit 操作
map.put("end", pageSize);
return (List<T>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTBYMAP, map);
}

/**
* 從數據庫分頁查詢出ids列表的前200個id
*
* @param properties
* 查詢條件字段名
* @param propertyValues
* 字段取值
* @return
*/
protected List<PK> pageQueryIdsBy(String[] properties, Object[] propertyValues, String orderBy, String order) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
map.put("limit", true);
map.put("start", 0);// 操作
map.put("end", Config.getInstance().getPageCacheSize());
return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);

}

/**
* 分頁查詢出id列表(處理緩存)
*
* @param properties
* 查詢條件字段名
* @param propertyValues
* 字段取值
* @return
*/
public List<PK> pageQueryIdsByMap(String[] properties, Object[] propertyValues, String orderBy, String order, int pageSize, int pageNo) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
map.put("limit", true);
map.put("start", (pageNo - 1) * pageSize );// limit 操作
map.put("end", pageSize);
return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);
}

/** like分頁查詢(不走列表緩存) */
public List<T> pageLikeBy(String[] properties, Object[] propertyValues, String orderBy, String order, int pageSize, int pageNo){
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
map.put("limit", true);
map.put("start", (pageNo - 1) * pageSize );// limit 操作
map.put("end", pageSize);
List<PK> ids = (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTIDSLIKEBYMAP, map);
return findByIds(ids);
}

/**
* 新增對象
*/
public Serializable insert(T o) throws Exception {
if (t instanceof QueryCachable) {
// 清除受影響的緩存
clearListCache(o);
}
return (Serializable) masterSqlMapClientTemplate.insert(clazz.getName() + POSTFIX_INSERT, o);
}


/**
* 更新對象
*/
public T update(T o) throws Exception {
masterSqlMapClientTemplate.update(clazz.getName() + POSTFIX_UPDATE, o);
return o;
}

/**
* 更新對象的部分屬性
*/
public int update(PK id, String[] properties, Object[] propertyValues) throws Exception {
// 更新數據庫
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
map.put("id", id);
return masterSqlMapClientTemplate.update(clazz.getName() + POSTFIX_UPDATEBYMAP, map);
}

/**
* 根據ID列表更新對象的部分屬性
*/
public int updateByIdsMap(List<PK> ids,String[] properties, Object[] propertyValues) throws Exception{
// 更新數據庫
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
map.put("ids", ids);
return masterSqlMapClientTemplate.update(clazz.getName() + POSTFIX_UPDATEBYIDSMAP, map);
}

/**
* 根據ID刪除對象
*/
public void deleteById(PK id) throws Exception {
masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYID, id);
}

/**
* 根據ID刪除對象
*/
public void deleteByIds(List<PK> ids) throws Exception {
masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYIDS, ids);
}

/** 根據ID及條件刪除對象 */
public void deleteByIdsMap(List<PK> ids, String[] properties, Object[] propertyValues) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
map.put("ids", ids);
masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYIDSMAP, map);
}

/**
* 根據條件刪除對象
*/
public int deleteByMap(String[] properties, Object[] propertyValues) throws Exception {

Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
return masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYMAP, map);
}

/**
* 根據自定義SqlMap中的條件語句查詢出列表(註意:不處理緩存)
*/
public List<T> findByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues, String orderBy, String order) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
return slaveSqlMapClientTemplate.queryForList(clazz.getName() + statementPostfix, map);
}

/**
* 根據自定義SqlMap中的條件語句查詢出列表(註意:不處理緩存)
*/
public List<T> pageQueryByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues, String orderBy, String order,int pageSize,int pageNo) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
map.put("limit", true);
map.put("start", (pageNo - 1) * pageSize );// limit 操作
map.put("end", pageSize);
List<PK> ids = (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + statementPostfix, map);
return findByIds(ids);
}

/**
* 根據自定義SqlMap中的條件語句更新數據(註意:不處理緩存)
*/
public void updateByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
masterSqlMapClientTemplate.update(clazz.getName() + statementPostfix, map);
}

/**
* 根據自定義SqlMap中的條件語句刪除數據(註意:不處理緩存)
*/
public void deleteByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
masterSqlMapClientTemplate.delete(clazz.getName() + statementPostfix, map);
}

/**
* 根據自定義SqlMap中的條件語句插入數據(註意:不處理緩存)
*/
public void insertByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
masterSqlMapClientTemplate.insert(clazz.getName() + statementPostfix, map);
}

}

3、所有其他Dao只需繼承GenericDao,傳個具體的類型,不需再寫增刪改查的方法

package com.teacherclub.business.album.dao;

import org.springframework.stereotype.Repository;

import com.teacherclub.business.album.entity.Album;
import com.teacherclub.core.mem.MemcacheUtil;
import com.teacherclub.core.orm.EntityCachable;
import com.teacherclub.core.orm.dao.impl.GenericDao;

@Repository
public class AlbumDao extends GenericDao<Album,Integer> { //傳參數Album類

}

此泛型的實現,關鍵在於:clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

這在項目中經常使用。

使用泛型類簡化ibatis系統架構