2016-01-12 198 views
2

我有一個關於存儲過程多選的問題在mybatis中。在stored_procedure.sqlsql server 2008存儲過程多選mybatis

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

只有一個SELECT語句的結果

兩個SELECT語句(先選擇一句)在stored_procedure.sql保存在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

謝謝。

+0

因爲它是一個存儲過程,所以你正在執行兩個語句,你的過程的結果將是最後一個,你不能同時檢索這兩個語句。在不同的查詢(不同的mybatis語句)中分隔兩個語句的查詢使用UNION。類似於'SELECT * FROM wine ORDER BY name UNION SELECT * FROM wine where id = 5' –

+0

「你不能同時檢索」 - 不正確。查詢MARS –

+0

謝謝你的回答,Jorge。許多存儲過程在我的項目中都有一個多選擇句子。所以在mybatis中沒有別的辦法,我要放棄mybatis。再次感謝。 –

回答

0

我解決這個問題

<?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酒ORDER BY名稱;

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

我使用resultMap的解決了這個問題。謝謝:)