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] [int] NOT NULL ,
- [teacherid] [int] NOT NULL ,
- CONSTRAINT [PK_student_teacher] PRIMARY KEY CLUSTERED
- (
- [studentid],
- [teacherid]
- ) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
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)
iBATIS的多对多映射配置3,properties文件内容如下:
- driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
- url=jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=cursor;DatabaseName=ibatistest
- username=sa
- password=000
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﹥
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
- }
评论