iBATIS的多对多映射配置浅析

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

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

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

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

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

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

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

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

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

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

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

Teacher.java

package com.lsm.domain;  
import java.util.List;  
public class Teacher  
{  
 private int id;  
 private String name;  
 private String subject;  
 private List students; //注意这里有个List类型的students,表示一个老师对应多个学生  
   
 public List getStudents()  
 {  
  return students;  
 }  
 public void setStudents(List students)  
 {  
  this.students = students;  
 }  
 //省略掉其他的getter and setter  
}  
 
//Student.java  
package com.lsm.domain;  
 
import java.util.List;  
 
public class Student  
{  
 private int id;  
 private String name;  
 private String birthday;  
 private List teachers; //这里有一个list类型的teachers,表示一个学生有多个老师  
   
 public List getTeachers()  
 {  
  return teachers;  
 }  
 public void setTeachers(List teachers)  
 {  
  this.teachers = teachers;  
 }  
 //省略掉其他的getter and setter  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

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

Teacher.xml

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

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

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

8,输出

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

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

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-11 09:35:47

GlassFish配置多机集群

2009-06-24 15:53:08

Entity Bean多对多映射

2009-07-22 09:44:05

iBATIS Para

2009-07-22 15:01:01

iBATIS SQLM

2009-07-21 16:17:28

iBATIS.NET

2009-07-21 11:17:46

iBATISDAO的配置

2009-07-17 16:49:18

iBATIS XML配

2009-06-04 16:14:22

Hibernate一对Hibernate一对Hibernate多对

2009-07-22 16:27:24

iBATIS配置类iBATIS操作类

2009-07-20 13:47:08

iBATIS.NET字

2023-05-28 23:26:16

多模态机器学习大脑

2009-07-17 10:59:59

iBATIS接口

2022-01-12 11:55:43

Kubernetes多集群Linux

2010-04-15 09:09:02

Hibernate

2009-07-20 16:41:27

Struts2.0+i

2009-07-17 10:32:45

iBATIS MapB
点赞
收藏

51CTO技术栈公众号