sql server 2008存储过程多次在mybatis中选择

时间:2016-01-12 05:13:51

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

我在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语句的结果(第一个选择句子)保存在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

谢谢。

1 个答案:

答案 0 :(得分: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) - &gt; SELECT * FROM wine ORDER BY name;

list.get(1) - &gt; 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解决了这个问题。谢谢:))