【问题标题】:sql server 2008 stored procedure multiple selects in mybatisMybatis中的sql server 2008存储过程多选
【发布时间】:2016-01-12 05:13:50
【问题描述】:

mybatis中的存储过程多选有问题。

stored_procedure.sql 中的两个 select 语句

USE cellar;
GO
alter PROCEDURE findAll_sp
AS
SELECT * FROM wine ORDER BY name;  //The results of this select statement are stored in the list.
SELECT * FROM wine where id=5;  //The results of this select statement are not stored in the list.
GO

stored_procedure.sql

stored_procedure.sql 中只有一个 select 语句的结果(第一个 select 语句)保存在 WineDAO.java 的列表中。但我想在所有两个 select 语句的结果列表中。 我该如何解决这个问题?

以下是相关源码。

<?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="org.coenraets.cellar.cellar-mapper"> 
    <select id="findAll" resultType="Wine"> 
        {call findALL_sp}
    </select>
</mapper>

mapper.xml

public class WineDAO {

    private static SqlSessionFactory ssf;

    static{ 
        try{
            Reader reader= org.apache.ibatis.io.Resources.getResourceAsReader("Config.xml");
            ssf=new SqlSessionFactoryBuilder().build(reader);
        }catch(Exception ex){ex.getMessage();}
    }

    public List<Wine> findAll() {
        List<Wine> list = new ArrayList<Wine>();
        list = ssf.openSession().selectList("findAll");

        for(int i=0; i<list.size(); i++)
        {
            System.out.println(list.get(i).getName());
        }

        return list;
    }
}

WineDAO.java

谢谢。

【问题讨论】:

  • 因为它是一个存储过程,你正在执行两个语句,你的过程的结果将是最后一个,你不能同时检索两个。在您的查询中使用 UNION 将不同查询中的两个语句(不同的 mybatis 语句)分开。类似SELECT * FROM wine ORDER BY name UNION SELECT * FROM wine where id=5
  • "你不能同时检索两者" - 不正确。查找火星
  • 感谢您的回答,豪尔赫。许多存储过程在我的项目中已经有一个多选语句。所以mybatis没有别的办法了,我要放弃mybatis了。再次感谢。
  • 火星?什么是火星?

标签: java sql-server-2008 stored-procedures mybatis mssql-jdbc


【解决方案1】:

我解决了这个问题

<?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="org.coenraets.cellar.cellar-mapper"> 

     <resultMap id="WineAll" type="Wine">    
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="grapes" column="grapes"/>
        <result property="country" column="country"/>
        <result property="region" column="region"/>
        <result property="year" column="year"/>
        <result property="picture" column="picture"/>
        <result property="description" column="description"/>   
     </resultMap>

     <resultMap id="WineFive" type="Wine">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="grapes" column="grapes"/>
        <result property="country" column="country"/>
        <result property="region" column="region"/>
        <result property="year" column="year"/>
        <result property="picture" column="picture"/>
        <result property="description" column="description"/>   
     </resultMap>


     <select id="findAll" resultMap="WineAll, WineFive"> 
          {call findALL_sp}
     </select>

</mapper>

mapper.xml

public class WineDAO {

    private static SqlSessionFactory ssf;

    static{ 
        try{
            Reader reader= org.apache.ibatis.io.Resources.getResourceAsReader("Config.xml");
            ssf=new SqlSessionFactoryBuilder().build(reader);
        }catch(Exception ex){ex.getMessage();}
    }

    public List<Wine> findAll() {

        List<List<Wine>>list = new ArrayList<List<Wine>>();

        list = ssf.openSession().selectList("findAll");

        for(int i=0; i<list.get(0).size(); i++)
        {
            System.out.println(list.get(0).get(i).getName());
        }

        return list.get(0);
    }
}

WineDAO.java

list.get(0) -> SELECT * FROM wine ORDER BY name;

list.get(1) -> SELECT * FROM wine WHERE id=5;

public class Wine {

    private int id;

    private String name;

    private String grapes;

    private String country;

    private String region;

    private String year;

    private String picture;

    private String description;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGrapes() {
        return grapes;
    }

    public void setGrapes(String grapes) {
        this.grapes = grapes;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

Wine.java

我通过使用结果图解决了这个问题。谢谢你:)

【讨论】:

    猜你喜欢
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    相关资源
    最近更新 更多