简体   繁体   中英

Mapping a result set (of two integers) in mybatis?

I have the following stored procedure - simply returning two integers for now :

USE [DB_NAME_HERE]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[PR_PROCEDURE_NAME]
SELECT 0 AS ValueA,0 AS ValueB

and I have the following mapper XML :

  <resultMap id="BaseResultMap" type="com.companyname.service.mybatis.model.modelClass">
    <result column="ValueA" jdbcType="INTEGER" property="valueA" />
    <result column="ValueB" jdbcType="INTEGER" property="valueB" />
  </resultMap>

  <select id="getPurgedTokens" resultMap="BaseResultMap" statementType="CALLABLE">
      {call PR_PROCEDURE_NAME(
          #{resultContainer.valueA, mode=OUT, jdbcType=INTEGER},
          #{resultContainer.valueB, mode=OUT, jdbcType=INTEGER}
      )}
  </select>

Then I have the following java class for the mapping :

public class modelClass implements Serializable {

    private int valueA;
    private int valueB;

    public int getValueA() { return this.valueA; }
    public void setValueA(int valueA) { this.valueA = valueA; }

    public int getValueB() { return this.valueB; }
    public void setValueB(int valueB) { this.valueB= valueB; }
}

But when I run up my application, and attempt to get any kind of data, I get the following error :

code=DataIntegrityViolationException,messages=[SqlSession operation; SQL []; Invalid JDBC call escape at line position 10.; nested exception is java.sql.SQLException: Invalid JDBC call escape at line position 10., Invalid JDBC call escape at line position 10.]]

All help here is greatly appreciated - I have been trying to get this working for days!

EDIT : UPDATE WITH EXACT COPIES OF LATEST CODE!

MapperXML

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.company.product.mybatis.mappers.MapperXML">

    <!-- Commented out the base result map because now we link directly in the paramter type? -->

    <!--
    <resultMap id="BaseResultMap" type="com.companyname.service.mybatis.model.modelClass">
    <result column="ValueA" javaType="java.lang.Integer" jdbcType="INTEGER" property="valueA"/>
    <result column="ValueB" javaType="java.lang.Integer" jdbcType="INTEGER" property="valueB"/>
  </resultMap>-->

  <!-- -------------------------------------------- -->

  <select id="getPurgedTokens" parameterType="com.companyname.service.mybatis.model.ModelClass" statementType="CALLABLE">
    {call PR_TokenPurge_Clean(
      #{dvalueA, mode=OUT, jdbcType=INTEGER},
      #{valueB, mode=OUT, jdbcType=INTEGER}
    )}
  </select>
</mapper>

ModelClass

package com.companyname.service.mybatis.model;

import java.io.Serializable;

public class ModelClass implements Serializable {

    private int valueA;
    private int valueB;


    public int getVavlueA() { return this.valueA; }
    public void setValueA(int valueA) { this.valueA = valueA; }

    public int getValueB() { return this.valueB; }
    public void setValueB(int valueB) { this.valueB = valueB; }
}

DAO

package com.company.product.dao.mybatis;

[imports (removed for brevity and security)...]


@Repository
public class ModelClassDaoMybatis implements ModelClassDao {

    private TokenPurgeModelMapper tokenPurgeModelMapper;
    private SqlSessionOperations sqlSessionOperations;

    @Override
    public TokenPurgeModel purgeTokens()
    {
        if (this.sqlSessionOperations.selectOne(".getPurgedTokens") != null)
        {
            System.out.println("I FOUND A VALUE!!!!!!");
            return (TokenPurgeModel) sqlSessionOperations.selectOne(testing);
        }
        else
        {
            System.out.println("No value found for select statement");
            return null;
        }
    }

    @Override
    public List<TokenPurgeModel> getPurgedTokens() {
        return tokenPurgeModelMapper.getPurgedTokens();
    }

    @Required
    public void setTokenPurgeModelMapper(final TokenPurgeModelMapper tokenPurgeMapper) {
        this.tokenPurgeModelMapper = tokenPurgeMapper;
    }
    @Required
    public void setSqlSessionOperations(SqlSessionOperations sqlSessionOperations) {
        this.sqlSessionOperations = sqlSessionOperations;
    }
}

MODEL MAPPER CLASS

package com.company.product.mybatis.mappers;


[imports (removed for brevity and security)...]

public interface TokenPurgeModelMapper {

    // This is the one I am using right now. (as seen in the DAO!)
    TokenPurgeModel purgeTokens();

    // Get the list of tokens which have been removed from the PAN table.
    List<TokenPurgeModel> getPurgedTokens(); // This is actually never used - will be used in later funcationality!
}

THANKS for looking at this for me!

Indeed, first thing is to use syntax {call PR_PROCEDURE_NAME()}

Then it depends on how the procedure is written and on which SGBD is used. There are multiple possibilities including:

Return the values as procedure OUT parameters:

<select id="getPurgedTokens" parameterType="com.companyname.service.mybatis.model.modelClass" statementType="CALLABLE">
    {call PR_PROCEDURE_NAME(
        #{valueA, mode=OUT, jdbcType=INTEGER},
        #{valueB, mode=OUT, jdbcType=INTEGER}
    )}
</select>

The modelClass parameter does not actually provide input parameters in this case and is used here just as target container for OUT parameters; and then you don't need resultMap .

If the result becomes more complex, return a cursor on the select:

{call PR_PROCEDURE_NAME(
    #{resultContainer.resultList, mode=OUT, jdbcType=CURSOR, javaType=java.sql.ResultSet, resultMap=BaseResultMap}
)}

Or a function (instead of a procedure) returning the cursor fetching the Select (some SGBD may allow writing just the SELECT in the body), then merely:

SELECT function_name from DUAL

bound with the resultMap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM