基于springMVC+mybatis的实践记录
目前在做一个项目用到springMVC+mybatis,由于之前没学过,上手有点难,因此写下随笔记录下:
写了四个接口
第一个接口:GET请求,查询返回列表
查找 专户报告列表 -----GET
http://localhost:8080/fee/investWeeklyReport/getByAccoutId
通过postman 模拟各种参数
{ "code": 0, "message": "成功", "data": { "data": [ { "reportId": 0, "accountId": 0, "accountName": "浙商01", "accountTransactionType": 2, "accountTransactionTypeStr": "纯债及一级债", "reportDate": 1471336967000, "reportName": "专户月度数据统计", "submitTime": 1471423399000, "deadLine": 1471423391000, "reportStatus": 0 }, { "reportId": 1, "accountId": 0, "accountName": "浙商01", "accountTransactionType": 2, "accountTransactionTypeStr": "纯债及一级债", "reportDate": 1471250672000, "reportName": "交易型账户每日估算", "submitTime": 1471439967000, "deadLine": 1471423492000, "reportStatus": 0 }, { "reportId": 2, "accountId": 1, "accountName": "浙商02", "accountTransactionType": 0, "accountTransactionTypeStr": "纯债", "reportDate": 1471250706000, "reportName": "专户月度数据统计", "submitTime": 1471439970000, "deadLine": 1471439973000, "reportStatus": 0 } ], "totalcount": 3, "pageIndex": 1, "pageSize": 3 } }
Controller:
@Autowired AccountReportService accountReportService; /** * 根据当前系统登录用户的userId得到 相关专户的报表 * @param reportTypes * @param transactionTypes * @param reportStatuss * @param pageIndex * @param pageSize * @param userId * @return * @throws DatayesException * @throws ParseException */ @RequestMapping(value = "/accountReport/searchReport", method = RequestMethod.GET) public Object searchReport( @RequestParam(value = "reportTypes",required = false) String reportTypes, @RequestParam(value = "transactionTypes",required = false) String transactionTypes, @RequestParam(value = "reportStatuss",required = false) String reportStatuss, @RequestParam(value = "pageIndex",required = false) Integer pageIndex, @RequestParam(value = "pageSize",required = false) Integer pageSize, @RequestParam(value = "userId",required = true) Integer userId)throws DatayesException, ParseException { if (pageSize == null) pageSize = Constant.MAX_RECORD_NUM; if (pageIndex == null) pageIndex = 1; AccoutReportSearchRequest searchRequest = new AccoutReportSearchRequest(); searchRequest.setPageIndex(pageIndex); searchRequest.setPageSize(pageSize); List<Integer> tempReportStatusList = formatList(reportStatuss); List<Integer> tempReportTypeList = formatList(reportTypes); List<Integer> tempTransactionTypeList = formatList(transactionTypes); searchRequest.setReportStatusList(tempReportStatusList); searchRequest.setReportTypeList(tempReportTypeList); searchRequest.setTransactionTypeList(tempTransactionTypeList); searchRequest.setUserId(userId); Page<AccoutReportShow> list = accountReportService.searchReportList(searchRequest); return list; }
/** * 2016-08-17 * 通用的把相同名称的多个参数值 用逗号分隔的转换成 数组列表 * 因为在数据库查询中 in 语句 必须用 列表或数组才行 * @param params * @return */ public <T> List<T> formatList(String params){ String[] arrs = params.split(","); if(arrs.length>0){ List<T> list = new ArrayList<T>(); for(String s:arrs){ T t = (T)s; list.add(t); } return list; } return null; }
把请求参数封装到AccoutReportSearchRequest 实体中,传到业务层。具体后台不再复制代码;
接口二:
接口:添加新的 专户报告
POST 请求 请求参数封装在 请求AccountReportAddRequest 中
http://localhost:8080/fee/accountReport/addReport ----POST
请求参数格式:
{"accountId":0,"reportDate":"2016-08-18","name":"测试报表","deadline":"2016年08月18日 12:00","type":3}
自动添加新的专户报告 和 投资周报计划
暂没设置响应。
请求实体字段:
@NotNull public String reportDate; @NotNull public String deadline; @NotNull public Long accountId; @NotNull public String name; @NotNull public Integer type;
Controller:
@RequestMapping(value = "/accountReport/addReport", method = RequestMethod.POST) public void addReport( @Valid @RequestBody AccountReportAddRequest request)throws DatayesException, ParseException { AccountReport accountReport = new AccountReport(); accountReport.setAccountId(request.getAccountId()); accountReport.setName(request.getName()); Date deadLine = DateFormatUtil.formatDateToDateY_M_D_H_MByString(request.getDeadline()); accountReport.setDeadline(deadLine); accountReport.setType(request.getType()); Date reportDate = DateFormatUtil.formatDateToE_DateY_M_DByString(request.getReportDate()); accountReport.setReportDate(reportDate); accountReportService.addAccountReport(accountReport); }
第三个接口:
接口:通过 指定的 专户报告找到对应的 投资周报 ----GET
http://localhost:8080/fee/investWeeklyReport/getByAccoutId?accounReporttId=1 { "code": 0, "message": "成功", "data": { "investWeeklyReport": { "id": 1, "accountReportId": 1, "startTime": 1471255400000, "endTime": 1471601000000, "fundManager": null, "openingVolume": null, "lightenVolume": null, "expectationAttitude": null, "summary": null, "createTime": 1471514600000, "updateTime": 1471514600000, "marketReview": null, "configStructure": null, "accountStatus": null, "briefDescription": null, "investmentPlan": null }, "accountReport": { "id": 1, "accountId": 0, "reportDate": 1471449600000, "name": "测试报表", "deadline": 1471492800000, "status": 0, "submitTime": 1471513743000, "type": 3 } } }
Controller:
@Autowired private AccountReportService accountReportService; @Autowired private InvestWeeklyReportService investWeeklyReportService; /** * 通过 accountReportId 值得到 投资周报表 * @param accountReportId * @return */ @RequestMapping(value = "/investWeeklyReport/getByAccoutId", method = RequestMethod.GET) public Object getByAccoutId(@RequestParam(value = "accountReportId",required = true) Long accountReportId){ InvestWeeklyReportSearchByAccountIdResponse response = new InvestWeeklyReportSearchByAccountIdResponse(); AccountReport accountReport = accountReportService.getById(accountReportId); InvestWeeklyReport investWeeklyReport = investWeeklyReportService.getByAccountReportId(accountReportId); response.setAccountReport(accountReport); response.setInvestWeeklyReport(investWeeklyReport); return response; }
接口:提交投资周报 方法 根据提交的Id号
http://localhost:8080/fee/investWeeklyReport/submit ---POST
响应:
{ "id": 1, "accountReportId": 1, "fundManager":"基金经理1", "openingVolume": 12, "lightenVolume": 13, "expectationAttitude": 1, "summary": "一句话概括", "marketReview": "市场回顾", "configStructure": "配置结构", "accountStatus": "期末账户状态", "briefDescription": "简要说明", "investmentPlan": "投资计划" } 响应: { "code": 0, "message": "成功", "data": { "id": 1, "accountReportId": 1, "startTime": 1471255400000, "endTime": 1471601000000, "fundManager": "基金经理1", "openingVolume": 12, "lightenVolume": 13, "expectationAttitude": 1, "summary": "一句话概括", "createTime": 1471514600000, "updateTime": 1471514600000, "marketReview": "市场回顾", "configStructure": "配置结构", "accountStatus": "期末账户状态", "briefDescription": "简要说明", "investmentPlan": "投资计划" } }
/** * 提交保存 * @return */ @RequestMapping(value = "/investWeeklyReport/submit", method = RequestMethod.POST) public Object submitInvestWeeklyReport(@Valid @RequestBody InvetWeeklyReportSubmitResponse request){ InvestWeeklyReportWithBLOBs blobs = investWeeklyReportService.submitInvetsWeeklyReport(request); return blobs; }