SpringBoot+mybatis调⽤oracle函数并获取返回的游标
SpringBoot+mybatis调⽤oracle函数并获取返回的游标
需求:报表展⽰的数据列可能会改变,可能增加⼀列,减少⼀列。当⽤户需求发⽣改变时,不需要修改java代码,也不需要修改前端代码,只修改函数即可(前端也需要去动态铺数据)。
⼤致思路:
简单的讲:函数返回的结果就是⼀个Map,但是这个Map的value是⼀个Map类型的List。我们能获取到的就是第⼀个Map的key值
<!--配置返回游标中别名对应的resultMap,因为返回值不确定,所以在这只定义了resultMap,并未做映射
因为返回字段的顺序是随机的,如果想根据函数中查询出来的数据返回给前端,在这⾥使⽤LinkedHashMap,如果对顺序没有要求的话,可以直接使⽤HashMap -->
<resultMap type ="java.util.LinkedHashMap" id= "cursorMap">
</resultMap >
<!-- 函数返回的游标是Map key:value类型的,但是不能直接使⽤resultMap=Map,也不可以使⽤resultType=Map
注:可以理解为result就是返回的map的键值,可以通过result去获取数据库具体的返回的值-->
<select id="query"parameterType="map"statementType="CALLABLE">
{
#{result,mode=OUT, jdbcType=CURSOR,resultMap=cursorMap}=call NEW_FUN_TEST(
#{begin,mode=IN,jdbcType=VARCHAR},
#{end,mode=IN,jdbcType=VARCHAR}
)
}
</select>
Mapper层代码
public interface AntibioticUseIntensityMapper {
//l⽂件中的select标签并未返回数据,⽤void
void query(Map<String,Object> param);
}
Service层代码
public interface AntibioticUseIntensityService {
List<LinkedHashMap<String,Object>>query(Long begin, Long end);
}
@Autowired
private AntibioticUseIntensityMapper antibioticUseIntensityMapper;
@Override
public List<LinkedHashMap<String,Object>>query(Long begin, Long end){
String strStart =  StringDate(begin);
String strEnd =  StringDate(end);
//定义paramMap⽤于传参,以及接收返回值
Map<String,Object> paramMap =new HashMap<>();
paramMap.put("begin",strStart);
paramMap.put("end",strEnd);
oracle游标的使用
//调⽤mapper层⽅法,此时 key键result 对应的value就是sql查询出来的结果
antibioticUseIntensityMapper.query(paramMap);
//去除result中的值
List<LinkedHashMap<String, Object>> cursorList =(ArrayList<LinkedHashMap<String, Object>>) ("result");
return cursorList;
}
Controller层代码(报表的查询和导出)
@Autowired
private AntibioticUseIntensityService antibioticUseIntensityService;
/** 查询
* @param begin
* @param end
* @return
*/
@GetMapping("query")
public ResultInfo query(@RequestParam(required =true) Long begin,
@RequestParam(required =true) Long end){
try{
List<LinkedHashMap<String,Object>> dateList = antibioticUseIntensityService.query(begin, end);
if(CollectionUtils.isEmpty(dateList)){
return ResultInfo.fail();
}
//存放返回给前端的数据
Map<String,Object> map =new HashMap<>();
//因为前端的表头也通过后端动态获取,所以需要把表头和表格中数据分开传递给前端
//⽤于接收表头数据
List<HashMap> titleList =new ArrayList<>();
//遍历表头数据
for(Map.Entry<String, Object> entry : (0).entrySet()){
HashMap<String,Object> titleMap =new HashMap<>();
// label⽤于写表头,prop⽤于表头和表格数据进⾏绑定
titleMap.put("prop",Key());
titleMap.put("label",Key());
titleList.add(titleMap);
}
map.put("tableTitle",titleList);
map.put("tableData",dateList);
return ResultInfo.success().add(map);
}catch(Exception e){
e.printStackTrace();
return ResultInfo.Message());
}
}
/**
* 导出
* @param response
* @param begin
* @param end
*/
@GetMapping("/export")
public void exportExcel(HttpServletResponse response,
@RequestParam(required =true) Long begin,
@RequestParam(required =true) Long end){
// 定义表的标题
String title ="测试";
//获取后端返回数据
List<LinkedHashMap<String,Object>> dateList = antibioticUseIntensityService.query(begin, end);
//表格的表头
String [] rowsName =new (0).size()];
int index =0;
//遍历表头数据
for(Map.Entry<String, Object> entry : (0).entrySet()){
rowsName[index]= Key();
index++;
}
//导出的数据内容
List<Object[]> exportList =new ArrayList<Object[]>();
//定义表的内容
for(int i =0;i<dateList.size();i++){
for(int i =0;i<dateList.size();i++){
int j =0;
Object[] objs =new (0).size()];
for(Map.Entry<String, Object> entry : (i).entrySet()){
objs[j]= Value();
j++;
}
exportList.add(objs);
}
// 创建ExportExcel对象
ExcelUtils excelUtil =new ExcelUtils();
try{
String fileName=new String("测试.xlsx".getBytes("UTF-8"),"iso-8859-1");            portExcel(title,rowsName,exportList,fileName,response);
}catch(Exception e){
e.printStackTrace();
}
}
}
第⼀次遇见这样的问题,纪录⼀下

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。