2 qq 27040173 qq_27040173 于 2016.04.04 16:39 提问

MyBatis使用select关联 一对多表的时候?????????????

SchoolMapper.xml 关联关系是一个学校里面有很多学生

student 表的属性如下:
图片说明

school表如下:
图片说明

school.xml如下:

图片说明

public class School {

//建立一对多的关系
private List<Student> slist;


public List<Student> getSlist() {
    return slist;
}

public void setSlist(List<Student> slist) {
    this.slist = slist;
}




private Integer sId;

private String sName;

private String sAddress;

public Integer getsId() {
    return sId;
}

public void setsId(Integer sId) {
    this.sId = sId;
}

public String getsName() {
    return sName;
}

public void setsName(String sName) {
    this.sName = sName == null ? null : sName.trim();
}

public String getsAddress() {
    return sAddress;
}

public void setsAddress(String sAddress) {
    this.sAddress = sAddress == null ? null : sAddress.trim();
}

public class Student {
//建立多多对一的关系

private School school;



public School getSchool() {
    return school;
}

public void setSchool(School school) {
    this.school = school;
}

private Integer stId;

private String stClass;

private String stName;

private String stDream;

private Integer stS;

public Integer getStId() {
    return stId;
}

public void setStId(Integer stId) {
    this.stId = stId;
}

public String getStClass() {
    return stClass;
}

public void setStClass(String stClass) {
    this.stClass = stClass == null ? null : stClass.trim();
}

public String getStName() {
    return stName;
}

public void setStName(String stName) {
    this.stName = stName == null ? null : stName.trim();
}

public String getStDream() {
    return stDream;
}

public void setStDream(String stDream) {
    this.stDream = stDream == null ? null : stDream.trim();
}

public Integer getStS() {
    return stS;
}

public void setStS(Integer stS) {
    this.stS = stS;
}



test.java  结果list就是null  


public static void main(String[] args) {
    // TODO Auto-generated method stub
    StuService ss=new StuService();
    ss.selectSchool();
}

public void selectSchool(){



      SqlSession ss=null;


        try {
            ss=sf.openSession();

                                                         //selectByPrimaryKey
        School sc =ss.selectOne("com.wuye.dao.SchoolMapper.selectSchool", 1);


        System.out.println("学校的信息:"+sc.getsId()+" "+sc.getsName()+" "+sc.getsAddress());

    List<Student> lis=sc.getSlist();

    for(Student st:lis){
        System.out.println("学生信息学号:"+st.getStId()+"名字:"+st.getStName()+"所在班级: "+st.getStClass()+"梦想:"+st.getStDream()+"所在学校编号:=="+st.getStS());

    }

        } catch (Exception e) {
        // TODO: handle exception
          e.printStackTrace();
        }









}

2个回答

wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.04.04 17:54

是这样的啊,定义自己定义resultMap指定类的属性和查询列的映射关系,集合列指定select语句。
你这段代码怎么了,有报错么?

qq_27040173
qq_27040173 你好 我的代码 查不出来List<student> lis里面的学生信息 显示空指针
一年多之前 回复
qq_27040173
qq_27040173   2016.04.04 16:42

## # 上面肯写的不清楚:

SchoolMapper.xml 关联关系是一个学校里面有很多学生






 <select id="selectSchool" resultMap="BaseResultMap3"  parameterType="java.lang.Integer">

select * from school sc where sc.s_id=#{sId,jdbcType=INTEGER}





</collection> 


select st_id stId, st_class stClass, st_name stName,st_dream stDream

from student s where s.st_s=#{sId,jdbcType=INTEGER}

StudentMapper.xml

<mapper namespace="com.wuye.dao.StudentMapper" >






<association property="school" column="st_s" select="selectShoolByStu" ></association>

两个表:!![图片说明](http://img.ask.csdn.net/upload/201604/04/1459759020_969759.png)

图片说明
public class School {

//建立一对多的关系
private List<Student> slist;


public List<Student> getSlist() {
    return slist;
}

public void setSlist(List<Student> slist) {
    this.slist = slist;
}




private Integer sId;

private String sName;

private String sAddress;

public Integer getsId() {
    return sId;
}

public void setsId(Integer sId) {
    this.sId = sId;
}

public String getsName() {
    return sName;
}

public void setsName(String sName) {
    this.sName = sName == null ? null : sName.trim();
}

public String getsAddress() {
    return sAddress;
}

public void setsAddress(String sAddress) {
    this.sAddress = sAddress == null ? null : sAddress.trim();
}

public class Student {
//建立多多对一的关系

private School school;



public School getSchool() {
    return school;
}

public void setSchool(School school) {
    this.school = school;
}

private Integer stId;

private String stClass;

private String stName;

private String stDream;

private Integer stS;

public Integer getStId() {
    return stId;
}

public void setStId(Integer stId) {
    this.stId = stId;
}

public String getStClass() {
    return stClass;
}

public void setStClass(String stClass) {
    this.stClass = stClass == null ? null : stClass.trim();
}

public String getStName() {
    return stName;
}

public void setStName(String stName) {
    this.stName = stName == null ? null : stName.trim();
}

public String getStDream() {
    return stDream;
}

public void setStDream(String stDream) {
    this.stDream = stDream == null ? null : stDream.trim();
}

public Integer getStS() {
    return stS;
}

public void setStS(Integer stS) {
    this.stS = stS;
}



test.java  结果list就是null  


public static void main(String[] args) {
    // TODO Auto-generated method stub
    StuService ss=new StuService();
    ss.selectSchool();
}

public void selectSchool(){



      SqlSession ss=null;


        try {
            ss=sf.openSession();

                                                         //selectByPrimaryKey
        School sc =ss.selectOne("com.wuye.dao.SchoolMapper.selectSchool", 1);


        System.out.println("学校的信息:"+sc.getsId()+" "+sc.getsName()+" "+sc.getsAddress());

    List<Student> lis=sc.getSlist();

    for(Student st:lis){
        System.out.println("学生信息学号:"+st.getStId()+"名字:"+st.getStName()+"所在班级: "+st.getStClass()+"梦想:"+st.getStDream()+"所在学校编号:=="+st.getStS());

    }

        } catch (Exception e) {
        // TODO: handle exception
          e.printStackTrace();
        }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!