博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis-generator生成Service和Controller
阅读量:6954 次
发布时间:2019-06-27

本文共 8764 字,大约阅读时间需要 29 分钟。

  好久记录笔记,这段时间做政府的项目,数据录入系统基本都是通过excel导入,且数据量大,许多也是单表的录入,这就有很多可以通用的代码,如controller,service层的那一套都是可以代码生成,添加了一个数据库批量添加接口(目前只支持oracle),代码是基于mybatis-generator-1.3.5源码修改后的,具体的源码解析,后面等项目上线后,再好好整理一下,这里就粗鲁的记录如何使用。

1:mybatis-generator.xml 配置文件

2:执行生成代码

 2-1:最后生成 InsertBatch

insert into FIXED_ASSETS_INDICATOR (ID, ORDER_MARK, COUNT_TIME, CITY, CITY_CODE, FIXED_INVESTMENT_TOTAL, FIXED_INVESTMENT_SPEED_UP, FOLK_INVESTMENT_TOTAL, FOLK_INVESTMENT_SPEED_UP, REALTY_INVESTMENT_TOTAL, REALTY_INVESTMENT_SPEED_UP, EMPLOYMENT_INVESTMENT_TOTAL, EMPLOYMENT_INVESTMENT_SPEED_UP, TECHNOLOGY_INVESTMENT_TOTAL, TECHNOLOGY_INVESTMENT_SPEED_UP, INFRASTRUCTURE_TOTAL, INFRASTRUCTURE_SPEED_UP, HIGH_TECH_TOTAL, HIGH_TECH_SPEED_UP, MANUFACTURING_TOTAL, MANUFACTURING_SPEED_UP)
SELECT #{item.id,jdbcType=VARCHAR}, #{item.orderMark,jdbcType=DECIMAL}, #{item.countTime,jdbcType=TIMESTAMP}, #{item.city,jdbcType=VARCHAR}, #{item.cityCode,jdbcType=VARCHAR}, #{item.fixedInvestmentTotal,jdbcType=DECIMAL}, #{item.fixedInvestmentSpeedUp,jdbcType=FLOAT}, #{item.folkInvestmentTotal,jdbcType=DECIMAL}, #{item.folkInvestmentSpeedUp,jdbcType=FLOAT}, #{item.realtyInvestmentTotal,jdbcType=DECIMAL}, #{item.realtyInvestmentSpeedUp,jdbcType=FLOAT}, #{item.employmentInvestmentTotal,jdbcType=DECIMAL}, #{item.employmentInvestmentSpeedUp,jdbcType=FLOAT}, #{item.technologyInvestmentTotal,jdbcType=DECIMAL}, #{item.technologyInvestmentSpeedUp,jdbcType=FLOAT}, #{item.infrastructureTotal,jdbcType=DECIMAL}, #{item.infrastructureSpeedUp,jdbcType=FLOAT}, #{item.highTechTotal,jdbcType=DECIMAL}, #{item.highTechSpeedUp,jdbcType=FLOAT}, #{item.manufacturingTotal,jdbcType=DECIMAL}, #{item.manufacturingSpeedUp,jdbcType=FLOAT} FROM DUAL

 2-2:生成service

public class FixedAssetsIndicatorServiceImpl implements FixedAssetsIndicatorService {    @Autowired    private FixedAssetsIndicatorMapper fixedAssetsIndicatorMapper;    public int insertBatch(List
list) { if(list != null && list.size() > 0 ){ FixedAssetsIndicatorExample fixedAssetsIndicatorExample = new FixedAssetsIndicatorExample(); fixedAssetsIndicatorExample.createCriteria().andCountTimeEqualTo(list.get(0).getCountTime()); fixedAssetsIndicatorMapper.deleteByExample(fixedAssetsIndicatorExample); return fixedAssetsIndicatorMapper.insertBatch(list); } return 0; } public PageInfo
list(ListFixedAssetsIndicatorParam param) { FixedAssetsIndicatorExample example = new FixedAssetsIndicatorExample(); if(param.getCountTime() != null){ example.createCriteria().andCountTimeEqualTo(param.getCountTime()); } PageHelper.startPage(param.getPageNum(), param.getPageSize()); example.setOrderByClause("ORDER_MARK"); List
fromDB = fixedAssetsIndicatorMapper.selectByExample(example); PageInfo pageInfo = new PageInfo(fromDB); return pageInfo; } public FixedAssetsIndicator get(String id) { return fixedAssetsIndicatorMapper.selectByPrimaryKey(id); } public void save(FixedAssetsIndicator toDB) { fixedAssetsIndicatorMapper.insertSelective(toDB); } public void delete(String id) { fixedAssetsIndicatorMapper.deleteByPrimaryKey(id); } public void update(FixedAssetsIndicator toDB) { fixedAssetsIndicatorMapper.updateByPrimaryKeySelective(toDB); } }

 2-3:生成controller:添加excel导入导出接口(基于easypoi导入导出)

