Mybatis 的一種奇特封裝

看咱們一個大佬自定義的 Mybatis 實現,簡化了 Dao 接口,直呼新奇,通過容許後發出來分享下。sql

咱們在業務代碼中這樣操做數據庫的數據庫

queryDao.executeForObject("selItemStaByIdSQL", itemId, Integer.class); updateDao.execute("insBasItemSQL", basicItemDO);ide

查詢類的接口用 queryDao,新增修改刪除用 updateDaothis

具體有如下幾個接口: spa

具體實現很簡單,繼承 SqlSessionDaoSupport 獲取 SqlSession,把 SqlSession 的方法重寫下就行了。debug

@Override
    public <E> E executeForObject(String sqlId, Object params, Class<E> clazz) {
        if (log.isDebugEnabled()) {
            log.debug("executeForObject Start.");
        }
		if (ObjectUtils.isEmpty(sqlId)) {
			throw new IllegalClassTypeException("sqlId can not be empty.");
		}
        log.info(MessageFormat.format("sqlId[{0}],輸入參數:[{1}]", sqlId, params));
        StopWatch stopWatch = new StopWatch(sqlId);
        stopWatch.start();

        SqlSession sqlSession = this.getSqlSession();
        Object obj = sqlSession.selectOne(sqlId, params);

        if (log.isDebugEnabled() && ObjectUtils.isNotEmpty(obj)) {
            log.debug("Return type:" + obj.getClass().getName());
        }

        E entity = null;
        try {
            if (ObjectUtils.isNotEmpty(clazz) && ObjectUtils.isNotEmpty(obj)) {
                entity = clazz.cast(obj);
            }
        } catch (ClassCastException e) {
            log.error("The illegal Class Type of the argument.");
            throw new IllegalClassTypeException(e);
        }

        if (log.isDebugEnabled()) {
            log.debug("executeForObject End.");
        }
        stopWatch.stop();
        log.info("----------sqlId:" + sqlId + "執行完畢,耗時" + stopWatch.getTotalTimeMillis() + "毫秒----------");
        log.debug(MessageFormat.format("sqlId[{0}],返回值:[{1}]", sqlId, entity));
        return entity;
    }
    
        @Override
    public <E> List<E> executeForObjectListByPage(String sqlId, Object params, PageInfo pageInfo) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("executeForObjectListByPage Start.");
        }
		if (ObjectUtils.isEmpty(sqlId)) {
			throw new IllegalClassTypeException("sqlId can not be empty.");
		}
        log.info(MessageFormat.format("sqlId[{0}],輸入參數:[{1}]", sqlId, params));
        if (ObjectUtils.isEmpty(pageInfo)) {
            throw new BLogicException("sqlId:" + sqlId + "的參數pageInfo對象不能爲空!");
        }

        Integer pageNum = pageInfo.getPageNum();
        if (ObjectUtils.isEmpty(pageNum) || pageNum <= 0) {
            throw new BLogicException("sqlId:" + sqlId + "的參數的pageNum屬性不能爲空,而且必須大於0!");
        }
        Integer pageSize = pageInfo.getPageSize();
        if (ObjectUtils.isEmpty(pageSize) || pageSize <= 0) {
            throw new BLogicException("sqlId:" + sqlId + "的參數的pageSize屬性不能爲空,而且必須大於0!");
        }
        StopWatch stopWatch = new StopWatch(sqlId);
        stopWatch.start();

        SqlSession sqlSession = this.getSqlSession();
        PageHelper.startPage(pageNum, pageSize);
        String orderBy = pageInfo.getOrderBy();
        if (ObjectUtils.isNotEmpty(orderBy)) {
            PageHelper.orderBy(orderBy);
        }
        List<E> list = sqlSession.selectList(sqlId, params);
        if (log.isDebugEnabled()) {
            log.debug("executeForObjectListByOrderPage End.");
        }
        stopWatch.stop();
        log.info("----------sqlId:" + sqlId + "執行完畢,耗時" + stopWatch.getTotalTimeMillis() + "毫秒----------");
        log.info(MessageFormat.format("sqlId[{0}],返回值:[{1}]", sqlId, list));
        return list;
    }
複製代碼
@Override
    public int execute(String sqlId, Object params) {
        if (log.isDebugEnabled()) {
            log.debug("execute Start.");
        }
		if (ObjectUtils.isEmpty(sqlId)) {
			throw new IllegalClassTypeException("sqlId can not be empty.");
		}
        log.info(MessageFormat.format("sqlId[{0}],輸入參數:[{1}]", sqlId, params));
        StopWatch stopWatch = new StopWatch(sqlId);
        stopWatch.start();
        SqlSession sqlSession = this.getSqlSession();
        int row = sqlSession.update(sqlId, params);
        if (log.isDebugEnabled()) {
            log.debug("execute End. success count:" + row);
        }
        stopWatch.stop();
        log.info("----------sqlId:" + sqlId + "執行完畢,耗時" + stopWatch.getTotalTimeMillis() + "毫秒----------");
        return row;
    }
複製代碼

emmm,這篇文章沒啥技術含量,就提供一種新的思路,有喜歡的朋友歡迎借鑑,我以爲少了 Dao 層的封裝用起來就挺方便的,sqlId 惟一就行了。若是用那種見名知意的方式,也不會重複。code

通常 sqlId 的命名我習慣簡寫 Insert 爲 Ins,Update 爲 Upd,Delete 爲 Del,Select 爲 Sel,列表查詢是 List 開頭,儘可能不要讓一個 sqlId 過長反而看不懂,結尾加上 SQL 代表是 sqlId。orm

對這種方式有不一樣看法的朋友歡迎評論交流~~~期待你的回覆。cdn


每一次成長,都想與你分享。(小聲BB,公衆號裏有抽獎送書活動。)對象

相關文章
相關標籤/搜索