【Mybatis】一对多实例

①创建数据库和表,数据库为mytest,表为teacher和student

 1 DROP TABLE IF EXISTS teacher;
 2 DROP TABLE IF EXISTS student;
 3 
 4 CREATE TABLE teacher(
 5     teacher_id INT PRIMARY KEY auto_increment NOT NULL,
 6     teacher_name VARCHAR(20)
 7 );
 8 
 9 CREATE TABLE student(
10     student_id INT PRIMARY KEY auto_increment NOT NULL,
11     student_name VARCHAR(20),
12     teacher_id INT
13 );
14 
15 ALTER TABLE student ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(teacher_id);
16 
17 INSERT INTO teacher(teacher_name) VALUES ("张三丰");
18 INSERT INTO teacher(teacher_name) VALUES ("王重阳");
19 INSERT INTO student(student_name, teacher_id) VALUES ("宋远桥", 1);
20 INSERT INTO student(student_name, teacher_id) VALUES ("俞莲舟", 1);
21 INSERT INTO student(student_name, teacher_id) VALUES ("俞岱岩", 1);
22 INSERT INTO student(student_name, teacher_id) VALUES ("张松溪", 1);
23 INSERT INTO student(student_name, teacher_id) VALUES ("张翠山", 1);
24 INSERT INTO student(student_name, teacher_id) VALUES ("殷梨亭", 1);
25 INSERT INTO student(student_name, teacher_id) VALUES ("莫声谷", 1);
26 
27 INSERT INTO student(student_name, teacher_id) VALUES ("丹阳子马钰", 2);
28 INSERT INTO student(student_name, teacher_id) VALUES ("长真子谭处端", 2);
29 INSERT INTO student(student_name, teacher_id) VALUES ("长生子刘处玄", 2);
30 INSERT INTO student(student_name, teacher_id) VALUES ("长春子丘处机", 2);
31 INSERT INTO student(student_name, teacher_id) VALUES ("玉阳子王处一", 2);
32 INSERT INTO student(student_name, teacher_id) VALUES ("广宁子郝大通", 2);
33 INSERT INTO student(student_name, teacher_id) VALUES ("清静散人孙不二", 2);

创建Java工程,导入相应的jar

创建配置文件conf.xml和数据库配置文件db.properties

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
 3     "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4     
 5 <configuration>
 6     <properties resource="db.properties"></properties>
 7     <environments default="development">
 8         <environment id="development">
 9             <transactionManager type="JDBC"></transactionManager>
10             <dataSource type="POOLED">
11                 <property name="driver" value="${driver}"/>
12                 <property name="url" value="${url}"/>
13                 <property name="username" value="${name}"/>
14                 <property name="password" value="${password}"/>
15             </dataSource>
16         </environment>
17     </environments>
18     
19     <mappers>
20         <mapper resource="org/mybatis/mapping/teacherMapper.xml"/>
21         <mapper resource="org/mybatis/mapping/studentMapper.xml"/>
22     </mappers>
23 </configuration>
1 driver = com.mysql.jdbc.Driver
2 url = jdbc:mysql://localhost:3306/mytest
3 name = root
4 password = root

创建实体类Teacher和Student

 

 1 package org.mybatis.domain;
 2 
 3 import java.util.List;
 4 
 5 public class Teacher {
 6 
 7     private int teacher_id;
 8     private String teacher_name;
 9     private List<Student> students;
10     
11     public int getTeacher_id() {
12         return teacher_id;
13     }
14 
15     public void setTeacher_id(int teacher_id) {
16         this.teacher_id = teacher_id;
17     }
18 
19     public String getTeacher_name() {
20         return teacher_name;
21     }
22 
23     public void setTeacher_name(String teacher_name) {
24         this.teacher_name = teacher_name;
25     }
26 
27     public List<Student> getStudents() {
28         return students;
29     }
30 
31     public void setStudents(List<Student> students) {
32         this.students = students;
33     }
34 
35     @Override
36     public String toString() {
37         return "[Teacher = teacher_id:" + teacher_id + ", teacher_name:" + teacher_name 
38                 + ", the number of students:" + students.size() + "]";
39     }
40     
41     
42 }

 

 1 package org.mybatis.domain;
 2 
 3 public class Student {
 4 
 5     private int student_id;
 6     private String student_name;
 7     private Teacher teacher;
 8     
 9     public int getStudent_id() {
10         return student_id;
11     }
12 
13     public void setStudent_id(int student_id) {
14         this.student_id = student_id;
15     }
16 
17     public String getStudent_name() {
18         return student_name;
19     }
20 
21     public void setStudent_name(String student_name) {
22         this.student_name = student_name;
23     }
24 
25     public Teacher getTeacher() {
26         return teacher;
27     }
28 
29     public void setTeacher(Teacher teacher) {
30         this.teacher = teacher;
31     }
32 
33     @Override
34     public String toString() {
35         return "[Student = student_id:" + student_id + ", student_name:" + student_name + 
36                 ", teacher:" + teacher.getTeacher_name() + "]";
37     }
38     
39     
40 }

