一对多联表查询

一:总结说明

1.需求

  根据classId查询出对应的班级信息,包括学生和老师。

 

2.结构目录

 

3.新建学生表

 1 CREATE TABLE student(
 2     s_id INT PRIMARY KEY AUTO_INCREMENT,
 3     s_name VARCHAR(20),
 4     class_id INT
 5 );
 6 INSERT INTO student(s_name,class_id) VALUES('xs1',1);
 7 INSERT INTO student(s_name,class_id) VALUES('xs2',1);
 8 INSERT INTO student(s_name,class_id) VALUES('xs3',1);
 9 INSERT INTO student(s_name,class_id) VALUES('xs4',2);
10 INSERT INTO student(s_name,class_id) VALUES('xs5',2);
11 INSERT INTO student(s_name,class_id) VALUES('xs6',2);

 

4.实体类Student.java

 1 package com.jun.bean;
 2 
 3 public class Student {
 4     //
 5     private int id;
 6     private String name;
 7     //
 8     public Student() {}
 9     public Student(int id,String name) {
10         this.id=id;
11         this.name=name;
12     }
13     public int getId() {
14         return id;
15     }
16     public void setId(int id) {
17         this.id = id;
18     }
19     public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25     @Override
26     public String toString() {
27         return "Student [id=" + id + ", name=" + name + "]";
28     }
29     
30 }

 

5.修改Classes.java类

  因为classes除了老师,现在需要添加一个属性学生,这个学生的属性是List。

  需要修改get set方法,修改构造函数,修改tostring方法。

 1 package com.jun.bean;
 2 
 3 import java.util.List;
 4 
 5 public class Classes {
 6     //
 7     private int id;
 8     private String name;
 9     private Teacher teacher;
10     private List list;     //新添加的属性
11     //
12     public Classes() {}
13     public Classes(int id,String name,Teacher teacher,List list) {
14         this.id=id;
15         this.name=name;
16         this.teacher=teacher;
17         this.list=list;
18     }
19     public int getId() {
20         return id;
21     }
22     public void setId(int id) {
23         this.id = id;
24     }
25     public String getName() {
26         return name;
27     }
28     public void setName(String name) {
29         this.name = name;
30     }
31     public Teacher getTeacher() {
32         return teacher;
33     }
34     public void setTeacher(Teacher teacher) {
35         this.teacher = teacher;
36     }
37     public List getList() {
38         return list;
39     }
40     public void setList(List list) {
41         this.list = list;
42     }
43     @Override
44     public String toString() {
45         return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher + ", list=" + list + "]";
46     }
47         
48 }

 

6.Configuration.xml

 1 xml version="1.0" encoding="UTF-8" ?>
 2 DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
 3 
 4 <configuration>
 5   <environments default="development">
 6     <environment id="development">
 7       <transactionManager type="JDBC">
 8         <property name="" value=""/>
 9       transactionManager>
10       <dataSource type="UNPOOLED">
11         <property name="driver" value="com.mysql.jdbc.Driver"/>
12         <property name="url" value="jdbc:mysql://127.0.0.1:3308/mybatis"/>
13         <property name="username" value="root"/>
14         <property name="password" value="123456"/>
15       dataSource>
16     environment>
17   environments>
18 
19    <mappers>
20       <mapper resource="com/jun/sql/config/class.xml"/>
21    mappers>
22 
23 configuration>

 

二:第一种方式

1.映射文件class.xml

 1 xml version="1.0" encoding="UTF-8"?>
 2 DOCTYPE mapper
 3     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="one2many">
 6     
10     <select id="selectAll" parameterType="int" resultMap="getClasses">
11         Select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}
12     select>
13     <resultMap type="com.jun.bean.Classes" id="getClasses">
14         <id property="id" column="c_id"/>
15         <result property="name" column="c_name"/>
16         <association property="teacher" javaType="com.jun.bean.Teacher">
17             <id property="id" column="t_id"/>
18             <result property="name" column="t_name"/>
19         association>
20         <collection property="list" ofType="com.jun.bean.Student">
21             <id property="id" column="s_id"/>
22             <result property="name" column="s_name"/>
23         collection>
24     resultMap>
25 mapper>

 

