首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MyBatis: java.sql.SQLException:无效的java.sql.Types常量值-9传递给set或update方法

MyBatis: java.sql.SQLException:无效的java.sql.Types常量值-9传递给set或update方法
EN

Stack Overflow用户
提问于 2019-11-13 10:33:26
回答 1查看 704关注 0票数 0

当从Stored_Procedure调用SqlServer中的MyBatis时,Im会收到此错误:

代码语言:javascript
复制
> ### Error querying database.  Cause: java.sql.SQLException: Invalid java.sql.Types constant value -9 passed to set or update method.
> ### The error may exist in mybatis-mapper/UpstreamMapper.xml
> ### The error may involve callStoredProcedure
> ### The error occurred while executing a query
> ### SQL: {call dbo.get_discount_value(?,?,       ?,?,?,?)}
> ### Cause: java.sql.SQLException: Invalid java.sql.Types constant value -9 passed to set or update method.

是否需要将Stored_Procedure中的变量类型与POJO的类型匹配?

因此,如果这些是Stored_Procedure的变量:

代码语言:javascript
复制
CREATE PROCEDURE [dbo].[get_discount_value]
    @firstname nvarchar(50),
    @lastname nvarchar(10),
    @gender nvarchar(1),
    @dateOfBirth date,
    @age bigint
AS
BEGIN 
 .. SP BODY..

POJO: StoredProcRef.java

代码语言:javascript
复制
public class StoredProcRef {

    String  SP_FirstName= "";
    String  SP_LastName= "";
    String  SP_Gender = "";
    Date    SP_Birthday = null;
    Integer SP_Age = 0;
    String  Output = "";
    ..GETTERS AND SETTERS..
}

MapperXML.xml

代码语言:javascript
复制
<resultMap id = "ReferenceValueParams"  type="StoredProcRef" autoMapping="false" >
      <result property = "SP_FirstName"         column = "SP_FirstName"/>
      <result property = "SP_LastName"          column = "SP_LastName"/>
      <result property = "SP_Gender"            column = "SP_Gender"/>
      <result property = "SP_Birthday"          column = "SP_Birthday"/>
      <result property = "SP_Age"               column = "SP_Age"/>
      <result property = "Output"               column = "DiscountValue"/>
   </resultMap>   

        <select id = "callStoredProcedure" statementType = "CALLABLE" resultMap="ReferenceValueParams">
              {call dbo.lab_get_result_form_field(
     #{SP_FirstName,mode=IN,jdbcType=NVARCHAR},
     #{SP_LastName,mode=IN,jdbcType=NVARCHAR},
     #{SP_Gender,mode=IN,jdbcType=NVARCHAR},
     #{SP_Birthday,mode=IN,jdbcType=DATE},
     #{SP_Age,mode=IN,jdbcType=BIGINT},
     #{Output,mode=OUT,jdbcType=NVARCHAR}
    )}
      </select> 

Interfacemapper.java

代码语言:javascript
复制
public interface UpstreamXmlMapper {   
         public List<Map<String, ?>> callStoredProcedure(String FirstName, String LastName, String gender, Date dateOfBirth, Integer age);
}

Service.java

代码语言:javascript
复制
    public List<Map<String, ?>> getDiscountValue( StoredProcRef storedProcRef ) {

                List<Map<String, ?>> referenceValue = new ArrayList<>();
                SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
                try {
                    UpstreamXmlMapper applicationMapper = session.getMapper(UpstreamXmlMapper.class);
                    referenceValue = applicationMapper.callStoredProcedure(storedProcRef.getSP_FirstName(), storedProcRef.getLastName(), storedProcRef.getSP_Gender(), storedProcRef.getSP_Birthday(), storedProcRef.getSP_Age()) ;

                    session.commit();
                } catch (Exception e) {
                    LOGGER.error(e);
                } finally {
                    session.close();
                }
                return referenceValue;          
        }       

Main.java

代码语言:javascript
复制
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date;
        date = sdf.parse("01-Oct-1984");

        List<StoredProcRef> storedProcRefList = new ArrayList<>();
        StoredProcRef storedProcRefObj = new StoredProcRef();
        storedProcRefObj.setSP_Age(28);
        storedProcRefObj.setSP_Birthday(date);
        storedProcRefObj.setSP_Gender("M");
        storedProcRefObj.setSP_FirstName("Joe");
        storedProcRefObj.setSP_LastName("Higashi");
        storedProcRefList.add(storedProcRefObj);

        List<Map<String, ?>> referenceValue = new ArrayList<>();
        referenceValue = service.getDiscountvalue(storedProcRefList.get(0));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-13 12:42:57

您似乎对输入和输出参数感到困惑。

由于过程不声明OUT参数,结果将被映射到不同的对象。

我将用下面的新课来解释这个问题。

代码语言:javascript
复制
public class StoredProcOutput {
  private String Output;
  // getter/setter
}

由于过程是用五个参数声明的,所以call语句中应该有五个参数,而不是六个参数。即

代码语言:javascript
复制
<select id="callStoredProcedure" statementType="CALLABLE"
    resultMap="outputResultMap">
  {call MY_PROC(
  #{SP_FirstName,mode=IN,jdbcType=NVARCHAR},
  #{SP_LastName,mode=IN,jdbcType=NVARCHAR},
  #{SP_Gender,mode=IN,jdbcType=NVARCHAR},
  #{SP_Birthday,mode=IN,jdbcType=DATE},
  #{SP_Age,mode=IN,jdbcType=BIGINT}
  )}
</select>

结果映射用于映射查询的结果(在您的过程中有一个查询,对吗?)

因为您只想要DiscountValue,所以它很简单。

代码语言:javascript
复制
<resultMap id="outputResultMap" type="test.StoredProcOutput">
  <result property="Output" column="DiscountValue" />
</resultMap>

映射器方法应该如下所示:

代码语言:javascript
复制
List<StoredProcOutput> callStoredProcedure(StoredProcInput inParam);

我已经更新了演示项目以及。

https://github.com/harawata/mybatis-issues/tree/master/so-58802623

如果您需要进一步的帮助,我可能会有一段时间可以聊天(不过,我不知道聊天是如何进行的)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58835141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档