简体   繁体   English

MyBatis Springs Oracle存储过程

[英]MyBatis Springs Oracle Stored Procedure

I have been working on Spring application where I am working with MyBatis: 我一直在使用MyBatis的Spring应用程序上工作:

Consider I am having a Oracle Stored Procedure: 考虑我正在使用Oracle存储过程:

CREATE OR REPLACE PROCEDURE getDBUSERByUserId(
     p_userid IN DBUSER.USER_ID INTEGER,
     o_username OUT DBUSER.USERNAME VARCHAR,
     o_createdby OUT  DBUSER.CREATED_BY VARCHAR, 
     o_date OUT DBUSER.CREATED_DATE DATE)
IS 
BEGIN
   SELECT USERNAME , CREATED_BY, CREATED_DATE
   INTO o_username, o_createdby,  o_date
   from  DBUSER WHERE USER_ID = p_userid;
END

For the above SP I have written the following mapper.xml: 对于以上SP,我编写了以下mapper.xml:

<mapper namespace="UserDetails">
  <resultMap id="getDbUserDetails" type="map">
    <result property="p_userid" column="INTEGER" />
    <result property="o_username" column="VARCHAR" />
    <result property="o_createdby" column="VARCHAR" />
    <result property="o_date" column="VARCHAR" />
  </resultMap>

  <select id="getDbUserByUserId" resultMap="getDbUserDetails" parameterType="map" statementType="CALLABLE">
        { call getDBUSERByUserId(
           #{p_userid,jdbcType=INTEGER,mode=IN},
           #{o_username,jdbcType=VARCHAR,mode=OUT},
           #{o_createdby,jdbcType=VARCHAR,mode=OUT},
           #{o_date,jdbcType=VARCHAR,mode=OUT})
         }
  </select>
</mapper>

The corresponding Java Code for calling this SP: 用于调用此SP的相应Java代码:

public Map<String, Object> getUserDetails() {

  Map<String, Object> parametersMap = new HashMap<String, Object>();
  parametersMap.put(p_userid,"XYZ-123");
  parametersMap.put(o_username,"Tom74");
  parametersMap.put(o_createdby,"Maverick");
  parametersMap.put(o_date,new Date());

  getSqlSession().selectList("UserDetails.getDbUserByUserId", parametersMap);

  System.out.println(parametersMap);

  return parametersMap;
}

At the time of execution it is throwing an exception: 在执行时,它引发了一个异常:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.lang.String
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:364)
    at com.sun.proxy.$Proxy7.selectList(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:194)
    at com.bt.cmps.kci_services.dao.NotificationDaoImplV10.getKcis(NotificationDaoImplV10.java:252)
    at com.bt.kci_services.services.NotificationServiceImplV10.getKcis(NotificationServiceImplV10.java:208)
    at com.bt.kci_services.services.NotificationProcessorImplV10.getKcis(NotificationProcessorImplV10.java:106)
    at com.bt.kci_services.services.test.NotificationProcessorImplV10Test.testGetKcisForOnlyCustId(NotificationProcessorImplV10Test.java:103)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.runManaged(AbstractJUnit38SpringContextTests.java:334)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.access$0(AbstractJUnit38SpringContextTests.java:326)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests$1.run(AbstractJUnit38SpringContextTests.java:216)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.runTest(AbstractJUnit38SpringContextTests.java:296)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.runTestTimed(AbstractJUnit38SpringContextTests.java:253)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.runBare(AbstractJUnit38SpringContextTests.java:213)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:131)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.lang.String
    at org.apache.ibatis.builder.MapperBuilderAssistant.setStatementResultMap(MapperBuilderAssistant.java:373)
    at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:311)
    at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:109)
    at org.apache.ibatis.session.Configuration.buildAllStatements(Configuration.java:698)
    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:629)
    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:624)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:103)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:354)
Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain value for java.lang.String
    at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:791)
    at org.apache.ibatis.session.Configuration.getResultMap(Configuration.java:550)
    at org.apache.ibatis.builder.MapperBuilderAssistant.setStatementResultMap(MapperBuilderAssistant.java:371)

I googled a lot about this issues but nothing seems to help. 我在这个问题上搜索了很多,但似乎无济于事。 I am new with MyBatis so any help would be highly appreciated. 我是MyBatis的新手,因此将非常感谢您的帮助。 I am using mybatis-3.2.3. 我正在使用mybatis-3.2.3。

Thanks in advance..!!! 提前致谢..!!!

You should not spesify out params in your hashMap. 您不应该在hashMap中添加参数。 You should write something like 你应该写类似

.selectOne("test.testOutput", params); .selectOne(“ test.testOutput”,params);

public Map<String, Object> getUserDetails() {

  Map<String, Object> parametersMap = new HashMap<String, Object>();
  parametersMap.put(p_userid,"XYZ-123");

  getSqlSession().selectOne("UserDetails.getDbUserByUserId", parametersMap);

  System.out.println(parametersMap);

  return parametersMap;
}

I disagree with @Michael Piankov on the source of the issue: I have just checked that there is no difference whether or not the parameterMap contains key/values for OUT parameters. 对于问题的来源,我不同意@Michael Piankov :我刚刚检查了parameterMap是否包含OUT参数的键/值没有区别。

I have encountered few problems trying to execute your code. 我在尝试执行您的代码时遇到了一些问题。 Could not reproduce the same exception, few tips anyway: 无论如何,无法重现相同的异常:

  • You don't need at all the result map since all the mapping is done in the procedure call. 您完全不需要结果映射,因为所有映射都在过程调用中完成。

  • Expected value for parameter p_userid must be convertible to an integer ,"XYZ-123" is not. 参数p_userid的期望值必须可以转换整数 ,而“ XYZ-123”则不能。

  • OUT parameter o_date is declared as DATE but mapped as jdbcType=VARCHAR , it could have created issue with other types. OUT参数o_date声明为DATE,但映射为jdbcType = VARCHAR ,可能与其他类型有关。

  • Finally I have the following working: 最后,我有以下工作:

-Stored Procedure: -存储过程:

CREATE OR REPLACE PROCEDURE getDBUSERByUserId(
    p_userid IN INTEGER,
    o_username OUT VARCHAR2,
    o_createdby OUT VARCHAR2,
    o_date OUT DATE)
IS
BEGIN
  SELECT 'USERNAME' , 'CREATED_BY', CURRENT_DATE
  INTO o_username, o_createdby, o_date
  FROM dual ;
END;

-Mapper: -地图:

<select id="getDbUserByUserId" resultMap="getDbUserDetails" parameterType="map" statementType="CALLABLE">
        { call getDBUSERByUserId(
           #{p_userid,jdbcType=INTEGER,mode=IN},
           #{o_username,jdbcType=VARCHAR,mode=OUT},
           #{o_createdby,jdbcType=VARCHAR,mode=OUT},
           #{o_date,jdbcType=VARCHAR,mode=OUT})
         }
</select>

-Java: -Java:

SqlSession session = sessionFactory.openSession();

Map<String, Object> parametersMap = new HashMap<String, Object>();
parametersMap.put("p_userid",123);
parametersMap.put("o_username",null);
parametersMap.put("o_createdby",null);
parametersMap.put("o_date",null);

session.selectOne("getDbUserByUserId", parametersMap);
System.out.println(parametersMap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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