MyBatis:Sybase 存储过程返回零行

发布于 2024-09-30 10:13:10 字数 3145 浏览 7 评论 0原文

我从这里复制了文本: http://code.google.com/p/mybatis/issues/detail ?id=164,但我们遇到了同样的问题。

MyBatis 版本 3.0.1

我使用 MyBatis 3 作为 java 应用程序和 sybase 数据库之间的 OR 映射。 从数据库中查询数据的sql是一个存储过程,对于简单的过程来说是可以的,但是如果存储过程中声明并使用了内部变量,则似乎无法正常工作,查询结果为null,而有抛出异常也不例外。

下面是示例代码,我也作为附件附上。 JavaBean:


    public class Test {

    private String input1;
    private String input2;

    public String getInput1() {
        return input1;
    }

    public void setInput1(String input1) {
        this.input1 = input1;
    }

    public String getInput2() {
        return input2;
    }

    public void setInput2(String input2) {
        this.input2 = input2;
    }
    }

s​​qlMap:


    <mapper namespace="cargoStatus_shipment">
    <resultMap id="testMap"     type="com.icil.esolution.cargoStatus.AS.model.Test">
    <result column="result1" jdbcType="VARCHAR" property="input1" />
    <result column="result2" jdbcType="VARCHAR" property="input2" />
    </resultMap>

    <select id="getValidData" statementType="CALLABLE"     resultMap="testMap"     parameterType="String">
     {call tempdb..testSP #{in}}
    </select> 

    </mapper>

存储过程:


    use tempdb
    go
    drop proc testSP
    go
    create proc testSP
      @in varchar(10)

    as
     declare @var char(3)
     select @var="XXX"
     select result1= '1', result2=@in
    go
    grant exec on testSP  to public
    go

Java代码:


public class TestSP {


  private static SqlSessionFactory createSqlMapper() throws IOException {
        String resource = "resources/sqlMapConfig.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        return new SqlSessionFactoryBuilder().build(reader,"development");
  }


  public static void main(String[] args) {

    SqlSession session=null;
    try {
          session = createSqlMapper().openSession(ExecutorType.SIMPLE, true); //autocommit = true

    } catch (Exception e) {
       e.printStackTrace();
       System.out.println("Error in open Session. Cause: " + e);
       System.exit(1);
    }


    List result = (List) session.selectList("getValidData", "mydata"); 

    System.out.println("Result = "+result);
    System.out.println(result.get(2).getInput2());

  }

}

通常结果应该是:

DEBUG PreparedStatement - ==>  Executing: {call tempdb..testSP ?} 
DEBUG PreparedStatement - ==> Parameters: mydata(String)
DEBUG ResultSet - 

但实际上,没有得到结果,也没有例外:

DEBUG PreparedStatement - ==>  Executing: {call tempdb..testSP ?} 
DEBUG PreparedStatement - ==> Parameters: mydata(String)

经过计数器测试,如果我从sp中删除内部变量@var,那么就可以了。

    use tempdb
    go
    drop proc testSP
    go
    create proc testSP
      @in varchar(10)

    as
     select result1= '1', result2=@in
    go
    grant exec on testSP  to public
    go

您能否检查一下问题是什么以及我该怎么做才能确保我可以调用这种存储过程?

I have copied the text from here:
http://code.google.com/p/mybatis/issues/detail?id=164, but we faced the same issue.

MyBatis version 3.0.1

I use MyBatis 3 as the OR mapping between the java application and sybase database.
The sql used to query data from the database is a stored procedure, it is ok for simple procedure, but if there is inner variable declared and used in the stored procedure, it seems does not work correctly, the query result is null, while there is no exception throwing.

below is the example code, and i also attach as the attachment.
JavaBean:


    public class Test {

    private String input1;
    private String input2;

    public String getInput1() {
        return input1;
    }

    public void setInput1(String input1) {
        this.input1 = input1;
    }

    public String getInput2() {
        return input2;
    }

    public void setInput2(String input2) {
        this.input2 = input2;
    }
    }

sqlMap:


    <mapper namespace="cargoStatus_shipment">
    <resultMap id="testMap"     type="com.icil.esolution.cargoStatus.AS.model.Test">
    <result column="result1" jdbcType="VARCHAR" property="input1" />
    <result column="result2" jdbcType="VARCHAR" property="input2" />
    </resultMap>

    <select id="getValidData" statementType="CALLABLE"     resultMap="testMap"     parameterType="String">
     {call tempdb..testSP #{in}}
    </select> 

    </mapper>

stored procedure:


    use tempdb
    go
    drop proc testSP
    go
    create proc testSP
      @in varchar(10)

    as
     declare @var char(3)
     select @var="XXX"
     select result1= '1', result2=@in
    go
    grant exec on testSP  to public
    go

Java code:


public class TestSP {


  private static SqlSessionFactory createSqlMapper() throws IOException {
        String resource = "resources/sqlMapConfig.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        return new SqlSessionFactoryBuilder().build(reader,"development");
  }


  public static void main(String[] args) {

    SqlSession session=null;
    try {
          session = createSqlMapper().openSession(ExecutorType.SIMPLE, true); //autocommit = true

    } catch (Exception e) {
       e.printStackTrace();
       System.out.println("Error in open Session. Cause: " + e);
       System.exit(1);
    }


    List result = (List) session.selectList("getValidData", "mydata"); 

    System.out.println("Result = "+result);
    System.out.println(result.get(2).getInput2());

  }

}

normally the result should be:

DEBUG PreparedStatement - ==>  Executing: {call tempdb..testSP ?} 
DEBUG PreparedStatement - ==> Parameters: mydata(String)
DEBUG ResultSet - 

but actually, there is no result get, neither exceptions:

DEBUG PreparedStatement - ==>  Executing: {call tempdb..testSP ?} 
DEBUG PreparedStatement - ==> Parameters: mydata(String)

after counter test, if i remove the inner variable @var from the sp, then it will be ok.

    use tempdb
    go
    drop proc testSP
    go
    create proc testSP
      @in varchar(10)

    as
     select result1= '1', result2=@in
    go
    grant exec on testSP  to public
    go

Could you pls check what is the problem and what shall I do to make sure I can call this kind of stored procedure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱猫软卧 2024-10-07 10:13:10

我想修改上面帖子的一个错字。

通常结果应该是:

<代码>
DEBUG 准备语句 - ==>执行:{call tempdb..testSP ?}
DEBUG 准备语句 - ==>参数:mydata(字符串)
调试结果集 - <== 列:结果 1、结果 2
调试结果集 - <== 行:1,mydata

I woule like to revise one typo for above post.

normally the result should be:


DEBUG PreparedStatement - ==> Executing: {call tempdb..testSP ?}
DEBUG PreparedStatement - ==> Parameters: mydata(String)
DEBUG ResultSet - <== Columns: result1, result2
DEBUG ResultSet - <== Row: 1, mydata

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文