`

mybatis泛型Dao参考三

阅读更多
 

使用泛型类简化ibatis系统架构

 2702人阅读 评论(6) 收藏 举报

jdk1.5的推出为我们带来了枚举、泛型、foreach循环、同步工具包等等好东西。其中,泛型的使用为我们的代码开发提供了很大的简便,简化了我们的代码。

1、设计思路

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

2)所有的dao在继承GenericDao泛型类拥有自身的增删改查功能,不需再写其他代码。

 

2、实现GenericDao

 

[java] view plaincopy
 
  1. public abstract class GenericDao<T, PK extends Serializable>  {  
  2.   
  3.     protected final Logger logger = LoggerFactory.getLogger(getClass());  
  4.   
  5.     public static final String POSTFIX_SELECTBYID = ".selectById";  
  6.     public static final String POSTFIX_SELECTBYIDS = ".selectByIds";  
  7.     public static final String POSTFIX_SELECTBYMAP = ".selectByMap";  
  8.     public static final String POSTFIX_SELECTIDSLIKEBYMAP = ".selectIdsLikeByMap";  
  9.     public static final String POSTFIX_PKSELECTMAP = ".pkSelectByMap";  
  10.     public static final String POSTFIX_COUNT = ".count";  
  11.     public static final String POSTFIX_COUNTLIKEBYMAP = ".countLikeByMap";  
  12.     public static final String POSTFIX_INSERT = ".insert";  
  13.     public static final String POSTFIX_DELETEBYID = ".deleteById";  
  14.     public static final String POSTFIX_DELETEBYIDS = ".deleteByIds";  
  15.     public static final String POSTFIX_DELETEBYIDSMAP = ".deleteByIdsMap";  
  16.     public static final String POSTFIX_DELETEBYMAP = ".deleteByMap";  
  17.     public static final String POSTFIX_UPDATE = ".update";  
  18.     public static final String POSTFIX_UPDATEBYMAP = ".updateByMap";  
  19.     public static final String POSTFIX_UPDATEBYIDSMAP = ".updateByIdsMap";  
  20.   
  21.     protected Class<T> clazz;  
  22.   
  23.     protected String clazzName;  
  24.   
  25.     protected T t;  
  26.   
  27.     // 定义主从数据库服务器,主数据库负责(Write op),从数据库负责(read op)  
  28.     @Autowired  
  29.     protected SqlMapClientTemplate masterSqlMapClientTemplate;  
  30.   
  31.     @Autowired  
  32.     protected SqlMapClientTemplate slaveSqlMapClientTemplate;  
  33.   
  34.     public GenericDao() {  
  35.         // 通过范型反射,取得在子类中定义的class.  
  36.         clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];  
  37.         clazzName = clazz.getSimpleName();  
  38.     }  
  39.   
  40.     public Integer count(String propertyName, Object propertyValue) {  
  41.         return count(new String[]{propertyName},new Object[]{propertyValue});  
  42.     }  
  43.   
  44.     public Integer count(String[] propertyNames, Object[] propertyValues) {  
  45.   
  46.             Map<String, Object> map = new HashMap<String, Object>();  
  47.             for (int i = 0; i < propertyNames.length; i++) {  
  48.                 map.put(propertyNames[i], propertyValues[i]);  
  49.             }  
  50.             return (Integer) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + POSTFIX_COUNT, map);  
  51.     }  
  52.   
  53.     @Override  
  54.     public Integer countLikeByMap(String[] propertyNames, Object[] propertyValues) {  
  55.         Map<String, Object> map = new HashMap<String, Object>();  
  56.         for (int i = 0; i < propertyNames.length; i++) {  
  57.             map.put(propertyNames[i], propertyValues[i]);  
  58.         }  
  59.         return (Integer) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + POSTFIX_COUNTLIKEBYMAP, map);  
  60.     }  
  61.   
  62.     /** 根据自定义SqlMap中的条件语句查询出记录数量 */  
  63.     public Integer countByStatementPostfix(String statementPostfix,String[] properties, Object[] propertyValues){  
  64.         Map<String, Object> map = new HashMap<String, Object>();  
  65.         for (int i = 0; i < properties.length; i++) {  
  66.             map.put(properties[i], propertyValues[i]);  
  67.         }  
  68.         return (Integer) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + statementPostfix, map);  
  69.     }  
  70.     /** 
  71.      * 通过id得到实体对象 
  72.      */  
  73.     public T findById(PK id) {  
  74.             return (T) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + POSTFIX_SELECTBYID, id);  
  75.   
  76.     }  
  77.   
  78.     /** 
  79.      * 根据ids获取实体列表 
  80.      *  
  81.      * @param ids 
  82.      * @return 
  83.      */  
  84.     public List<T> findByIds(List<PK> ids) {  
  85.             return (List<T>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTBYIDS, ids);  
  86.     }  
  87.   
  88.     /** 
  89.      * 直接从数据库查询出ids列表(包括符合条件的所有id) 
  90.      *  
  91.      * @param properties 
  92.      *            查询条件字段名 
  93.      * @param propertyValues 
  94.      *            字段取值 
  95.      * @return 
  96.      */  
  97.     public List<PK> findIdsBy(String[] properties, Object[] propertyValues, String orderBy, String order) {  
  98.         Map<String, Object> map = new HashMap<String, Object>();  
  99.         for (int i = 0; i < properties.length; i++) {  
  100.             map.put(properties[i], propertyValues[i]);  
  101.         }  
  102.         if (orderBy != null) {  
  103.             map.put("orderBy", orderBy);  
  104.             map.put("order", order);  
  105.         }  
  106.         return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);  
  107.     }  
  108.   
  109.     /** 
  110.      * 根据条件查询结果 
  111.      *  
  112.      * @param properties 
  113.      *            查询条件名 
  114.      * @param propertyValues 
  115.      *            查询条件值 
  116.      * @return 
  117.      */  
  118.     public List<T> findByMap(String[] properties, Object[] propertyValues, String orderBy, String order) {  
  119.             return (List<T>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTBYMAP, map);  
  120.     }  
  121.   
  122.     /** 
  123.      * 根据条件查询结果的id列表 
  124.      *  
  125.      * @param properties 
  126.      *            查询条件名 
  127.      * @param propertyValues 
  128.      *            查询条件值 
  129.      * @return 
  130.      */  
  131.     public List<PK> findIdsByMap(String[] properties, Object[] propertyValues, String orderBy, String order) {  
  132.             Map<String, Object> map = new HashMap<String, Object>();  
  133.             for (int i = 0; i < properties.length; i++) {  
  134.                 map.put(properties[i], propertyValues[i]);  
  135.             }  
  136.             if (orderBy != null) {  
  137.                 map.put("orderBy", orderBy);  
  138.                 map.put("order", order);  
  139.             }  
  140.             return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);  
  141.     }  
  142.   
  143.     /** 
  144.      * 分页查询(未处理缓存) 
  145.      *  
  146.      * @param properties 
  147.      *            查询条件字段名 
  148.      * @param propertyValues 
  149.      *            字段取值 
  150.      * @return 
  151.      */  
  152.     public List<T> pageQueryBy(String[] properties, Object[] propertyValues, String orderBy, String order, int pageSize, int pageNo) {  
  153.             Map<String, Object> map = new HashMap<String, Object>();  
  154.             for (int i = 0; i < properties.length; i++) {  
  155.                 map.put(properties[i], propertyValues[i]);  
  156.             }  
  157.             if (orderBy != null) {  
  158.                 map.put("orderBy", orderBy);  
  159.                 map.put("order", order);  
  160.             }  
  161.             map.put("limit"true);  
  162.             map.put("start", (pageNo - 1) * pageSize );// limit 操作  
  163.             map.put("end", pageSize);  
  164.             return (List<T>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTBYMAP, map);  
  165.     }  
  166.   
  167.     /** 
  168.      * 从数据库分页查询出ids列表的前200个id 
  169.      *  
  170.      * @param properties 
  171.      *            查询条件字段名 
  172.      * @param propertyValues 
  173.      *            字段取值 
  174.      * @return 
  175.      */  
  176.     protected List<PK> pageQueryIdsBy(String[] properties, Object[] propertyValues, String orderBy, String order) {  
  177.         Map<String, Object> map = new HashMap<String, Object>();  
  178.         for (int i = 0; i < properties.length; i++) {  
  179.             map.put(properties[i], propertyValues[i]);  
  180.         }  
  181.         if (orderBy != null) {  
  182.             map.put("orderBy", orderBy);  
  183.             map.put("order", order);  
  184.         }  
  185.         map.put("limit"true);  
  186.         map.put("start"0);//  操作  
  187.         map.put("end", Config.getInstance().getPageCacheSize());  
  188.         return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);  
  189.   
  190.     }  
  191.   
  192.     /** 
  193.      * 分页查询出id列表(处理缓存) 
  194.      *  
  195.      * @param properties 
  196.      *            查询条件字段名 
  197.      * @param propertyValues 
  198.      *            字段取值 
  199.      * @return 
  200.      */  
  201.     public List<PK> pageQueryIdsByMap(String[] properties, Object[] propertyValues, String orderBy, String order, int pageSize, int pageNo) {  
  202.             Map<String, Object> map = new HashMap<String, Object>();  
  203.             for (int i = 0; i < properties.length; i++) {  
  204.                 map.put(properties[i], propertyValues[i]);  
  205.             }  
  206.             if (orderBy != null) {  
  207.                 map.put("orderBy", orderBy);  
  208.                 map.put("order", order);  
  209.             }  
  210.             map.put("limit"true);  
  211.             map.put("start", (pageNo - 1) * pageSize );// limit 操作  
  212.             map.put("end", pageSize);  
  213.             return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);  
  214.     }  
  215.   
  216.     /** like分页查询(不走列表缓存) */  
  217.     public List<T> pageLikeBy(String[] properties, Object[] propertyValues, String orderBy, String order, int pageSize, int pageNo){  
  218.         Map<String, Object> map = new HashMap<String, Object>();  
  219.         for (int i = 0; i < properties.length; i++) {  
  220.             map.put(properties[i], propertyValues[i]);  
  221.         }  
  222.         if (orderBy != null) {  
  223.             map.put("orderBy", orderBy);  
  224.             map.put("order", order);  
  225.         }  
  226.         map.put("limit"true);  
  227.         map.put("start", (pageNo - 1) * pageSize );// limit 操作  
  228.         map.put("end", pageSize);  
  229.         List<PK> ids = (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTIDSLIKEBYMAP, map);  
  230.         return findByIds(ids);  
  231.     }  
  232.   
  233.     /** 
  234.      * 新增对象 
  235.      */  
  236.     public Serializable insert(T o) throws Exception {  
  237.         if (t instanceof QueryCachable) {  
  238.             // 清除受影响的缓存  
  239.             clearListCache(o);  
  240.         }  
  241.         return (Serializable) masterSqlMapClientTemplate.insert(clazz.getName() + POSTFIX_INSERT, o);  
  242.     }  
  243.   
  244.   
  245.     /** 
  246.      * 更新对象 
  247.      */  
  248.     public T update(T o) throws Exception {  
  249.         masterSqlMapClientTemplate.update(clazz.getName() + POSTFIX_UPDATE, o);  
  250.         return o;  
  251.     }  
  252.   
  253.     /** 
  254.      * 更新对象的部分属性 
  255.      */  
  256.     public int update(PK id, String[] properties, Object[] propertyValues) throws Exception {  
  257.         // 更新数据库  
  258.         Map<String, Object> map = new HashMap<String, Object>();  
  259.         for (int i = 0; i < properties.length; i++) {  
  260.             map.put(properties[i], propertyValues[i]);  
  261.         }  
  262.         map.put("id", id);  
  263.         return masterSqlMapClientTemplate.update(clazz.getName() + POSTFIX_UPDATEBYMAP, map);  
  264.     }  
  265.   
  266.     /** 
  267.      * 根据ID列表更新对象的部分属性 
  268.      */  
  269.     public int updateByIdsMap(List<PK> ids,String[] properties, Object[] propertyValues) throws Exception{  
  270.         // 更新数据库  
  271.         Map<String, Object> map = new HashMap<String, Object>();  
  272.         for (int i = 0; i < properties.length; i++) {  
  273.             map.put(properties[i], propertyValues[i]);  
  274.         }  
  275.         map.put("ids", ids);  
  276.         return masterSqlMapClientTemplate.update(clazz.getName() + POSTFIX_UPDATEBYIDSMAP, map);  
  277.     }  
  278.   
  279.     /** 
  280.      * 根据ID删除对象 
  281.      */  
  282.     public void deleteById(PK id) throws Exception {  
  283.         masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYID, id);  
  284.     }  
  285.   
  286.     /** 
  287.      * 根据ID删除对象 
  288.      */  
  289.     public void deleteByIds(List<PK> ids) throws Exception {  
  290.         masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYIDS, ids);  
  291.     }  
  292.   
  293.     /** 根据ID及条件删除对象 */  
  294.     public void deleteByIdsMap(List<PK> ids, String[] properties, Object[] propertyValues) throws Exception {  
  295.         Map<String, Object> map = new HashMap<String, Object>();  
  296.         for (int i = 0; i < properties.length; i++) {  
  297.             map.put(properties[i], propertyValues[i]);  
  298.         }  
  299.         map.put("ids", ids);  
  300.         masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYIDSMAP, map);  
  301.     }  
  302.   
  303.     /** 
  304.      * 根据条件删除对象 
  305.      */  
  306.     public int deleteByMap(String[] properties, Object[] propertyValues) throws Exception {  
  307.   
  308.         Map<String, Object> map = new HashMap<String, Object>();  
  309.         for (int i = 0; i < properties.length; i++) {  
  310.             map.put(properties[i], propertyValues[i]);  
  311.         }  
  312.         return masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYMAP, map);  
  313.     }  
  314.   
  315.     /** 
  316.      * 根据自定义SqlMap中的条件语句查询出列表(注意:不处理缓存) 
  317.      */  
  318.     public List<T> findByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues, String orderBy, String order) {  
  319.         Map<String, Object> map = new HashMap<String, Object>();  
  320.         for (int i = 0; i < properties.length; i++) {  
  321.             map.put(properties[i], propertyValues[i]);  
  322.         }  
  323.         if (orderBy != null) {  
  324.             map.put("orderBy", orderBy);  
  325.             map.put("order", order);  
  326.         }  
  327.         return slaveSqlMapClientTemplate.queryForList(clazz.getName() + statementPostfix, map);  
  328.     }  
  329.       
  330.     /** 
  331.      * 根据自定义SqlMap中的条件语句查询出列表(注意:不处理缓存) 
  332.      */  
  333.     public List<T> pageQueryByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues, String orderBy, String order,int pageSize,int pageNo) {  
  334.         Map<String, Object> map = new HashMap<String, Object>();  
  335.         for (int i = 0; i < properties.length; i++) {  
  336.             map.put(properties[i], propertyValues[i]);  
  337.         }  
  338.         if (orderBy != null) {  
  339.             map.put("orderBy", orderBy);  
  340.             map.put("order", order);  
  341.         }  
  342.         map.put("limit"true);  
  343.         map.put("start", (pageNo - 1) * pageSize );// limit 操作  
  344.         map.put("end", pageSize);  
  345.         List<PK> ids = (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + statementPostfix, map);  
  346.         return findByIds(ids);  
  347.     }  
  348.   
  349.     /** 
  350.      * 根据自定义SqlMap中的条件语句更新数据(注意:不处理缓存) 
  351.      */  
  352.     public void updateByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues) {  
  353.         Map<String, Object> map = new HashMap<String, Object>();  
  354.         for (int i = 0; i < properties.length; i++) {  
  355.             map.put(properties[i], propertyValues[i]);  
  356.         }  
  357.         masterSqlMapClientTemplate.update(clazz.getName() + statementPostfix, map);  
  358.     }  
  359.   
  360.     /** 
  361.      * 根据自定义SqlMap中的条件语句删除数据(注意:不处理缓存) 
  362.      */  
  363.     public void deleteByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues) {  
  364.         Map<String, Object> map = new HashMap<String, Object>();  
  365.         for (int i = 0; i < properties.length; i++) {  
  366.             map.put(properties[i], propertyValues[i]);  
  367.         }  
  368.         masterSqlMapClientTemplate.delete(clazz.getName() + statementPostfix, map);  
  369.     }  
  370.   
  371.     /** 
  372.      * 根据自定义SqlMap中的条件语句插入数据(注意:不处理缓存) 
  373.      */  
  374.     public void insertByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues) {  
  375.         Map<String, Object> map = new HashMap<String, Object>();  
  376.         for (int i = 0; i < properties.length; i++) {  
  377.             map.put(properties[i], propertyValues[i]);  
  378.         }  
  379.         masterSqlMapClientTemplate.insert(clazz.getName() + statementPostfix, map);  
  380.     }  
  381.   
  382. }  

 

 

3、所有其他Dao只需继承GenericDao,不需再写增删改查的方法

 

[java] view plaincopy
 
  1. package com.teacherclub.business.album.dao;  
  2.   
  3. import org.springframework.stereotype.Repository;  
  4.   
  5. import com.teacherclub.business.album.entity.Album;  
  6. import com.teacherclub.core.mem.MemcacheUtil;  
  7. import com.teacherclub.core.orm.EntityCachable;  
  8. import com.teacherclub.core.orm.dao.impl.GenericDao;  
  9.   
  10. @Repository  
  11. public class AlbumDao extends GenericDao<Album,Integer> {  
  12.   
  13. }  

 

 

此泛型的实现,关键在于:clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics