All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yqjr.framework.base.BaseService Maven / Gradle / Ivy

package com.yqjr.framework.base;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.yqjr.framework.annotation.FrameworkService;
import com.yqjr.framework.component.config.Configuration;
import com.yqjr.framework.component.context.SpringContext;
import com.yqjr.framework.component.context.ThreadContext;
import com.yqjr.framework.component.mapper.BeanMapper;
import com.yqjr.framework.datatype.BizzException;
import com.yqjr.framework.datatype.Page;
import com.yqjr.framework.utils.Constants;
import com.yqjr.framework.utils.Reflections;

/**
 * ClassName: BaseService 
* Description: 业务服务基类
* Create By: admin
* Create Date: 2017年4月19日 下午7:00:04
* Modified By:
* Modified Date:
* Modified Content:
* Version: 1.0
*/ @SuppressWarnings("rawtypes") @FrameworkService public abstract class BaseService, T extends BaseEntity, C extends BaseCondition, BM extends BaseModel> { @Autowired protected D dao; @Autowired private SqlSessionFactoryBean sqlSessionFactory; private static final int BATCH_SIZE = Configuration.getConfig().getIntValue("framework.db.batchsize"); /** * 保存数据 * * @param model Model类 * @return ID */ @Transactional public ID save(BM model) { T entity = toEntity(model, getEntityClass()); entity.preInsert(); dao.insert(entity); return entity.getId(); } /** * Description: 批量保存
* Create By: admin
* Create Date: 2017年5月2日 上午11:30:54 * * @param entities * 待保存数据 */ @Transactional public void batchSave(List entities) { Assert.notEmpty(entities); String mapperName = getDaoClass().getName() + ".insert"; SqlSession sqlSession = null; try { sqlSession = sqlSessionFactory.getObject().openSession(ExecutorType.BATCH); for (int i = 1; i <= entities.size(); i++) { T entity = entities.get(i - 1); entity.preInsert(); sqlSession.insert(mapperName, entity); if (i % BATCH_SIZE == 0 || i == entities.size()) { sqlSession.commit(); sqlSession.clearCache(); } } } catch (Exception e) { throw new BizzException("批量保存数据异常", e); } finally { if (sqlSession != null) { sqlSession.close(); } } } /** * Description: 更新数据
* Create By: admin
* Create Date: 2017年4月27日 上午8:32:07 * * @param model Model类 */ @Transactional public void update(BM model) { T entity = toEntity(model, getEntityClass()); entity.preUpdate(); int res = dao.update(entity); if (res == 0) throw new BizzException("影响记录数为0,更新失败"); } /** * Description: 批量更新
* Create By: admin
* Create Date: 2017年5月2日 上午11:30:54 * * @param entities * 待更新数据 */ @Transactional public void batchUpdate(List entities) { Assert.notEmpty(entities); String mapperName = getDaoClass().getName() + ".update"; SqlSession sqlSession = null; try { sqlSession = sqlSessionFactory.getObject().openSession(ExecutorType.BATCH); for (int i = 1; i <= entities.size(); i++) { T entity = entities.get(i - 1); entity.preUpdate(); sqlSession.update(mapperName, entity); // 达到批量大小或者循环结束时提交数据 if (i % BATCH_SIZE == 0 || i == entities.size()) { sqlSession.commit(); sqlSession.clearCache(); } } } catch (Exception e) { throw new BizzException("批量更新数据异常", e); } finally { if (sqlSession != null) { sqlSession.close(); } } } /** * 删除数据 * * @param model Model类 */ @Transactional public void delete(BM model) { T entity = toEntity(model, getEntityClass()); entity.setDeleteStatus(T.DELETE); entity.preUpdate(); int res = dao.delete(entity); if (res == 0) throw new BizzException("影响记录数为0,删除失败"); } /** * Description: 批量删除
* Create By: admin
* Create Date: 2017年5月2日 上午11:30:54 * * @param entities * 待删除数据 */ @Transactional public void batchDelete(List entities) { Assert.notEmpty(entities); String mapperName = getDaoClass().getName() + ".delete"; SqlSession sqlSession = null; try { sqlSession = sqlSessionFactory.getObject().openSession(ExecutorType.BATCH); for (int i = 1; i <= entities.size(); i++) { T entity = entities.get(i - 1); entity.setDeleteStatus(T.DELETE); entity.preUpdate(); sqlSession.update(mapperName, entity); // 达到批量大小或者循环结束时提交数据 if (i % BATCH_SIZE == 0 || i == entities.size()) { sqlSession.commit(); sqlSession.clearCache(); } } } catch (Exception e) { throw new BizzException("批量删除数据异常", e); } finally { if (sqlSession != null) { sqlSession.close(); } } } /** * Description: 批量删除数据
* Create By: admin
* Create Date: 2017年6月28日 上午10:06:21 * * @param ids * 数据ID,使用,分割 */ @SuppressWarnings("unchecked") public void batchDelete(String ids) { Assert.hasText(ids); List deleteIds = new ArrayList(); for (String id : ids.split(",")) { T t = null; try { t = getEntityClass().newInstance(); } catch (Exception e) { throw new BizzException("批量删除数据异常", e); } Class idClass = getIDClass(); Object tmp = null; if (idClass == Long.class) { tmp = Long.valueOf(id); } else if (idClass == Integer.class) { tmp = Integer.valueOf(id); } else { tmp = id; } t.setId((ID) tmp); deleteIds.add(t); } batchDelete(deleteIds); } /** * 获取单条数据 * * @param id 主键ID * @return BM */ public BM id(ID id) { T entity = dao.id(id); return toModel(entity, getModelClass(), false); } /** * 获取单条数据 * * @param model Model类 * @return BM */ public BM get(BM model) { T entity = toEntity(model, getEntityClass()); entity = dao.get(entity); return toModel(entity, getModelClass(), false); } /** * 查询列表数据 * @param model Model类 * @param Model类 * @return 模型对象List */ @SuppressWarnings("unchecked") public List findList(M model) { T entity = toEntity(model, getEntityClass()); List list = dao.findList(entity); return toModels(list, model.getClazz(), false); } /** * Description: 单表分页查询
* Create By: admin
* Create Date: 2017年6月21日 下午1:02:01 * * @param page 分页对象 * @param condition 条件对象 * @return 分页查询结果 */ @SuppressWarnings("unchecked") public Page findPage(Page page, C condition) { Assert.notNull(page); ThreadContext.getInstance().put(Constants.PAGE, page); List result = dao.findByCondition(condition); List models = new ArrayList(); for (Object object : result) { // 如果查询结果为model则直接存入结果集 if (object instanceof BaseModel) { models.add((BM) object); } // 如果为entity则进行属性复制 else if (object instanceof BaseEntity) { models.add(toModel((T) object, page.getClazz(), true)); } } page.setList(models); return page; } /** * 查询分页数据 * * @param page 分页对象 * @param methodName 方法名称 * @param parameter 参数 * @param Model对象 * @return 查询结果Model对象 */ @SuppressWarnings("unchecked") public Page findPage(Page page, String methodName, Object... parameter) { Assert.notNull(page); Assert.hasText(methodName); ThreadContext.getInstance().put(Constants.PAGE, page); Method method = SpringContext.getInstance().getMethodWithName(dao.getClass(), methodName); List result = null; try { result = (List) method.invoke(dao, parameter); } catch (Exception e) { throw new BizzException(e); } List models = new ArrayList(); for (Object object : result) { // 如果查询结果为model则直接存入结果集 if (object instanceof BaseModel) { models.add((M) object); } // 如果为entity则进行属性复制 else if (object instanceof BaseEntity) { models.add(toModel((T) object, page.getClazz(), true)); } } page.setList(models); return page; } /** * Description: 实体对象转模型
* Create By: admin
* Create Date: 2017年4月24日 下午6:56:40 * * @param entity 实体对象 * @param clazz 模型对象class * @param transferDict 是否需要转换数据字典 * @param 模型对象 * @param 实体对象 * @return 模型对象 */ public M toModel(E entity, Class clazz, boolean transferDict) { if (entity == null) return null; Assert.notNull(clazz); try { M modelInstance = clazz.newInstance(); BeanMapper.beanToBean(entity, modelInstance, false, transferDict); return modelInstance; } catch (Exception e) { throw new BizzException(e); } } /** * 实体对象转模型 * @param entity 实体对象 * @param clazz 模型对象class * @param 模型对象 * @param 实体对象 * @return: 模型对象 */ public M toModel(E entity, Class clazz) { return toModel(entity, clazz); } /** * Description: 实体对象转模型
* Create By: admin
* Create Date: 2017年5月3日 上午9:42:27 * * @param entities 实体对象List * @param clazz 模型对象class * @param transferDict 是否需要转换数据字典 * @param 模型对象 * @param 实体对象 * @return 模型对象List */ public List toModels(List entities, Class clazz, boolean transferDict) { if (entities == null) return null; Assert.notNull(clazz); try { List list = new ArrayList(); for (E entity : entities) { list.add(toModel(entity, clazz, transferDict)); } return list; } catch (Exception e) { throw new BizzException(e); } } public List toModels(List entities, Class clazz) { return toModels(entities, clazz, false); } /** * Description: 模型转实体对象
* Create By: admin
* Create Date: 2017年4月24日 下午6:56:40 * * @param model 模型对象 * @param clazz 实体对象class * @param 模型对象 * @param 实体对象 * @return 实体对象 */ public E toEntity(M model, Class clazz) { Assert.notNull(model); Assert.notNull(clazz); try { E entityInstance = clazz.newInstance(); BeanMapper.beanToBean(model, entityInstance, false, false); return entityInstance; } catch (Exception e) { throw new BizzException(e); } } /** * @return 实体对象Class */ @SuppressWarnings("unchecked") private Class getEntityClass() { return Reflections.getClassGenricType(this.getClass(), 2); } /** * @return 模型对象Class */ @SuppressWarnings("unchecked") private Class getModelClass() { return Reflections.getClassGenricType(this.getClass(), 4); } /** * @return DAO对象Class */ @SuppressWarnings("unchecked") private Class getDaoClass() { return Reflections.getClassGenricType(this.getClass(), 1); } /** * @return ID对象Class */ @SuppressWarnings("unchecked") private Class getIDClass() { return Reflections.getClassGenricType(this.getClass(), 0); } /** * Description: 校验属性是否重复
* Create By: admin
* Create Date: 2017年6月28日 下午4:46:36 * * @param old 老属性 * @param cur 新属性 * @param model 模型 * @return 是否重复 */ public String checkUnique(String old, String cur, BM model) { if (cur != null && cur.equals(old)) { return "true"; } else if (cur != null && get(model) == null) { return "true"; } return "false"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy