major打印为null

major打印为null

<select id="findStudent" resultMap="forStudent">
   select * from student
   <if test="id != null">
       where id = #{id}
   </if>
</select>
<resultMap id="forStudent" type="Student">
   <collection property="courses" column="major" ofType="Course" select="selectCourse"></collection>
</resultMap>
<select id="selectCourse" resultType="Course">
   select * from course WHERE major_name = #{major}
</select>


import java.util.List;

public class Student {

   private int id;
   private int reg_no;
   private String name;
   private String sex;
   private int age;
   private String grade;
   private String major;
   private List<Course> courses;

   public List<Course> getCourses() {
       return courses;
   }

   public void setCourses(List<Course> courses) {
       this.courses = courses;
   }

   public Student() {
   }

   public Student(int id) {
       this.id = id;
   }

   public Student(int id, int reg_no, String name, String sex, int age, String grade, String major) {
       this.id = id;
       this.reg_no = reg_no;
       this.name = name;
       this.sex = sex;
       this.age = age;
       this.grade = grade;
       this.major = major;
   }
   public Student(int reg_no, String name, String sex, int age, String grade, String major) {
       this.reg_no = reg_no;
       this.name = name;
       this.sex = sex;
       this.age = age;
       this.grade = grade;
       this.major = major;
   }

   public int getId() {
       return id;
   }

   @Override
   public String toString() {
       return "Student{" +
               "id=" + id +
               ", reg_no=" + reg_no +
               ", name='" + name + '\'' +
               ", sex='" + sex + '\'' +
               ", age=" + age +
               ", grade='" + grade + '\'' +
               ", major='" + major + '\'' +
               ", courses=" + courses +
               '}';
   }

   public void setId(int id) {
       this.id = id;
   }

   public int getReg_no() {
       return reg_no;
   }

   public void setReg_no(int reg_no) {
       this.reg_no = reg_no;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getSex() {
       return sex;
   }

   public void setSex(String sex) {
       this.sex = sex;
   }

   public int getAge() {
       return age;
   }

   public void setAge(int age) {
       this.age = age;
   }

   public String getGrade() {
       return grade;
   }

   public void setGrade(String grade) {
       this.grade = grade;
   }

   public String getMajor() {
       return major;
   }

   public void setMajor(String major) {
       this.major = major;
   }
}


import java.sql.Date;

public class Course {
   private int id;
   private String courseid;
   private String major_name;
   private String courname;
   private Date courtime;
   private String courteacher;

   public Course(int id, String courseid, String major_name, String courname, Date courtime, String courteacher) {
       this.id = id;
       this.courseid = courseid;
       this.major_name = major_name;
       this.courname = courname;
       this.courtime = courtime;
       this.courteacher = courteacher;
   }

   public String getCourname() {
       return courname;
   }

   public void setCourname(String courname) {
       this.courname = courname;
   }

   public Course() {
   }

   public Course(int id, String courseid, String major_name, Date courtime, String courteacher) {
       this.id = id;
       this.courseid = courseid;
       this.major_name = major_name;
       this.courtime = courtime;
       this.courteacher = courteacher;
   }

   public int getId() {
       return id;
   }

   public void setId(int id) {
       this.id = id;
   }

   public String getCourseid() {
       return courseid;
   }

   public void setCourseid(String courseid) {
       this.courseid = courseid;
   }

   public String getMajor_name() {
       return major_name;
   }

   public void setMajor_name(String major_name) {
       this.major_name = major_name;
   }

   public Date getCourtime() {
       return courtime;
   }

   public void setCourtime(Date courtime) {
       this.courtime = courtime;
   }

   public String getCourteacher() {
       return courteacher;
   }

   @Override
   public String toString() {
       return "Course{" +
               "id=" + id +
               ", courseid='" + courseid + '\'' +
               ", major_name='" + major_name + '\'' +
               ", courname='" + courname + '\'' +
               ", courtime=" + courtime +
               ", courteacher='" + courteacher + '\'' +
               '}';
   }

   public void setCourteacher(String courteacher) {
       this.courteacher = courteacher;
   }
}

打印结果是

http://img1.sycdn.imooc.com//climg/5c7939b10001e9db14250082.jpg

可以打出course,但是major为null是为什么

http://img1.sycdn.imooc.com//climg/5c793a260001d46a12930245.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
chrismorgen 2019-03-03 10:54:56

请问同学数据库中的major字段是否为空呢?建议你可以使用workbench查询查询一下数据库中的magor数据是否为空,祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师