/** * Copyright © 2012-2014 JeeSite All rights reserved. */ package com.nis.web.service; import java.util.ArrayList; import java.util.List; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.beans.factory.annotation.Autowired; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.nis.domain.BaseEntity; import com.nis.domain.Page; import com.nis.domain.configuration.AreaIpCfg; import com.nis.domain.configuration.BaseCfg; import com.nis.domain.configuration.BaseIpCfg; import com.nis.domain.configuration.BaseStringCfg; import com.nis.domain.configuration.ComplexkeywordCfg; import com.nis.domain.maat.MaatCfg; import com.nis.domain.maat.ToMaatBean; import com.nis.web.dao.CrudDao; /** * Service基类 * @author ThinkGem * @version 2014-05-16 */ public abstract class CrudService, T extends BaseEntity> extends BaseService { /** * 持久层对象 */ @Autowired protected D dao; /** * 获取单条数据 * @param id * @return */ public T get(Long id) { return dao.get(id); } /** * 获取单条数据 * @param entity * @return */ public T get(T entity) { return dao.get(entity); } /** * 查询列表数据 * @param entity * @return */ public List findList(T entity) { return dao.findList(entity); } /** * 查询分页数据 * @param page 分页对象 * @param entity * @return */ public Page findPage(Page page, T entity) { entity.setPage(page); page.setList(dao.findList(entity)); return page; } /** * 保存数据(插入或更新) * @param entity */ public void save(T entity) { if (entity.getIsNewRecord()){ dao.insert(entity); }else{ dao.update(entity); } } /** * 删除数据 * @param entity */ public void delete(T entity) { dao.delete(entity); } /** * * saveBatch(批量全部保存数据) * (这里描述这个方法适用条件 – 可选) * @param data 数据集合 * @param mClass 传入的dao.xml里的mapper * @author wx * @since 1.0.0 */ @SuppressWarnings("unchecked") public void saveBatch(List data,@SuppressWarnings("rawtypes")Class mClass) { SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class); SqlSession batchSqlSession = null; try{ batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); for(int index = 0; index < data.size();index++){ T t = data.get(index); ((CrudDao) batchSqlSession.getMapper(mClass)).insert(t); } batchSqlSession.commit(); // }catch (Exception e){ // batchSqlSession.rollback(); // throw e; }finally { if(batchSqlSession != null){ batchSqlSession.close(); } } } /** * * updateBatch(批量更新全部数据) * (这里描述这个方法适用条件 – 可选) * @param data 数据集合 * @param mClass 传入的dao.xml里的mapper * @author wx * @since 1.0.0 */ @SuppressWarnings("unchecked") public void updateBatch(List data,@SuppressWarnings("rawtypes") Class mClass) { SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class); SqlSession batchSqlSession = null; try{ batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); for(int index = 0; index < data.size();index++){ T t = data.get(index); ((CrudDao) batchSqlSession.getMapper(mClass)).update(t); } batchSqlSession.commit(); // }catch (Exception e){ // batchSqlSession.rollback(); // throw e; }finally { if(batchSqlSession != null){ batchSqlSession.close(); } } } /** * * deleteBatch(批量删除全部数据) * (这里描述这个方法适用条件 – 可选) * @param data 数据集合 * @param mClass 传入的dao.xml里的mapper * @author wx * @since 1.0.0 */ @SuppressWarnings("unchecked") public void deleteBatch(List data,@SuppressWarnings("rawtypes") Class mClass) { SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class); SqlSession batchSqlSession = null; try{ batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); for(int index = 0; index < data.size();index++){ T t = data.get(index); ((CrudDao) batchSqlSession.getMapper(mClass)).delete(t); } batchSqlSession.commit(); // }catch (Exception e){ // batchSqlSession.rollback(); // throw e; }finally { if(batchSqlSession != null){ batchSqlSession.close(); } } } /** * * sendToMaatConvertor(转换配置为Maat格式) * (所有的配置需拥有一样的编译ID,action,serviceId,等属性) * @param cfg * @return *boolean * @exception * @since 1.0.0 */ public boolean sendToMaatConvertor(AreaIpCfg[] areaCfg,T ... cfg){ if(cfg==null){ throw new RuntimeException("转换出错,配置为空"); } long compileId=0l; ToMaatBean toMaatBean=new ToMaatBean(); MaatCfg maatCfg=new MaatCfg(); if(cfg[0] instanceof BaseIpCfg){ maatCfg.setIpCfg((BaseIpCfg[])cfg); compileId=((BaseIpCfg)cfg[0]).getCompileId(); }else if(cfg[0] instanceof BaseStringCfg){ maatCfg.setStrCfg((BaseStringCfg[])cfg); compileId=((BaseStringCfg)cfg[0]).getCompileId(); }else if(cfg[0] instanceof ComplexkeywordCfg){ maatCfg.setComplexStrCfg((ComplexkeywordCfg[])cfg); compileId=((ComplexkeywordCfg)cfg[0]).getCompileId(); } if(areaCfg!=null){ AreaIpCfg[] cfgArray=new AreaIpCfg[1]; } List serviceCfg=new ArrayList<>(); serviceCfg.add(maatCfg); BaseCfg baseCfg=(BaseCfg)cfg[0]; if(compileId==0l){ throw new RuntimeException("转换出错,未获取到正确的compileId"); }else{ toMaatBean.setCompileId(compileId); } if(baseCfg.getAction()==null){ throw new RuntimeException("转换出错,未获取到正确的action"); }else{ toMaatBean.setAction(baseCfg.getAction()); } if(baseCfg.getIsAreaEffective()==null){ throw new RuntimeException("转换出错,未获取到正确的isAreaEffective"); }else{ toMaatBean.setIsAreaEffective(baseCfg.getIsAreaEffective()); } if(baseCfg.getIsValid()==null){ throw new RuntimeException("转换出错,未获取到正确的isValid"); }else{ toMaatBean.setIsValid(baseCfg.getIsValid()); } if(baseCfg.getRequestId()==null){ throw new RuntimeException("转换出错,未获取到正确的requestId"); }else{ toMaatBean.setRequestId(baseCfg.getRequestId()); } if(baseCfg.getServiceId()==null){ throw new RuntimeException("转换出错,未获取到正确的serviceId"); }else{ toMaatBean.setServiceId(baseCfg.getServiceId()); } toMaatBean.setAreaEffectiveIds(baseCfg.getAreaEffectiveIds()==null?"":baseCfg.getAreaEffectiveIds()); toMaatBean.setAttribute(baseCfg.getAttribute()==null?"":baseCfg.getAttribute()); toMaatBean.setClassify(baseCfg.getClassify()==null?"":baseCfg.getClassify()); toMaatBean.setLable(baseCfg.getLable()==null?"":baseCfg.getLable()); Gson gson=new GsonBuilder().disableHtmlEscaping() .setDateFormat("yyyy-MM-dd HH:mm:ss") /*.setPrettyPrinting().serializeNulls()*/ .excludeFieldsWithoutExposeAnnotation() .create(); String json=gson.toJson(toMaatBean); //发送至maat,待完成 return false; } }