MyBatis调用存储过程返回多条数据结果集

MyBatis调用存储过程返回多条数据结果集

需求:执行存储过程返回三条数据结果集返回前端

执行存储过程效果如下所示:

MyBatis调用存储过程返回多条数据结果集_第1张图片

三种数据分别代表盘点盈余,盘点亏损,盘点正常,最终实现结果:

MyBatis调用存储过程返回多条数据结果集_第2张图片

现要求一个接口返回三种数据

Service层

List<List<?>> queryCaleResult(Map map);

Mapper文件

用于调用存储过程返回最终结果

    <parameterMap id="pm" type="java.util.Map">
        <parameter property="inventoryId" jdbcType="INTEGER" mode="IN"/>
        <parameter property="inventoryMode" jdbcType="INTEGER" mode="IN"/>
    </parameterMap>
    <select id="queryCaleResult" parameterMap="pm" statementType="CALLABLE" resultMap="VAssetInventoryScan,VAssetStock,VAssetInventoryScan"
            resultType="java.util.List">
            <![CDATA[
        {
           call dbo.CalcInventoryResult(
                #{inventoryId,mode=IN, jdbcType=INTEGER},
                #{inventoryMode,mode=IN,jdbcType=INTEGER}
            )
        }
       ]]>
    </select>

解释说明:

parameterMap 为参数列表,调用存储过程需要statementType=“CALLABLE”,resultMap为定义映射返回结果视图,可跟多个用逗号分隔 ,具体如下

 <resultMap type="com.example.phonegd.inventory.pojo.VAssetInventoryScan" id="VAssetInventoryScan">
        <result column="InventoryScanId" property="InventoryScanId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="InventoryId" property="InventoryId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="AssocId" property="AssocId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="BarCodeId" property="BarCodeId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="InventoryDetailId" property="InventoryDetailId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="RecordId" property="RecordId"   javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="TypeId" property="TypeId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="WareOrDeptOrStaff" property="WareOrDeptOrStaff"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="ScanMode" property="ScanMode"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="AssocStr" property="AssocStr"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="ScanId"  property="ScanId" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="ScanUserName" property="ScanUserName"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="ScanTime"  property="ScanTime" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="InventoryResult" property="InventoryResult"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="IsDeal" property="IsDeal"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="DealId"  property="DealId" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="DealUserName" property="DealUserName"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="DealTime" property="DealTime"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="InventoryScanNotes" property="InventoryScanNotes"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="InventoryCode" property="InventoryCode"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="InventoryMode" property="InventoryMode"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="InventoryModeStr" property="InventoryModeStr"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="InventoryOrderBuildTime" property="InventoryOrderBuildTime"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="InventoryAssocStr" property="InventoryAssocStr"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="InventoryAssoc" property="InventoryAssoc"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="StockAmount" property="StockAmount" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="ScanAmount" property="ScanAmount" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="CheckFlag" property="CheckFlag"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="CheckId" property="CheckId"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="CheckName" property="CheckName"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="CheckTime" property="CheckTime"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="AssetCode" property="AssetCode"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="AssetName" property="AssetName"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="LifeMonth" property="LifeMonth"  javaType="java.lang.Double" jdbcType="DOUBLE"/>
        <result column="Salvage" property="Salvage"  javaType="java.lang.Double" jdbcType="DOUBLE"/>
        <result column="Unit" property="Unit" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="IsPatrol" property="IsPatrol"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="PatolCycle" property="PatolCycle"  javaType="java.lang.Double" jdbcType="DOUBLE"/>
        <result column="AssetValue" property="AssetValue"  javaType="java.lang.Double" jdbcType="DOUBLE"/>
        <result column="DepreciationType" property="DepreciationType"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="RecordType" property="RecordType"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="RecordAmount" property="RecordAmount"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="RecordTime" property="RecordTime"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="UseMouth" property="UseMouth"  javaType="java.lang.Double" jdbcType="DOUBLE"/>
        <result column="RecordNotes" property="RecordNotes"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="TypeName" property="TypeName"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="BarCode" property="BarCode" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="PrintTime" property="PrintTime"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="OperatorStatus" property="OperatorStatus" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="IsDispose" property="IsDispose" javaType="java.lang.Integer" jdbcType="INTEGER"/>
    </resultMap>
 <resultMap type="com.example.phonegd.inventory.pojo.VAssetInventoryStock" id="VAssetStock">
        <result column="StockId" property="StockId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="AssetBarCodeId" property="AssetBarCodeId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="WareId" property="WareId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="UserId" property="UserId" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="RecordId" property="RecordId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="TypeId" property="TypeId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="BarCodeId" property="BarCodeId"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="InTime" property="InTime"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="AssetCode" property="AssetCode"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="AssetName" property="AssetName"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="LifeMonth" property="LifeMonth"  javaType="java.lang.Double" jdbcType="DOUBLE"/>
        <result column="Salvage"  property="Salvage" javaType="java.lang.Double" jdbcType="DOUBLE"/>
        <result column="Unit" property="Unit" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="IsPatrol" property="IsPatrol" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="PatolCycle" property="PatolCycle" javaType="java.lang.Double" jdbcType="DOUBLE"/>
        <result column="AssetValue"  property="AssetValue" javaType="java.lang.Double" jdbcType="DOUBLE"/>
        <result column="DepreciationType" property="DepreciationType" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="RecordType" property="RecordType" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="Amount" property="Amount" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="RecordTime" property="RecordTime"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="UseMouth" property="UseMouth"  javaType="java.lang.Double" jdbcType="DOUBLE"/>
        <result column="RecordNotes" property="RecordNotes" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="TypeName" property="TypeName" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="BarCode" property="BarCode"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="PrintTime" property="PrintTime"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="OperatorStatus" property="OperatorStatus"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="IsDispose" property="IsDispose" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="WareCode" property="WareCode"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="WareName" property="WareName" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="HeaderName" property="HeaderName" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="PlaceAddress" property="PlaceAddress" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="IsPrintBarCode" property="IsPrintBarCode" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="WareHouseNotes" property="WareHouseNotes"  javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="UserName" property="UserName" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="IsUsed" property="IsUsed" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="AllowDelete" property="AllowDelete"  javaType="java.lang.Integer" jdbcType="INTEGER"/>
    </resultMap>

注意:这里需要注意的地方,property不能省略,否则会数据全为null,type为对应的实体类,类似下图

MyBatis调用存储过程返回多条数据结果集_第3张图片

Controller层

 @PostMapping("/caleResult")
    public BaseResponse queryCaleResult(@RequestBody Map map){
        BaseResponse response= null;
        try {
            response = new BaseResponse(StatusCode.Success);
            Map mm=new HashMap();
            List<List<?>> result=null;
            if(Integer.parseInt(String.valueOf(map.get("inventoryMode")))==1){
                result = inventoryService.queryCaleResult(map);
                mm.put("VAssetInventoryScan",result.get(0));
                mm.put("VAssetStock",result.get(1));
                mm.put("VAssetInventoryNormal",result.get(2));
            }else if(Integer.parseInt(String.valueOf(map.get("inventoryMode")))==2){
                result = inventoryService.queryCaleResult2(map);
                mm.put("VAssetInventoryScan",result.get(0));
                mm.put("VAssetStock",result.get(1));
                mm.put("VAssetInventoryNormal",result.get(2));
            }
            response.setData(mm);
        } catch (NumberFormatException e) {
           response=new BaseResponse(StatusCode.SysBusy);
        }
        return response;
    }

PostMan测试

MyBatis调用存储过程返回多条数据结果集_第4张图片

你可能感兴趣的:(java,mybatis)