cn.chenlichao.web.ssm.service.impl.BaseServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for cn.chenlichao.web.ssm.service.impl.BaseServiceImpl.java

Source

/*
 * Copyright 2016-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.chenlichao.web.ssm.service.impl;

import cn.chenlichao.web.ssm.dao.entity.BaseEntity;
import cn.chenlichao.web.ssm.dao.mapper.BaseMapper;
import cn.chenlichao.web.ssm.exception.DAOException;
import cn.chenlichao.web.ssm.service.BaseService;
import cn.chenlichao.web.ssm.service.PageParams;
import cn.chenlichao.web.ssm.service.PageResults;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.exceptions.PersistenceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.ClassArrayEditor;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.Column;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * ?
 *
 * <br>author: ?
 * <br>date: 16/6/12 ?11:10
 * <br>version: V1.0.0
 * <br>CopyrightCopyright  2016 Chen Lichao. All rights reserved.
 */
public abstract class BaseServiceImpl<E extends BaseEntity<PK>, PK extends Serializable>
        implements BaseService<E, PK> {

    /**  */
    protected Logger LOGGER = LoggerFactory.getLogger(getClass());

    protected BaseMapper<E, PK> baseMapper;

    private Class<E> entityClass;
    private Class<PK> pkClass;

    @SuppressWarnings("unchecked")
    public BaseServiceImpl() {
        //  Class  (, ?, void)  Type
        Type genType = getClass().getGenericSuperclass();

        // ??, , ?
        while (!genType.equals(Object.class)) {
            if (genType instanceof ParameterizedType) {
                ParameterizedType superType = (ParameterizedType) genType;
                Type[] params = superType.getActualTypeArguments();
                if (params.length == 2 && params[0] instanceof Class) {
                    entityClass = (Class<E>) params[0];
                    pkClass = (Class<PK>) params[1];
                    break;
                }
            }
            if (genType instanceof Class) {
                genType = ((Class) genType).getGenericSuperclass();
            } else {
                break;
            }
        }
        LOGGER.debug("Entity Class: {}, PK class: {}", entityClass.getSimpleName(), pkClass.getSimpleName());
    }

    @Autowired
    public void setBaseMapper(BaseMapper<E, PK> baseMapper) {
        this.baseMapper = baseMapper;
    }

    @Override
    public PK save(E entity) {
        if (entity == null) {
            throw new IllegalArgumentException("?null");
        }
        int result = baseMapper.insert(entity);
        if (result != 1) {
            throw new PersistenceException("??");
        }
        LOGGER.trace("???, ID: [{}]", entity.getId());
        return entity.getId();
    }

    @Override
    public int save(Collection<E> entities) {
        if (entities == null) {
            throw new IllegalArgumentException("??null");
        }
        int successed = 0;
        for (E entity : entities) {
            try {
                save(entity);
                successed++;
            } catch (Exception e) {
                LOGGER.error("???: [{}]", entity);
            }
        }
        return successed;
    }

    @Override
    public PK saveWithNull(E entity) {
        if (entity == null) {
            throw new IllegalArgumentException("?null");
        }
        int result = baseMapper.insertWithNull(entity);
        if (result != 1) {
            throw new PersistenceException("??");
        }
        LOGGER.trace("???, ID: [{}]", entity.getId());
        return entity.getId();
    }

    @Override
    public int saveWithNull(Collection<E> entities) {
        if (entities == null) {
            throw new IllegalArgumentException("??null");
        }
        int successed = 0;
        for (E entity : entities) {
            try {
                saveWithNull(entity);
                successed++;
            } catch (Exception e) {
                LOGGER.error("???: [{}]", entity);
            }
        }
        return successed;
    }

    @Override
    public int update(E entity) {
        if (entity == null) {
            throw new IllegalArgumentException("?null");
        }
        if (entity.getId() == null) {
            throw new IllegalArgumentException("?IDnull");
        }
        return baseMapper.update(entity);
    }

    @Override
    public int updateWithNull(E entity) {
        if (entity == null) {
            throw new IllegalArgumentException("?null");
        }
        if (entity.getId() == null) {
            throw new IllegalArgumentException("?IDnull");
        }
        return baseMapper.updateWithNull(entity);
    }

    @Override
    public int updateByExample(Example example, E entity) {
        if (example == null) {
            throw new IllegalArgumentException("?null");
        }
        if (entity == null) {
            throw new IllegalArgumentException("?null");
        }
        if (example.getEntityClass() == null || !entity.getClass().equals(example.getEntityClass())) {
            throw new IllegalArgumentException("???");
        }
        return baseMapper.updateByExample(entity, example);
    }

    @Override
    public int updateByExampleWithNull(Example example, E entity) {
        if (example == null) {
            throw new IllegalArgumentException("?null");
        }
        if (entity == null) {
            throw new IllegalArgumentException("?null");
        }
        if (example.getEntityClass() == null || !entity.getClass().equals(example.getEntityClass())) {
            throw new IllegalArgumentException("???");
        }
        return baseMapper.updateByExampleWithNull(entity, example);
    }

    @Override
    public int delete(PK id) {
        if (id == null) {
            throw new IllegalArgumentException("IDnull");
        }
        return baseMapper.deleteById(id);
    }

    @Override
    public int delete(Collection<PK> ids) {
        if (ids == null) {
            throw new IllegalArgumentException("ID?null");
        }
        int successed = 0;
        for (PK id : ids) {
            try {
                delete(id);
                successed++;
            } catch (Exception e) {
                LOGGER.error("??: [{}]", id);
            }
        }
        return successed;
    }

    @Override
    public int delete(E entity) {
        if (entity == null) {
            throw new IllegalArgumentException("?null");
        }
        return baseMapper.delete(entity);
    }

    @Override
    public int deleteByExample(Example example) {
        if (example == null) {
            throw new IllegalArgumentException("?null");
        }
        if (entityClass != null && !entityClass.equals(example.getEntityClass())) {
            throw new IllegalArgumentException("????");
        }
        return baseMapper.deleteByExample(example);
    }

    @Override
    public E get(PK id) {
        if (id == null) {
            throw new IllegalArgumentException("IDnull");
        }
        return baseMapper.get(id);
    }

    @Override
    public List<E> select(E entity) {
        if (entity == null) {
            throw new IllegalArgumentException("?null");
        }
        return baseMapper.select(entity);
    }

    @Override
    public List<E> selectByExample(Example example) {
        if (example == null) {
            throw new IllegalArgumentException("?null");
        }
        if (entityClass != null && !entityClass.equals(example.getEntityClass())) {
            throw new IllegalArgumentException("????");
        }
        return baseMapper.selectByExample(example);
    }

    @Override
    public List<E> selectAll() {
        return baseMapper.selectAll();
    }

    @Override
    public int selectCount(E entity) {
        if (entity == null) {
            throw new IllegalArgumentException("?null");
        }
        return baseMapper.selectCount(entity);
    }

    @Override
    public int selectCountByExample(Example example) {
        if (example == null) {
            throw new IllegalArgumentException("?null");
        }
        if (entityClass != null && !entityClass.equals(example.getEntityClass())) {
            throw new IllegalArgumentException("????");
        }
        return baseMapper.selectCountByExample(example);
    }

    @Override
    public PageResults<E> queryPage(PageParams<E> pageParams) {
        if (pageParams == null) {
            throw new IllegalArgumentException("?null");
        }
        LOGGER.trace("...");

        // ??
        int pageNum = pageParams.getPageIndex();
        int pageSize = pageParams.getPageSize();
        if (pageSize > 100) {
            LOGGER.warn(", ?[{}] ?, ? 100 ?", pageSize);
            pageSize = 100;
        }

        // ?
        PageHelper.startPage(pageNum, pageSize);

        Example example = new Example(entityClass);
        // ??
        E params = pageParams.getParamEntity();
        if (params != null) {
            Example.Criteria criteria = example.createCriteria();
            PropertyDescriptor[] propArray = BeanUtils.getPropertyDescriptors(entityClass);
            for (PropertyDescriptor pd : propArray) {
                if (pd.getPropertyType().equals(Class.class)) {
                    continue;
                }
                try {
                    Object value = pd.getReadMethod().invoke(params);
                    if (value != null) {
                        if (pd.getPropertyType() == String.class) {
                            String strValue = (String) value;
                            if (strValue.startsWith("%") || strValue.endsWith("%")) {
                                criteria.andLike(pd.getName(), strValue);
                                continue;
                            }
                        }
                        criteria.andEqualTo(pd.getName(), value);
                    }
                } catch (IllegalAccessException | InvocationTargetException e) {
                    LOGGER.error("example: {}", e.getMessage(), e);
                }
            }
        }
        // ??
        String orderBy = pageParams.getOrderBy();
        if (StringUtils.hasText(orderBy)) {
            processOrder(example, orderBy, pageParams.isAsc());
        }
        List<E> results = baseMapper.selectByExample(example);

        // 
        if (results == null || !(results instanceof Page)) {
            return new PageResults<>(0, new ArrayList<E>(0), pageParams);
        }
        Page page = (Page) results;
        Long totalCount = page.getTotal();
        return new PageResults<>(totalCount.intValue(), Collections.unmodifiableList(results), pageParams);
    }

    @Override
    public PageResults<E> queryPageByExample(PageParams<E> pageParams, Example example) {
        if (pageParams == null) {
            throw new IllegalArgumentException("?null");
        }
        if (example == null) {
            throw new IllegalArgumentException("?null");
        }
        if (entityClass != null && !entityClass.equals(example.getEntityClass())) {
            throw new IllegalArgumentException("????");
        }

        LOGGER.trace("example?...");
        // ??
        int pageNum = pageParams.getPageIndex();
        int pageSize = pageParams.getPageSize();
        if (pageSize > 100) {
            LOGGER.warn(", ?[{}] ?, ? 100 ?", pageSize);
            pageSize = 100;
        }

        // 
        PageHelper.startPage(pageNum, pageSize);
        List<E> results = baseMapper.selectByExample(example);
        // 
        if (results == null || !(results instanceof Page)) {
            return new PageResults<>(0, new ArrayList<E>(0), pageParams);
        }
        Page page = (Page) results;
        Long totalCount = page.getTotal();
        return new PageResults<>(totalCount.intValue(), results, pageParams);
    }

    private void processOrder(Example example, String orderBy, boolean isAsc) {
        Class<?> clazz = example.getEntityClass();
        try {
            Field field = clazz.getDeclaredField(orderBy);
            if (field != null) {
                Column column = field.getAnnotation(Column.class);
                if (column != null) {
                    example.setOrderByClause(column.name() + (isAsc ? " ASC" : " DESC"));
                    return;
                }
            }
        } catch (NoSuchFieldException e) {
            // do nothing
        }
        example.setOrderByClause(orderBy + (isAsc ? " ASC" : " DESC"));
    }
}