包结构

mysql 多对多拆分成 一对多(学生,选修课,成绩)

数据库表结构

mysql 多对多拆分成 一对多(学生,选修课,成绩)mysql 多对多拆分成 一对多(学生,选修课,成绩)mysql 多对多拆分成 一对多(学生,选修课,成绩)mysql 多对多拆分成 一对多(学生,选修课,成绩)

 

entity

mysql 多对多拆分成 一对多(学生,选修课,成绩)mysql 多对多拆分成 一对多(学生,选修课,成绩)mysql 多对多拆分成 一对多(学生,选修课,成绩)

 

 1 package com.jdbc.dao;
 2 
 3 import java.sql.SQLException;
 4 import java.util.List;
 5 
 6 import org.apache.commons.dbutils.QueryRunner;
 7 import org.apache.commons.dbutils.handlers.BeanHandler;
 8 import org.apache.commons.dbutils.handlers.BeanListHandler;
 9 
10 import com.chinasofti.jdbc.TxQueryRunner;
11 import com.jdbc.entity.Course;
12 import com.jdbc.entity.Student;
13 
14 public class StudentDao {
15     /*
16      * 获取一个学生的所有选修课
17      */
18 
19     public Student getStuByCourse(int sid) throws SQLException {
20         QueryRunner qr =new TxQueryRunner();
21         String sql = "select * from student where sid = ?";
22         Student student = qr.query(sql, new BeanHandler<Student>(Student.class),sid);
23          sql ="select * from course c,score sc where sc.cid =c.cid and sid = ?";
24          List<Course> courses = qr.query(sql, new BeanListHandler<Course>(Course.class),student.getSid());
25          student.setCourses(courses);
26         return student;
27          
28     }
29     /*
30      * 获取所有学生
31      */
32     public List<Student> getStusByCourses() throws SQLException{
33         QueryRunner qr =new TxQueryRunner();
34         String sql = "select * from student";
35         List<Student> students = qr.query(sql, new BeanListHandler<Student>(Student.class));
36         for(Student stu:students) {
37              sql ="select * from course c,score sc where sc.cid =c.cid and sid = ?";
38              List<Course> courses = qr.query(sql, new BeanListHandler<Course>(Course.class),stu.getSid());
39              stu.setCourses(courses);
40         }
41         return students;
42         
43     }
44 }
StudentDao

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
  • 2021-05-03
猜你喜欢
  • 2021-10-30
  • 2021-12-19
  • 2021-11-06
  • 2021-07-17
  • 2021-07-21
  • 2022-12-23
相关资源
相似解决方案