iBATIS的多对多映射配置浅析

开发 后端
iBATIS的多对多映射配置有什么需要注意的么?那么本文就通过实例的形式向你详细介绍。

iBATIS的多对多映射配置方法和多对一映射配置方法差不多,不同的是,多对多映射,数据库设计上需要一个记录两个类关系的中间表,本文以学生-老师为例,在iBATIS的sqlmap中配置多对多关系。

iBATIS的多对多映射配置1,建表。数据库中三个表,分别为:

  1.  CREATE TABLE [student] (  
  2.  [id] [int] IDENTITY (1, 1) NOT NULL ,  
  3.  [name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,  
  4.  [birthday] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,  
  5.  CONSTRAINT [PK_student] PRIMARY KEY  CLUSTERED   
  6.  (  
  7.   [id]  
  8.  )  ON [PRIMARY]   
  9. ON [PRIMARY]  
  10. GO  
  11. --------------------------------------------------  
  12. CREATE TABLE [teacher] (  
  13.  [id] [int] IDENTITY (1, 1) NOT NULL ,  
  14.  [name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,  
  15.  [subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,  
  16.  CONSTRAINT [PK_teacher] PRIMARY KEY  CLUSTERED   
  17.  (  
  18.   [id]  
  19.  )  ON [PRIMARY]   
  20. ON [PRIMARY]  
  21. GO  
  22. ---------------------------------------------------  
  23. CREATE TABLE [student_teacher] (  
  24.  [studentid] [intNOT NULL ,  
  25.  [teacherid] [intNOT NULL ,  
  26.  CONSTRAINT [PK_student_teacher] PRIMARY KEY  CLUSTERED   
  27.  (  
  28.   [studentid],  
  29.   [teacherid]  
  30.  )  ON [PRIMARY]   
  31. ON [PRIMARY]  
  32. GO 

iBATIS的多对多映射配置2,准备数据

  1. insert into student(name,birthday) values('张三','1982-01-01')  
  2. insert into student(name,birthday) values('李四','1983-02-02')  
  3. insert into student(name,birthday) values('王五','1984-03-03')  
  4.  
  5. insert into student(name,birthday) values('赵六','1985-04-04')  
  6. insert into teacher(name,subject) values('Jerry','语文')  
  7. insert into teacher(name,subject) values('Tom','数学')  
  8.  
  9. insert into teacher(name,subject) values('Steven','英语')  
  10. insert into student_teacher(studentid,teacherid) values(1,1)  
  11. insert into student_teacher(studentid,teacherid) values(1,2)  
  12. insert into student_teacher(studentid,teacherid) values(2,1)  
  13. insert into student_teacher(studentid,teacherid) values(3,2) 

iBATIS的多对多映射配置3,properties文件内容如下:

  1. driver=com.microsoft.jdbc.sqlserver.SQLServerDriver  
  2.  url=jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=cursor;DatabaseName=ibatistest  
  3.  username=sa  
  4.  password=000 

iBATIS的多对多映射配置4,总配置文件SqlMapConfig.xml内容如下:

  1. ﹤?xml version="1.0" encoding="UTF-8" ?﹥  
  2. ﹤!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"  
  3.     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"﹥  
  4.  
  5. ﹤sqlMapConfig﹥  
  6.  
  7.  ﹤properties resource="com/lsm/cfg/jdbc.properties" /﹥  
  8.  ﹤transactionManager type="JDBC"﹥  
  9.   ﹤dataSource type="SIMPLE"﹥  
  10.    ﹤property value="${driver}" name="JDBC.Driver" /﹥  
  11.    ﹤property value="${url}" name="JDBC.ConnectionURL" /﹥  
  12.    ﹤property value="${username}" name="JDBC.Username" /﹥  
  13.    ﹤property value="${password}" name="JDBC.Password" /﹥  
  14.   ﹤/dataSource﹥  
  15.  ﹤/transactionManager﹥  
  16.  ﹤sqlMap resource="com/lsm/domain/Student.xml" /﹥  
  17.  ﹤sqlMap resource="com/lsm/domain/Teacher.xml" /﹥  
  18.  
  19. ﹤/sqlMapConfig﹥ 

iBATIS的多对多映射配置5,domain对象两个,Student 和 Teacher,如下:

Teacher.java

  1. package com.lsm.domain;  
  2. import java.util.List;  
  3. public class Teacher  
  4. {  
  5.  private int id;  
  6.  private String name;  
  7.  private String subject;  
  8.  private List students; //注意这里有个List类型的students,表示一个老师对应多个学生  
  9.    
  10.  public List getStudents()  
  11.  {  
  12.   return students;  
  13.  }  
  14.  public void setStudents(List students)  
  15.  {  
  16.   this.students = students;  
  17.  }  
  18.  //省略掉其他的getter and setter  
  19. }  
  20.  
  21. //Student.java  
  22. package com.lsm.domain;  
  23.  
  24. import java.util.List;  
  25.  
  26. public class Student  
  27. {  
  28.  private int id;  
  29.  private String name;  
  30.  private String birthday;  
  31.  private List teachers; //这里有一个list类型的teachers,表示一个学生有多个老师  
  32.    
  33.  public List getTeachers()  
  34.  {  
  35.   return teachers;  
  36.  }  
  37.  public void setTeachers(List teachers)  
  38.  {  
  39.   this.teachers = teachers;  
  40.  }  
  41.  //省略掉其他的getter and setter  

iBATIS的多对多映射配置6,sqlmap配置文件

Teacher.xml

  1. ﹤?xml version="1.0" encoding="UTF-8" ?﹥  
  2. !DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
  3.    "http://ibatis.apache.org/dtd/sql-map-2.dtd"﹥  
  4.      
  5.    ﹤sqlMap namespace="teacher"﹥  
  6.        ﹤typeAlias alias="Teacher" type="com.lsm.domain.Teacher" /﹥  
  7.  ﹤typeAlias alias="Student" type="com.lsm.domain.Student" /﹥  
  8.    
  9.  ﹤resultMap class="Teacher" id="teacherBasicResultMap"﹥  
  10.   ﹤result property="id" column="id"/﹥  
  11.   ﹤result property="name" column="name"/﹥  
  12.   ﹤result property="subject" column="subject"/﹥  
  13.  ﹤/resultMap﹥  
  14.  ﹤!-- 下面这个resultMap中有个students属性,这个结果映射继承自上面的结果映射  
  15.   由于有了继承,结果映射可以任意扩展--﹥  
  16.  ﹤resultMap class="Teacher" id="teacherWithTeacherResultMap" extends="teacherBasicResultMap"﹥  
  17.   ﹤result property="students" column="id" select="getStudentsByTeacherId"/﹥  
  18.  ﹤/resultMap﹥  
  19.  ﹤!-- 这个查询中使用到了上面定义的结果映射,从而决定了查询出来的Teacher中关联出相关的students,在student.xml中配置相似,不再注释。--﹥  
  20.  ﹤select id="getTeachers" resultMap="teacherWithTeacherResultMap"﹥  
  21.   ﹤!--[CDATA[  
  22.    select * from teacher  
  23.   ]]﹥  
  24.  ﹤/select﹥  
  25.    
  26.  ﹤select id="getStudentsByTeacherId" resultClass="Student"﹥  
  27.   ﹤![CDATA[   
  28.    select s.* from student s,student_teacher st where s.id=st.studentid and st.teacherid=#value#   ]]--﹥  
  29.  ﹤/select﹥  
  30.          
  31.    ﹤/sqlMap﹥  
  32.  
  33. tudent.xml  
  34.  
  35. ﹤?xml version="1.0" encoding="UTF-8" ?﹥  
  36. !DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
  37.    "http://ibatis.apache.org/dtd/sql-map-2.dtd"﹥  
  38.      
  39.    ﹤sqlMap namespace="student"﹥  
  40.        ﹤typeAlias alias="Student" type="com.lsm.domain.Student" /﹥  
  41.  ﹤typeAlias alias="Teacher" type="com.lsm.domain.Teacher" /﹥  
  42.    
  43.  ﹤resultMap class="Student" id="studentBasicResultMap"﹥  
  44.   ﹤result property="id" column="id"/﹥  
  45.   ﹤result property="name" column="name"/﹥  
  46.   ﹤result property="birthday" column="birthday"/﹥  
  47.  ﹤/resultMap﹥  
  48.    
  49.  ﹤resultMap class="Student" id="studentWithTeacherResultMap" extends="studentBasicResultMap"﹥  
  50.   ﹤result property="teachers" column="id" select="getTeachersByStudentId"/﹥  
  51.  ﹤/resultMap﹥  
  52.    
  53.  ﹤select id="getStudents" resultMap="studentWithTeacherResultMap"﹥  
  54.   ﹤!--[CDATA[  
  55.    select * from student  
  56.   ]]﹥  
  57.  ﹤/select﹥  
  58.    
  59.  ﹤select id="getTeachersByStudentId" resultClass="Teacher"﹥  
  60.   ﹤![CDATA[   
  61.    select t.* from teacher t,student_teacher st where t.id=st.teacherid and st.studentid=#value#   ]]--﹥  
  62.  ﹤/select﹥  
  63.          
  64.    ﹤/sqlMap﹥ 

iBATIS的多对多映射配置7,测试

  1.  package com.lsm.test;  
  2.  
  3. import java.io.Reader;  
  4. import java.sql.SQLException;  
  5. import java.util.List;  
  6. import com.ibatis.common.resources.Resources;  
  7. import com.ibatis.sqlmap.client.SqlMapClient;  
  8. import com.ibatis.sqlmap.client.SqlMapClientBuilder;  
  9. import com.lsm.domain.Student;  
  10. import com.lsm.domain.Teacher;  
  11.  
  12. public class Many2Many  
  13. {  
  14.  
  15.  private static SqlMapClient sqlMapClient = null;  
  16.  static 
  17.  {  
  18.   try 
  19.   {  
  20.    Reader reader = Resources.getResourceAsReader("com/lsm/cfg/SqlMapConfig.xml");  
  21.    sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);  
  22.   }  
  23.   catch(Exception e)  
  24.   {  
  25.    e.printStackTrace();  
  26.   }  
  27.  }  
  28.  /**  
  29.   * @param args  
  30.   */ 
  31.  public static void main(String[] args)  
  32.  {  
  33.   Many2Many m2m = new Many2Many();  
  34.   List studentlist = null;  
  35.   studentlist = m2m.getStudentInfo();  
  36.   for(int i=0;i﹤studentlist.size();i++)  
  37.   {  
  38.    Student s = new Student();  
  39.    s = (Student) studentlist.get(i);  
  40.    System.out.println("name:"+s.getName() + "\t" + "birthday:"+s.getBirthday());  
  41.    List tlist = s.getTeachers();  
  42.    if(tlist!=null)  
  43.    {  
  44.     System.out.println("his teachers as follows:");  
  45.     {  
  46.      for(int ti=0;ti﹤tlist.size();ti++)  
  47.      {  
  48.       Teacher t = new Teacher();  
  49.       t = (Teacher) tlist.get(ti);  
  50.       System.out.println("teacher name:" + t.getName());  
  51.      }  
  52.     }  
  53.    }  
  54.   }  
  55.     
  56.   List teacherlist = null;  
  57.   teacherlist = m2m.getTeacherInfo();  
  58.   for(int i=0;i﹤teacherlist.size();i++)  
  59.   {  
  60.    Teacher t = new Teacher();  
  61.    t = (Teacher) teacherlist.get(i);  
  62.    System.out.println("name:"+t.getName() + "\t" + "subject:" + t.getSubject());  
  63.    List slist = t.getStudents();  
  64.    if(slist!=null)  
  65.    {  
  66.     System.out.println("his students as follows:");  
  67.     for(int si=0;si﹤slist.size();si++)  
  68.     {  
  69.      Student s = new Student();  
  70.      s = (Student) slist.get(si);  
  71.      System.out.println("student name:"+s.getName());  
  72.     }  
  73.    }  
  74.   }  
  75.  }  
  76.    
  77.  // 获取学生信息  
  78.  public List getStudentInfo()  
  79.  {  
  80.   List studentList = null;  
  81.   try 
  82.   {  
  83.    System.out.println("学生信息如下:");  
  84.    studentList = sqlMapClient.queryForList("getStudents");  
  85.   }  
  86.   catch (SQLException e)  
  87.   {  
  88.    e.printStackTrace();  
  89.   }  
  90.   return studentList;  
  91.  }  
  92.    
  93.  // 获取老师信息  
  94. //  获取学生信息  
  95.  public List getTeacherInfo()  
  96.  {  
  97.   List studentList = null;  
  98.   try 
  99.   {  
  100.    System.out.println("老师信息如下:");  
  101.    studentList = sqlMapClient.queryForList("getTeachers");  
  102.   }  
  103.   catch (SQLException e)  
  104.   {  
  105.    e.printStackTrace();  
  106.   }  
  107.   return studentList;  
  108.  }  
  109.  
  110. }  
  111.  

8,输出

  1. 学生信息如下:  
  2. name:张三 birthday:1982-01-01  
  3. his teachers as follows:  
  4. teacher name:Jerry  
  5. teacher name:Tom  
  6. name:李四 birthday:1983-02-02  
  7. his teachers as follows:  
  8. teacher name:Jerry  
  9. name:王五 birthday:1984-03-03  
  10. his teachers as follows:  
  11. teacher name:Tom  
  12. name:赵六 birthday:1985-04-04  
  13. his teachers as follows:  
  14. 老师信息如下:  
  15. name:Jerry subject:语文  
  16. his students as follows:  
  17. student name:张三  
  18. student name:李四  
  19. name:Tom subject:数学  
  20. his students as follows:  
  21. student name:张三  
  22. student name:王五  
  23. name:Steven subject:英语  
  24. his students as follows: 

查询学生时带出老师信息,查询老师时带出学生信息,说明多对多映射成功。

iBATIS的多对多映射配置的情况就向你介绍到这里,希望对你有所帮助。

【编辑推荐】

  1. iBATIS教程之如何获得output参数值
  2. iBATIS.NET处理多参数的SQL语句的配置
  3. iBATIS.NET与VS 2005进行单元测试浅析
  4. iBATIS.NET执行存储过程实例详解
  5. iBATIS一对多映射解析
责任编辑:仲衡 来源: CSDN博客
相关推荐

2009-07-21 17:31:39

iBATIS一对多映射

2009-07-15 17:58:07

iBATIS 动态映射

2009-07-21 11:12:00

iBATIS配置

2009-06-18 14:22:06

Hibernate多对Hibernate

2009-06-24 15:53:08

Entity Bean多对多映射

2009-06-11 09:35:47

GlassFish配置多机集群

2009-07-21 16:17:28

iBATIS.NET

2009-07-22 09:44:05

iBATIS Para

2009-07-22 15:01:01

iBATIS SQLM

2009-07-21 11:17:46

iBATISDAO的配置

2009-06-04 16:14:22

Hibernate一对Hibernate一对Hibernate多对

2009-07-17 16:49:18

iBATIS XML配

2009-07-22 16:27:24

iBATIS配置类iBATIS操作类

2009-07-20 13:47:08

iBATIS.NET字

2023-05-28 23:26:16

多模态机器学习大脑

2010-04-15 09:09:02

Hibernate

2022-01-12 11:55:43

Kubernetes多集群Linux

2009-07-17 10:59:59

iBATIS接口

2009-07-20 16:41:27

Struts2.0+i

2009-01-20 09:22:09

NGN下一代网络电信
点赞
收藏

51CTO技术栈公众号