之前一直没有用mybatis调过sqlserver的存储过程,今天抽时间写代码测试了下。
首先在sqlserver建存储过程(简单的写个测试存储过程,输入两个参数,返回查询列表数据):
create PROCEDURE [dbo].[testPro]
@sc VARCHAR(20), -----输入基金代码
@EndDate DATETIME -----输入日期
AS
BEGIN
select s.SecuCode,s.SecuAbbr,t.EndDate,t.StatisticInterval,t.StockPickingAbility from MFE_FundRatingValueNew t,SecuMain s where t.InnerCode = s.InnerCode
and s.SecuCategory in (8,13) and s.SecuCode = @sc and t.EndDate = @EndDate
END
然后在mybatis中的mapper映射文件中加入调用sqlserver存储过程的逻辑:
select标签中parameterMap为入参map,对应id="ParaMap_test"这个集合,包括两个参数sc和EndDate;statementType="CALLABLE"为固定写法,表明是调用存储过程;resultMap="ResultMap_test"表示执行存储过程返回的结果集,对应id="ResultMap_test"这个集合,集合中包括五个参数,对应存储过程中查询结果集的五列数据; {call testPro(#{sc},#{EndDate})}为调用存储过程的语句,sc和EndDate为入参。
如果存错过程需要返回多个查询结果集,可以再resultMap中加多个返回map,下面测试下。首先修改下存储过程:
alter PROCEDURE [dbo].[testPro]
@sc VARCHAR(20), -----输入基金代码
@EndDate DATETIME -----输入日期
AS
BEGIN
select s.SecuCode,s.SecuAbbr,t.EndDate,t.StatisticInterval,t.StockPickingAbility from MFE_FundRatingValueNew t,SecuMain s where t.InnerCode = s.InnerCode
and s.SecuCategory in (8,13) and s.SecuCode = @sc and t.EndDate = @EndDate
select s.SecuCode,s.SecuAbbr,t.EndDate,t.StatisticInterval,t.DownBeta from MFE_FundRatingValueNew t,SecuMain s where t.InnerCode = s.InnerCode
and s.SecuCategory in (8,13) and s.SecuCode = @sc and t.EndDate = @EndDate
END
然后修改mybatis映射文件:
select标签下的resultMap的值调整成"ResultMap_test,ResultMap_test2",增加了一个返回结果集,并在下面添加了这个集合
至此mybatis的映射文件已经书写完毕,下面把dao/service/controller层代码也列出来。首先是dao层:
package com.shjj.dao;
import java.util.List;
import java.util.Map;
public interface SqlServerDao {
/**
* 测试存储过程
*/
List
然后是,service层:
package com.shjj.service;
import java.util.Map;
public interface ISqlServerService {
/**
* 测试存储过程
*/
Object getFundTest(Map parameter);
}
package com.shjj.serviceimpl;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shjj.config.datasource.TargetDataSource;
import com.shjj.dao.SqlServerDao;
import com.shjj.result.GlobalErrorInfoEnum;
import com.shjj.result.ResultBody;
import com.shjj.service.ISqlServerService;
@Service
public class SqlServerServiceImpl implements ISqlServerService {
private Logger log =LoggerFactory.getLogger(FundServiceImpl.class);
@Autowired
private SqlServerDao sqlServerDao;
@Override
@TargetDataSource("ds1")
public Object getFundTest(Map parameter) {
// TODO Auto-generated method stub
return sqlServerDao.getFundTest(parameter);
}
}
最后是controller层:
package com.shjj.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONArray;
import com.shjj.result.GlobalErrorInfoEnum;
import com.shjj.result.GlobalErrorInfoException;
import com.shjj.service.IFundService;
import com.shjj.service.ISqlServerService;
import com.shjj.util.Util;
/**
* @author GongSizhen
* @Description 基金相关请求入口
* @date 2021年3月10日下午2:26:37
*/
@RestController
@CrossOrigin
public class FundController {
private Logger log =LoggerFactory.getLogger(FundController.class);
@Autowired
ISqlServerService sqlServerService;
/**
* 存储过程测试
*/
@SuppressWarnings("rawtypes")
@RequestMapping("/app/fundTest")
public Object queryFundTest(HttpServletRequest req){
String sc = req.getParameter("fundCode");
String EndDate = req.getParameter("endDate");
Map param = new HashMap<>();
param.put("sc", sc);
param.put("EndDate", EndDate);
return sqlServerService.getFundTest(param);
}
}
上面就是所有的代码,启动服务后,浏览器中输入地址:http://127.0.0.1/app/fundTest?fundCode=000001&endDate=20190630,可以正常返回接口数据: