2016-01-12 158 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的解决了这个问题。谢谢:)