老师为什么我这里查询不到课程信息呢?
<?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="com.mason.entity.Student"> <select id="findStu" resultMap="forStudent"> select * from student <if test="id !=null"> where id=#{id} </if> </select> <resultMap id="forStudent" type="com.mason.entity.Student"> <id property="id" column="id"></id> <collection property="courses" column="id" ofType="com.mason.entity.Course" select="getCourse"></collection> </resultMap> <select id="getCourse" resultType="com.mason.entity.Course"> select * from course where major_name=#{major} </select> <sql id="addStu_fields"> reg_no,name,sex,age,grade,major </sql> <insert id="addStu" useGeneratedKeys="true" keyProperty="id"> insert student(<include refid="addStu_fields"></include>) values (#{reg_no},#{name},#{sex},#{age},#{grade},#{major}) </insert> <update id="updateStu"> update student <set> <if test="reg_no!=null">reg_no=#{reg_no}, </if> <if test="name!=null">name=#{name}, </if> <if test="sex!=null">sex=#{sex}, </if> <if test="age!=null">age=#{age}, </if> <if test="grade!=null">grade=#{grade}, </if> <if test="major!=null">major=#{major}, </if> </set> where id=#{id} </update> </mapper>
package com.mason.DAO; import com.mason.entity.Student; import com.mason.utils.SqlSessionFactoryUtils; import org.apache.ibatis.session.SqlSession; import java.util.List; public class StudentDAO { private List<Student> list; private SqlSession sqlSession; private Student student; private SqlSession getSqlSession() { sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); return sqlSession; } //查询所有学生 public List<Student> findAll() { try { list = getSqlSession().selectList("findStu"); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } return list; } //添加学生 public Student addStu(Student student) { System.out.println("刚传进来的Student:"+student); try { getSqlSession().insert("addStu", student); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } System.out.println("准备传出去的Student:"+student); return student; } public Student findById(int id) { try { student = getSqlSession().selectOne("findStu", new Student(id)); } catch (Exception e) { e.printStackTrace(); }finally { sqlSession.close(); } return student; } public int updateStu(Student student) { try { int update = getSqlSession().update("updateStu",student); sqlSession.commit(); return update; } catch (Exception e) { e.printStackTrace(); }finally { sqlSession.close(); } return 0; } }
package com.mason.servlet; import com.mason.DAO.StudentDAO; import com.mason.entity.Student; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/detail") public class DetailServlet extends HttpServlet { private StudentDAO studentDAO=new StudentDAO(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); System.out.println("id:"+id); //调用DAO查询单个学生信息 Student student=studentDAO.findById(Integer.parseInt(id)); System.out.println("查询到的学生信息:"+student); //返回数据到页面 request.setAttribute("student", student); request.getRequestDispatcher("/detail.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
0
收起
正在回答
7回答
这个就是Student中专业属性和数据库自动列表中的专业对应起来呦,因为咱们在查询课程的时候用了专业这一列去查询对应专业所有的Course(这里同学可以理解为,作为getCourse这个查询的参数),如果不单独把这一列写出来,它会以为,major对应的是courses,
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
MasonM
2019-06-19 18:44:36
package com.mason.entity; public class Course { private Integer id; private String courseid; private String major_name; private String courname; private Float courtime; private String courteacher; public Integer getId() { return id; } public void setId(Integer 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 String getCourname() { return courname; } public void setCourname(String courname) { this.courname = courname; } public Float getCourtime() { return courtime; } public void setCourtime(Float courtime) { this.courtime = courtime; } public String getCourteacher() { return courteacher; } public void setCourteacher(String courteacher) { this.courteacher = courteacher; } @Override public String toString() { return "Course{" + "id=" + id + ", courseid='" + courseid + '\'' + ", major_name='" + major_name + '\'' + ", courname='" + courname + '\'' + ", courtime=" + courtime + ", courteacher='" + courteacher + '\'' + '}'; } }
代码全部贴出来了
查询到的course集合为空
请问这是为什么呢?
老师,我觉得最后一节课讲解的不太好,有些地方都不知道设置了有啥用,能否详细一点讲解
<collection property="courses" column="id" ofType="com.mason.entity.Course" select="getCourse"></collection>
我对这里的设置都不太懂,这里每一项的属性设置到底是什么意思呢?
MasonM
2019-06-19 18:39:10
页面代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>学生管理系统</title> <link rel="stylesheet" href="lib/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <script src="lib/2.2.4/jquery-1.12.4.min.js"></script> <script src="lib/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="page-header"> <h1>学生信息后台管理系统 <small>修改学生信息</small> </h1> </div> </div> <form class="form-horizontal" action="${pageContext.request.contextPath}/updateStu"> <input type="hidden" name="id" value="${student.id}"> <div class="form-group"> <label for="reg_no" class="col-sm-2 control-label">学号</label> <div class="col-sm-10"> <input type="text" class="form-control" name="reg_no" value="${student.reg_no}"> </div> </div> <div class="form-group"> <label for="name" class="col-sm-2 control-label">姓名</label> <div class="col-sm-10"> <input type="text" class="form-control" name="name" value="${student.name}"> </div> </div> <div class="form-group"> <label for="sex" class="col-sm-2 control-label">性别</label> <div class="col-sm-10"> <input type="text" class="form-control" name="sex" value="${student.sex}"> </div> </div> <div class="form-group"> <label for="age" class="col-sm-2 control-label">年龄</label> <div class="col-sm-10"> <input type="text" class="form-control" name="age" value="${student.age}"> </div> </div> <div class="form-group"> <label for="grade" class="col-sm-2 control-label">年级</label> <div class="col-sm-10"> <input type="text" class="form-control" name="grade" value="${student.grade}"> </div> </div> <div class="form-group"> <label for="major" class="col-sm-2 control-label">专业</label> <div class="col-sm-10"> <input type="text" class="form-control" name="major" value="${student.major}"> </div> </div> <button type="submit" class="btn btn-primary" style="float: right">修改</button> </form> <div class="row"> <table class="table table-striped"> <tr> <th>课程号</th> <th>专业名称</th> <th>课程名称</th> <th>讲授老师</th> <th>课时</th> </tr> <c:forEach var="course" items="${student.courses}"> <tr> <td>${course.courseid}</td> <td>${course.major_name}</td> <td>${course.courename}</td> <td>${course.courteacher}</td> <td>${course.courtime}</td> </tr> </c:forEach> </table> </div> </div> </div> </body> </html>
package com.mason.entity; import java.util.List; public class Student { private Integer id; private Integer reg_no; private String name; private String sex; private Integer age; private String grade; private String major; private List<Course> courses; @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 List<Course> getCourses() { return courses; } public void setCourses(List<Course> courses) { this.courses = courses; } public Student() { } public Student(Integer id) { this.id = id; } public Student(Integer reg_no, String name, String sex, Integer 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 Student(Integer id, Integer reg_no, String name, String sex, Integer 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 Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getReg_no() { return reg_no; } public void setReg_no(Integer 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 Integer getAge() { return age; } public void setAge(Integer 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; } }
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星