本文主要是介绍[ JAVA ] 多个泛型对象接口定义,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
泛型接口定义
有些时候我们在定义一个类去实现泛型接口时,我们并不确定这个类将要实现哪种类型的类,这时我们就不能确定接口中的泛型,那么接口中的泛型尚未确认,这时就要求这个类也必须定义泛型,而且泛型名称要一致。
代码背景
Spring MVC 框架下:
三个相似模块实现功能,通过接口定义和继承的方式动态传入相应的查询体Qo和数据体Do。
比如:
A类人员的导入、导出、查询等
B类...
C类...
...
每种类别的人由于属性不同定义了不同的数据对象和数据表格TA、TB、TC
一、接口定义
通用方法接口
package cn.showclear.www.service.base;
import cn.showclear.www.common.exception.BusinessException;
import cn.showclear.www.pojo.base.PrisonMemberQo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.MultipartFile;/*** 人员数据服务* @author YF-XIACHAOYANG* @date 2018/2/6 14:21*/
public interface DataReportService<T1,T2> {/*** 查询人员信息* @param memberQo* @return*/PageInfo<T1> query(final T2 memberQo);/*** 保存人员信息* @param memberDo*/void save(T1 memberDo) throws BusinessException;/*** 删除人员* @param id*/void delete(Integer id);/*** 更新人员* @param memberDo*/void update(T1 memberDo);/*** 加载人员信息* @param id* @return*/T1 load(Integer id);/*** 导出* @param memberQo* @return*/String export(T2 memberQo) throws BusinessException;/*** 人员导入* @param file* @return*/Integer upload(MultipartFile file);
}
业务接口定义
package cn.showclear.www.service.base;/*** 人员数据服务定义集合* 此处区分接口实例而不使用多态自动切换是因为Spring容器在使用Autowised注入时不能同名,一个sevice对应一个impl实例* @author YF-XIACHAOYANG* @date 2018/2/8 15:25*/
public interface DataReprotServices {interface DrugDataReportService<T1,T2> extends DataReportService<T1,T2>{};interface PrisonDataReportService<T1,T2> extends DataReportService<T1,T2>{};interface CommCorrDataReportService<T1,T2> extends DataReportService<T1,T2>{};
}
二、接口实现
使用泛型传入对应使用的对象,以其中一类为例:
package cn.showclear.www.service.base.impl;import cn.com.scooper.common.util.StringUtils;
import cn.showclear.www.common.constant.ExportConstants;
import cn.showclear.www.common.constant.SCConstants;
import cn.showclear.www.common.exception.BusinessException;
import cn.showclear.www.dao.base.CodeMapper;
import cn.showclear.www.dao.base.PrisonMemberMapper;
import cn.showclear.www.pojo.base.CodeDo;
import cn.showclear.www.pojo.base.ExcelHeadBean;
import cn.showclear.www.pojo.base.PrisonMemberQo;
import cn.showclear.www.pojo.base.PrisonMemberDo;
import cn.showclear.www.service.base.DataReprotServices;
import cn.showclear.www.utils.ExportExcel;
import cn.showclear.www.utils.POIUtils;
import cn.showclear.www.utils.PropertyConfig;
import cn.showclear.www.utils.TimeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;/*** 服刑人员数据服务** @author YF-XIACHAOYANG* @date 2018/2/6 14:22*/
@Service
public class PrisonDataServiceImpl implements DataReprotServices.PrisonDataReportService<PrisonMemberDo, PrisonMemberQo> {private static final Logger log = LoggerFactory.getLogger(PrisonDataServiceImpl.class);private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");@Resourceprivate PrisonMemberMapper prisonMemberMapper;@Resourceprivate CodeMapper codeMapper;@Autowiredprivate PropertyConfig propertyConfig;private static Map<String, String> codeMap = new ConcurrentHashMap<String, String>();static {initCodeMap();}/*** 初始化codeMap > 数字解析为汉字*/private static void initCodeMap() {codeMap.put("sex", "性别");codeMap.put("paperType", "证件类型");codeMap.put("degreeType", "文化程度");codeMap.put("politicalStatusOld", "政治面貌");codeMap.put("maritalStatus", "婚姻状况");codeMap.put("punishmentKind", "刑种");}/*** 查询服刑人员信息** @param memberQo* @return*/@Overridepublic PageInfo<PrisonMemberDo> query(PrisonMemberQo memberQo) {if (memberQo.getCurrentPage() == null) {memberQo.setCurrentPage(SCConstants.DEFUALT_PAGE_NUM);}if (memberQo.getPageSize() == null) {memberQo.setPageSize(SCConstants.DEFUALT_PAGE_SIZE);}PageHelper.startPage(memberQo.getCurrentPage(), memberQo.getPageSize());final List<PrisonMemberDo> prisonMemberDoList = prisonMemberMapper.query(memberQo);final PageInfo<PrisonMemberDo> pages = new PageInfo<PrisonMemberDo>(prisonMemberDoList);return pages;}/*** 保存服刑人员信息** @param memberDo*/@Overridepublic void save(PrisonMemberDo memberDo) throws BusinessException {memberDo.setUpdatetime(new Date());if(prisonMemberMapper.selectSameCode(memberDo.getCode()) != null){throw new BusinessException("服刑人员编码重复!");}prisonMemberMapper.insertSelective(memberDo);}/*** 删除服刑人员** @param id*/@Overridepublic void delete(Integer id) {prisonMemberMapper.deleteByPrimaryKey(id);}/*** 更新人员** @param memberDo*/@Overridepublic void update(PrisonMemberDo memberDo) {prisonMemberMapper.updateByPrimaryKeySelective(memberDo);}/*** 加载服刑人员信息** @param id* @return*/@Overridepublic PrisonMemberDo load(Integer id) {return prisonMemberMapper.load(id);}/*** 导出** @param memberQo* @return*/@Overridepublic String export(PrisonMemberQo memberQo) throws BusinessException {final List<PrisonMemberDo> prisonMemberDoList = prisonMemberMapper.query(memberQo);final List<List<String>> exportList = getExportList(prisonMemberDoList);final String folderPath = propertyConfig.get("export.url", "/icooper/appdata/hbsf/export/");final String fileName = ExportConstants.FILE_NAME_PRISON_MEM + ExportConstants.UNDER_LINE + TimeUtils.currTime1()+ SCConstants.FMT_XLS;final String filePath = folderPath + fileName;final ExportExcel ex = new ExportExcel(ExportConstants.TITLE_PRISON_MEM, ExportConstants.HEADER_PRISON_MEM, exportList,folderPath, fileName);try {ex.export();} catch (final Exception e) {log.error("服刑人员导出失败", e);throw new BusinessException("服刑人员导出失败");}return filePath;}/*** 人员导入** @param file* @return*/@Overridepublic Integer upload(MultipartFile file) {Sheet sheet = null;try {sheet = POIUtils.parseSheet(file, 0);} catch (final java.io.IOException e) {e.printStackTrace();}int num = 0;for (int i = 3; i <= sheet.getLastRowNum(); i++) {final Row row = sheet.getRow(i);if (null != row && row.getCell(0) != null && StringUtils.notEmpty(row.getCell(0).toString())) {num = parserExcel(row, num);}}return num;}...//Todo}
三、Controller层调用
package cn.showclear.www.controller.data;import cn.com.scooper.common.resp.APIRespJson;
import cn.com.scooper.common.resp.ResultCode;
import cn.showclear.www.controller.BaseController;
import cn.showclear.www.pojo.base.*;
import cn.showclear.www.service.base.DataReprotServices.*;
import cn.showclear.www.utils.POIUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import java.util.List;/*** 综合业务 - 数据上报* @author YF-XIACHAOYANG* @date 2018/2/6 14:17*/
@Controller("CriminalDataController")
@RequestMapping("/data/")
public class CriminalDataController extends BaseController {private static final Logger log = LoggerFactory.getLogger(CriminalDataController.class);@Autowiredprivate PrisonDataReportService prisonDataReportService;@Autowiredprivate DrugDataReportService drugDataReportService;@Autowiredprivate CommCorrDataReportService commCorrDataReportService;/*** 查询服刑人员信息* @param prisonMemberQo* @return*/@RequestMapping("prison/mem/queryPrisonList")@ResponseBodypublic APIRespJson queryPrisonList(@ModelAttribute PrisonMemberQo prisonMemberQo) {APIRespJson apiRespJson = null;try {PageInfo<PrisonMemberDo> page = prisonDataReportService.query(prisonMemberQo);apiRespJson = new APIRespJson(ResultCode.SUCC).setData(page);} catch (Exception e) {log.error("", e);apiRespJson = new APIRespJson(ResultCode.FAIL).setMessage("服刑人员信息获取失败");}return apiRespJson;}
...
这篇关于[ JAVA ] 多个泛型对象接口定义的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!