mybatis的一对多

1、配置文件

db.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=123456

SqlMapConfig.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6 
 7     <!-- 加载java的配置文件 -->
 8     <properties resource="config/db.properties"/>
 9     
10     <!-- 配置mybatis的环境信息,与spring整合,该信息由spring来管理 -->
11     <environments default="development">
12         <environment id="development">
13             <!-- 配置JDBC事务控制,由mybatis进行管理 -->
14             <transactionManager type="JDBC"></transactionManager>
15             <!-- 配置数据源,采用mybatis连接池 -->
16             <dataSource type="POOLED">
17                 <property name="driver" value="${db.driver}" />
18                 <property name="url" value="${db.url}" />
19                 <property name="username" value="${db.username}" />
20                 <property name="password" value="${db.password}" />
21             </dataSource>
22         </environment>
23     </environments>
24 
25     <!-- 加载映射文件 -->
26     <mappers>
27         <mapper resource="config/mapper.xml" />
28     </mappers>
29     
30 </configuration>

mapper.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="com.xiaostudy.domain.Mapper">
 6 
 7     <!-- ========================================================================================== -->
 8     <!-- 一对多之resultMap1 -->
 9     <select id="findOfTeacher" resultMap="OfTeacher">
10         select * from  teacher t, student s where t.tid=s.tid and t.tid=#{id}
11     </select>
12     <resultMap type="com.xiaostudy.domain.Teacher" id="OfTeacher">
13         <id column="tid" property="tid"/>
14         <result column="tname" property="tname"/>
15         <collection  property="student" ofType="com.xiaostudy.domain.Student" ><!-- javaType="HashSet" -->
16             <id column="sid" property="sid"></id>
17             <result column="tid" property="tid"/>
18             <result column="sname" property="sname"/>
19         </collection>
20     </resultMap>
21    <!-- ========================================================================================== -->
22 
23     <!-- ========================================================================================== -->
24     <!-- 一对多之resultMap2 -->
25     <select id="findOfTeachers" resultMap="OfTeachers">
26         select * from teacher t, student s  where t.tid=s.tid and t.tid=#{id}
27     </select>
28     <resultMap type="com.xiaostudy.domain.Teacher" id="OfTeachers">
29         <id column="tid" property="tid"/>
30         <result column="tname" property="tname"/>
31         <collection property="student" ofType="com.xiaostudy.domain.Student" select="getStudent" column="tid"/>
32     </resultMap>
33     
34     <select id="getStudent" parameterType="int" resultType="com.xiaostudy.domain.Student">
35         select * from student s where s.tid = #{id}
36     </select>
37    <!-- ========================================================================================== -->
38 
39 </mapper>

2、domain类

Student.java

 1 package com.xiaostudy.domain;
 2 
 3 public class Student {
 4     private int sid;
 5     private int tid;
 6     private String sname;
 7     private Teacher teacher;
 8 
 9     public int getSid() {
10         return sid;
11     }
12 
13     public void setSid(int sid) {
14         this.sid = sid;
15     }
16 
17     public int getTid() {
18         return tid;
19     }
20 
21     public void setTid(int tid) {
22         this.tid = tid;
23     }
24 
25     public String getSname() {
26         return sname;
27     }
28 
29     public void setSname(String sname) {
30         this.sname = sname;
31     }
32 
33     public Teacher getTeacher() {
34         return teacher;
35     }
36 
37     public void setTeacher(Teacher teacher) {
38         this.teacher = teacher;
39     }
40 
41     @Override
42     public String toString() {
43         return "Student [sid=" + sid + ", tid=" + tid + ", sname=" + sname
44                 + ", teacher=" + teacher + "]";
45     }
46 
47 }

Teacher.java

 1 package com.xiaostudy.domain;
 2 
 3 import java.util.Set;
 4 
 5 public class Teacher {
 6     private int tid;
 7     private String tname;
 8     private Set<Student> student;
 9 
10     public Set<Student> getStudent() {
11         return student;
12     }
13 
14     public void setStudent(Set<Student> student) {
15         this.student = student;
16     }
17 
18     public int getTid() {
19         return tid;
20     }
21 
22     public void setTid(int tid) {
23         this.tid = tid;
24     }
25 
26     public String getTname() {
27         return tname;
28     }
29 
30     public void setTname(String tname) {
31         this.tname = tname;
32     }
33 
34     @Override
35     public String toString() {
36         return "Teacher [tid=" + tid + ", tname=" + tname + ", student=" + student + "]";
37     }
38 
39 }

3、代理类Mapper.java

 1 package com.xiaostudy.domain;
 2 
 3 import java.util.List;
 4 
 5 public interface Mapper {
 6 
 7     // 一对多之resultMap1
 8     public List<Teacher> findOfTeacher(int id);
 9     
10     // 一对多之resultMap2
11     public List<Teacher> findOfTeachers(int id);
12 
13 }

4、测试类

 1 package com.xiaostudy.test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.List;
 6 
 7 import org.apache.ibatis.io.Resources;
 8 import org.apache.ibatis.session.SqlSession;
 9 import org.apache.ibatis.session.SqlSessionFactory;
10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
11 
12 import com.xiaostudy.domain.Mapper;
13 import com.xiaostudy.domain.Teacher;
14 
15 /**
16  * @desc 测试类
17  * @author xiaostudy
18  *
19  */
20 public class MybatisTest {
21 
22     public static void main(String[] args) throws IOException {
23         String resource = "config/SqlMapConfig.xml";
24         InputStream inputStream = Resources.getResourceAsStream(resource);
25 
26         // 创建SqlSessionFactory
27         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
28 
29         // 创建SqlSession
30         SqlSession sqlSession = sqlSessionFactory.openSession();
31 
32         // 获取一个代理dao实现
33         Mapper mapper = sqlSession.getMapper(Mapper.class);
34         
35         //一对多之resultMap1
36         /*List<Teacher> list = mapper.findOfTeacher(1);
37         for(Teacher t : list) {
38             System.out.println(t);
39         }*/
40         
41         //一对多之resultMap2
42         List<Teacher> list = mapper.findOfTeachers(1);
43         for(Teacher t : list) {
44             System.out.println(t);
45         }
46         
47         sqlSession.close();
48 
49     }
50     
51 }

5、数据库

teacher表

student表

 


原文地址:https://www.cnblogs.com/xiaostudy/p/9588985.html