sql mybatis中server 2008存储过程多选
sql server 2008 stored procedure multiple selects in mybatis
mybatis 中的多个 selects 存储过程有问题。
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
谢谢。
我解决了这个问题
<?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 * 来自 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
我用结果图解决了这个问题。谢谢:)
mybatis 中的多个 selects 存储过程有问题。
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
谢谢。
我解决了这个问题
<?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 * 来自 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
我用结果图解决了这个问题。谢谢:)