zoukankan      html  css  js  c++  java
  • Spring-mybatis 抽取 baseDao。

    抽取BaseDao是我们开发WEB应用里常用的做法,基本上都是根据数据库框架的特性抽取出带简单增删查改分页的baseDao。

    最近在学习MyBaties,因为项目上要用到Struts2-Spring-mybatis架构。所以在抽取baseDao的时候,自己总结了mybatis的特性,对baseDao进行简单封装。

    对于baseDao,我只取出了以下几个方法

    package cn.tanjiay.mbt.base;
    
    import java.util.List;
    
    import cn.tanjiay.mbt.pojo.PageNumber;
    import cn.tanjiay.mbt.pojo.QueryObject;
    
        /**
         * 通用泛型DAO基类
         * @author g
         *
         * @param <T>
         */
    public interface BaseDao<T> {
        /**
         * 对对象进行持久化操作,如果成功则返回持久化后的ID
         * 失败则返回null
         * @param obj
         * @return
         */
        void save(T obj);
        
        /**
         * 删除指定id的持久化对象
         * @param id
         */
        void delete(T obj);
        /**
         * 修改指定的持久化对象
         * @param id
         * @param obj
         */
        void update(T obj);
        
        /**
         * 返回持久化对象
         * @param id
         * @return 找到则返回,否则返回空
         */
        T get(Integer id);
        
        /**
         * 返回所有持久化对象
         * @return
         */
        List<T> list();
        
        /**
         * 传入页码对象,进行分页查询
         * @param pn
         * @return
         */
        List<T> list(PageNumber pn);
        List<T> list(QueryObject<T> qo);
    }

    其中PageNumber是个页码对象,里面带3个属性:开始码、每页大小、页码。

    这个baseDao要怎么实现呢?

    我们知道mybatis里,操作数据库是使用SqlSession.insertdeleteselect...等方法,这些方法一般带两个参数。

    如session.insert("cn.xxx.mapper.save",user)里,第一个参数是mapper.xml里的namespace+方法名,第二个参数就是传入对象。那我们可以利用这个特性,约定好基本的增删查改方法:savedeleteupdategetlist.

    在这里我写了一个生成"路径"的方法,即生成第一个参数的方法:getMethodPath(String methodType)

       private final String path = "cn.tanjiay.mbt.mapper.";
        private String getMethodPath(String methodType){
            return path + type.getSimpleName() + "Mapper." + methodType;
        }

    这个方法很简单,如果传入“save”,当前Dao的泛型为User,即生成cn.tanjiay.mbt.mapper.UserMapper.save。

    而我所有insert方法都约定好叫save,那我的baseDaoImpl里面的方法是这样写的

        
        public void save(T obj) {
            session.insert(getMethodPath("save"), obj);
        }
    
        public void delete(T obj) {
            session.delete(getMethodPath("delete"), obj);
        }
    
        public void update(T obj) {
            session.update(getMethodPath("update"), obj);
        }

    这样就完成了baseDao的简单抽取。

    在这里我把baseDaoImpl贴上:

    package cn.tanjiay.mbt.base;
    
    import java.lang.reflect.ParameterizedType;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.annotation.Resource;
    
    import org.apache.ibatis.session.SqlSession;
    
    import cn.tanjiay.mbt.pojo.PageNumber;
    import cn.tanjiay.mbt.pojo.QueryObject;
    
    /**
     * BaseDao的MyBatis实现
     * @author g
     *
     * @param <T>
     */
    public class BaseDaoImpl<T> implements BaseDao<T> {
        
        @Resource(name="sqlSessionTemplate")
        private SqlSession session;
        private final String path = "cn.tanjiay.mbt.mapper.";
        private Class type;
        
        public BaseDaoImpl(){
            this.type = this.getDAOClass(); 
        }
        
        @SuppressWarnings("all")
        private Class getDAOClass(){
             Class clazz = (Class)((ParameterizedType) this.getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];
             return clazz;
        }
        
        private String getMethodPath(String methodType){
            return path + type.getSimpleName() + "Mapper." + methodType;
        }
        
        public void save(T obj) {
            session.insert(getMethodPath("save"), obj);
        }
    
        public void delete(T obj) {
            session.delete(getMethodPath("delete"), obj);
        }
    
        public void update(T obj) {
            session.update(getMethodPath("update"), obj);
        }
    
        public T get(Integer id) {
            return session.selectOne(getMethodPath("get"),id);
        }
    
        public List<T> list() {
            return list(new PageNumber(1,9999));
        }
    
        public List<T> list(final PageNumber pn) {
            QueryObject<T> qo = new QueryObject<T>(new     HashMap()) {
                @Override
                protected void setQuery(T obj, Map<String, Object> data) {    
                    this.setPageNoAndPageSize(pn.getPageNo(), pn.getPageSize());
                }
            };
            return session.selectList(getMethodPath("list"),qo);
        }
    
        public List<T> list(QueryObject<T> qo) {
            return session.selectList(getMethodPath("list"),qo);
        }
    
    }

    .

    
    
  • 相关阅读:
    【leetcode】1403. Minimum Subsequence in Non-Increasing Order
    【leetcode】1399. Count Largest Group
    【leetcode】1396. Design Underground System
    【leetcode】1395. Count Number of Teams
    【leetcode】1394. Find Lucky Integer in an Array
    【leetcode】1391. Check if There is a Valid Path in a Grid
    【leetcode】1390. Four Divisors
    【leetcode】1389. Create Target Array in the Given Order
    modelsim仿真基本流程
    Quartus调用MOdelsim仿真过程
  • 原文地址:https://www.cnblogs.com/cha1r/p/3516896.html
Copyright © 2011-2022 走看看