@Slf4j@Controller@RequestMapping("/fixedAssetsIndicator")@Api(description = "能源投资统计科:分市固定资产投资主要指标")public class FixedAssetsIndicatorController extends BaseController {    @Autowired    private FixedAssetsIndicatorService fixedAssetsIndicatorService;    @ApiOperation(value = "列表查询", httpMethod = "POST") @RequestMapping("/list.do") @ResponseBody public JSONResult list(@Validated ListFixedAssetsIndicatorParam param) throws ShsoftException { JSONResult result = new JSONResult(fixedAssetsIndicatorService.list(param)); return result; } @ApiOperation(value = "单条查询", httpMethod = "POST") @RequestMapping("/get.do") @ResponseBody public JSONResult get(String id) throws ShsoftException { JSONResult result = new JSONResult(fixedAssetsIndicatorService.get(id)); return result; } @ApiOperation(value = "删除", httpMethod = "POST") @RequestMapping("/delete.do") @ResponseBody public JSONResult delete(String id) throws ShsoftException { fixedAssetsIndicatorService.delete(id); return new JSONResult(); } @ApiOperation(value = "新增", httpMethod = "POST") @RequestMapping("/save.do") @ResponseBody public JSONResult save(@Validated FixedAssetsIndicator toDB) throws ShsoftException { toDB.setId(new UUIDFactory().generate().toString()); fixedAssetsIndicatorService.save(toDB); return new JSONResult(); } @ApiOperation(value = "修改", httpMethod = "POST") @RequestMapping("/update.do") @ResponseBody public JSONResult update(@Validated FixedAssetsIndicator toDB) throws ShsoftException { fixedAssetsIndicatorService.update(toDB); return new JSONResult(); } @ApiOperation(value = "导出", httpMethod = "POST") @RequestMapping("/export.do") @ResponseBody public JSONResult exportExcel(@Validated ListFixedAssetsIndicatorParam param, HttpServletRequest request, HttpServletResponse response) throws ShsoftException { JSONResult result = new JSONResult(); PageInfo
pageInfo = fixedAssetsIndicatorService.list(param); List
list = pageInfo.getList(); List
> listMap = new ArrayList
>(); if(list != null && list.size() > 0){ for (int i = 0; i < list.size(); i++) { Map
lm = new HashMap
(); if(list.get(i).getCity() != null ){ lm.put("city", list.get(i).getCity()); } if(list.get(i).getCityCode() != null ){ lm.put("cityCode", list.get(i).getCityCode()); } if(list.get(i).getFixedInvestmentTotal() != null ){ lm.put("fixedInvestmentTotal", list.get(i).getFixedInvestmentTotal()); } if(list.get(i).getFixedInvestmentSpeedUp() != null ){ lm.put("fixedInvestmentSpeedUp", list.get(i).getFixedInvestmentSpeedUp()); } if(list.get(i).getFolkInvestmentTotal() != null ){ lm.put("folkInvestmentTotal", list.get(i).getFolkInvestmentTotal()); } if(list.get(i).getFolkInvestmentSpeedUp() != null ){ lm.put("folkInvestmentSpeedUp", list.get(i).getFolkInvestmentSpeedUp()); } listMap.add(lm); } } Calendar calendar = Calendar.getInstance(); calendar.setTime(param.getCountTime()); Map
map = new HashMap
(); map.put("maplist", listMap); map.put("year", calendar.get(Calendar.YEAR)); map.put("month", calendar.get(Calendar.MONTH + 1)); TemplateExportParams params = new TemplateExportParams("excel_temple/固定资产投资/分市固定资产投资主要指标.xls"); Workbook workbook = ExcelExportUtil.exportExcel(params, map); OutputStream os = null; try { response.setHeader("content-Type", "application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-disposition","attachment;filename=分市固定资产投资主要指标.xls"); os = response.getOutputStream(); workbook.write(os); os.flush(); } catch (Exception e) { e.printStackTrace(); } return result; } @ApiOperation(value = "导入", httpMethod = "POST") @RequestMapping("/import.do") @ResponseBody public JSONResult importExcel(HttpServletRequest request) throws ShsoftException { MultipartHttpServletRequest multipartHttpServletRequest = ((MultipartHttpServletRequest) request); MultipartFile file = multipartHttpServletRequest.getFile("file"); JSONResult result = new JSONResult(); ImportParams params = new ImportParams(); params.setTitleRows(3); params.setHeadRows(1); params.setStartRows(1); try { if(file.getSize() > 0){ List
dataList = new ShExcelImportUtils().importExcelByIs(file.getInputStream(), FixedAssetsIndicator.class, params, false).getList(); fixedAssetsIndicatorService.insertBatch(dataList); }else{ result = new JSONResult(new ErrorMessage(Error.DAMPE_FIELD_UNAUTH.getCode(),Error.DAMPE_FIELD_UNAUTH.getMessage())); } } catch (Exception e) { throw new ShsoftException(e.getMessage(),e); } return result; } }

 若文章在表述和代码方面如有不妥之处,欢迎批评指正。留下你的脚印,欢迎评论!希望能互相学习。需要源码和jar包的留下邮箱

 

转载于:https://www.cnblogs.com/lishun1005/p/10057129.html

你可能感兴趣的文章
HashMap[转]
查看>>
面向对象程序设计——总结作业
查看>>
linux之 sed命令
查看>>
oracle rac的特征
查看>>
Linux之 find之 ctime,atime,mtime
查看>>
mysql查询
查看>>
Mongodb JAVA API
查看>>
MacFree ePlicy Orchestrator
查看>>
python基础之列表生成式和生成器
查看>>
关于java.lang.ClassNotFoundException
查看>>
转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法
查看>>
一条SQL语句查询出成绩名次 排名 (转)
查看>>
什么是.NET应用程序域
查看>>
ZooKeeper 安装与部署
查看>>
Picnic Planning
查看>>
文章标题
查看>>
约瑟夫问题
查看>>
NinePatchChunk.java分析
查看>>
非template/render模式下使用iview组件时标签需要转化
查看>>
搜狐笔试 最大连续递增子段和 关键词连续递增
查看>>