⑤创建sql映射文件studentMapper.xml和teacherMapper.xml

注意比较两者的区别

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
 3     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4     
 5 <mapper namespace="org.mybatis.mapping.studentMapper">
 6     <select id="getStudent" resultMap="getStudentMap">
 7         select * from teacher t, student s where t.teacher_id=s.teacher_id and s.teacher_id=#{id}
 8     </select>
 9     
10     <resultMap type="org.mybatis.domain.Student" id="getStudentMap">
11         <id column="student_id" property="student_id" />
12         <result column="student_name" property="student_name" />
13         <association property="teacher" javaType="org.mybatis.domain.Teacher">
14             <id column="teacher_id" property="teacher_id"/>
15             <result column="teacher_name" property="teacher_name"/>
16         </association>
17     </resultMap>
18 </mapper>
studentMapper.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
 3     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4     
 5 <mapper namespace="org.mybatis.mapping.teacherMapper">
 6     <select id="getTeacher" resultMap="getTeacherMap">
 7         select * from teacher t, student s where t.teacher_id=s.teacher_id and t.teacher_id=#{id}
 8     </select>
 9     
10     <resultMap type="org.mybatis.domain.Teacher" id="getTeacherMap">
11         <id column="teacher_id" property="teacher_id" />
12         <result column="teacher_name" property="teacher_name" />
13         <collection property="students" ofType="org.mybatis.domain.Student">
14             <id column="student_id" property="student_id"/>
15             <result column="student_name" property="student_name"/>
16         </collection>
17     </resultMap>
18 </mapper>
teacherMapper.xml

⑥向配置文件中注册 studentMapper.xml和teacherMapper.xml 文件【已添加,查看第③步】

⑦测试类

 1 package org.mybatis.app;
 2 
 3 import java.io.InputStream;
 4 import java.util.List;
 5 
 6 import org.apache.ibatis.session.SqlSession;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 import org.junit.Before;
10 import org.junit.Test;
11 import org.mybatis.domain.Student;
12 import org.mybatis.domain.Teacher;
13 
14 public class TestOneToMany {
15 
16     SqlSession session;
17     
18     @Before
19     public void beforeLoad() {
20         InputStream inputStream = 
21                 TestOneToMany.class.getClassLoader().getResourceAsStream("conf.xml");
22         SqlSessionFactory sqlSessionFactory = 
23                 new SqlSessionFactoryBuilder().build(inputStream);
24         session = sqlSessionFactory.openSession();
25     }
26     
27     @Test
28     public void testGetStudents() {
29         String statement = "org.mybatis.mapping.studentMapper.getStudent";
30         List<Student> studentList = session.selectList(statement, 1);
31         for(Student s : studentList) {
32             System.out.println(s.getStudent_name() + "的师傅是:" + s.getTeacher().getTeacher_name());
33         }
34         
35         session.close();
36     }
37     
38     @Test
39     public void testGetTeacher() {
40         String statement = "org.mybatis.mapping.teacherMapper.getTeacher";
41         Teacher t = session.selectOne(statement, 1);
42         System.out.println(t.getTeacher_name() + "有" + t.getStudents().size() + "个徒弟");
43         session.close();
44     }
45     
46 }

⑧结构图

 

转载于:https://www.cnblogs.com/miantiaoandrew/p/8398443.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值