See us a big guy custom Mybatis implementation, simplified Dao interface, call novel, after permission to share out.

This is how we operate the database in our business code

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

QueryDao is used to query the class interface, and updateDao is used to add, modify, and delete classes.

There are the following interfaces:

SqlSessionDaoSupport = SqlSessionDaoSupport = SqlSessionDaoSupport = SqlSessionDaoSupport = SqlSessionDaoSupport = SqlSessionDaoSupport = SqlSessionDaoSupport = SqlSessionDaoSupport = SqlSessionDaoSupport = SqlSessionDaoSupport

    @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}], input parameter: [{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 + "Execution completed, time" + stopWatch.getTotalTimeMillis() + "Ms -- -- -- -- -- -- -- -- -- --");
        log.debug(MessageFormat.format(SqlId [{0}], return value: [{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}], input parameter: [{1}]", sqlId, params));
        if (ObjectUtils.isEmpty(pageInfo)) {
            throw new BLogicException("sqlId:" + sqlId + "Parameter pageInfo object cannot be empty!");
        }

        Integer pageNum = pageInfo.getPageNum();
        if (ObjectUtils.isEmpty(pageNum) || pageNum <= 0) {
            throw new BLogicException("sqlId:" + sqlId + "The pageNum property of the parameter cannot be empty and must be greater than 0!");
        }
        Integer pageSize = pageInfo.getPageSize();
        if (ObjectUtils.isEmpty(pageSize) || pageSize <= 0) {
            throw new BLogicException("sqlId:" + sqlId + "The pageSize property of the parameter cannot be empty and must be greater than 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 + "Execution completed, time" + stopWatch.getTotalTimeMillis() + "Ms -- -- -- -- -- -- -- -- -- --");
        log.info(MessageFormat.format(SqlId [{0}], return value: [{1}]", sqlId, list));
        return list;
    }
Copy the code
    @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}], input parameter: [{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 + "Execution completed, time" + stopWatch.getTotalTimeMillis() + "Ms -- -- -- -- -- -- -- -- -- --");
        return row;
    }
Copy the code

Emmm, this article has no technical content, it provides a new idea, there are friends like welcome to learn, I think it is very convenient to use without the PACKAGING of Dao layer, sqlId is the only good. In that way, it would not be repeated.

Insert (Ins), Update (Upd), Delete (Del), Select (Sel), List query (start List), try not to make an sqlId too long, but you can’t understand it. Add SQL at the end to indicate sqlId.

Friends who have different views on this way are welcome to comment and exchange ~ ~ ~ look forward to your reply.


Every time I grow up, I want to share with you. (Whisper BB, there is a lucky draw to send books in the official account.)