2.测试类

 1 package com.jun.main;
 2 
 3 import java.io.IOException;
 4 import java.io.Reader;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 import org.junit.Test;
11 import com.jun.bean.Classes;
12 public class MainTest {
13     /**
14      * 方式一
15      * @throws Exception
16      */
17     @Test
18     public void test1() throws Exception {
19         Reader reader=Resources.getResourceAsReader("com/jun/config/Configuration.xml");
20         SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
21         SqlSession sqlSession=sqlSessionFactory.openSession(true);                //true后是自动提交
22         String statement="one2many.selectAll";
23         Classes classes=sqlSession.selectOne(statement, 1);
24         System.out.println(classes);
25         sqlSession.close();
26     }
27     
28 }

 

3.测试结果

  

 

三:方式二

1.映射文件

 1 xml version="1.0" encoding="UTF-8"?>
 2 DOCTYPE mapper
 3     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="one2many">
 6     
10     <select id="selectAll" parameterType="int" resultMap="getClasses">
11         Select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}
12     select>
13     <resultMap type="com.jun.bean.Classes" id="getClasses">
14         <id property="id" column="c_id"/>
15         <result property="name" column="c_name"/>
16         <association property="teacher" javaType="com.jun.bean.Teacher">
17             <id property="id" column="t_id"/>
18             <result property="name" column="t_name"/>
19         association>
20         <collection property="list" ofType="com.jun.bean.Student">
21             <id property="id" column="s_id"/>
22             <result property="name" column="s_name"/>
23         collection>
24     resultMap>
25     
26     
33      <select id="selectClass" resultMap="getClasses2">
34          select * from class where c_id=#{id}
35      select>
36      <select id="getTeacher" resultType="com.jun.bean.Teacher">      
37          select t_id id,t_name name from teacher where t_id=#{id}
38      select>
39      <select id="getStudent" resultType="com.jun.bean.Student">      
40          select s_id id,s_name name from student where class_id=#{id} 
41      select>
42      <resultMap type="com.jun.bean.Classes" id="getClasses2">
43          <id property="id" column="c_id"/>
44         <result property="name" column="c_name"/>
45         <association property="teacher" column="teacher_id" select="getTeacher">association>
46         <collection property="list" column="c_id" select="getStudent">collection>
47      resultMap>
48 mapper>

 

2.测试类

 1 package com.jun.main;
 2 
 3 import java.io.IOException;
 4 import java.io.Reader;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 import org.junit.Test;
11 import com.jun.bean.Classes;
12 public class MainTest {
13     /**
14      * 方式一
15      * @throws Exception
16      */
17     @Test
18     public void test1() throws Exception {
19         Reader reader=Resources.getResourceAsReader("com/jun/config/Configuration.xml");
20         SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
21         SqlSession sqlSession=sqlSessionFactory.openSession(true);                //true后是自动提交
22         String statement="one2many.selectAll";
23         Classes classes=sqlSession.selectOne(statement, 1);
24         System.out.println(classes);
25         sqlSession.close();
26     }
27     /**
28      * 方式二
29      * @throws Exception
30      */
31     @Test
32     public void test2() throws Exception {
33         Reader reader=Resources.getResourceAsReader("com/jun/config/Configuration.xml");
34         SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
35         SqlSession sqlSession=sqlSessionFactory.openSession(true);                //true后是自动提交
36         String statement="one2many.selectClass";
37         Classes classes=sqlSession.selectOne(statement, 1);
38         System.out.println(classes);
39         sqlSession.close();
40     }
41     
42 }

 

3.结果

  一对多联表查询_第1张图片

 

  

 

你可能感兴趣的:(java,数据库)