MyBatis是一個支持普通SQL查詢,存儲過程和高級映射的優(yōu)秀持久層框架。MyBatis消除了幾乎所有的JDBC代碼和參數(shù)的手工設置以及對結果集的檢索封裝。MyBatis可以使用簡單的XML或注解用于配置和原始映射,將接口和Java的POJO(Plain Old Java Objects,普通的Java對象)映射成數(shù)據(jù)庫中的記錄。
在上篇文章給大家介紹MyBatis一對一映射初識教程。
下面給大家說下mybatis多對多映射知識,具體詳情如下所示:
多對多的例子也不少,比如課程與學生之間的關系,一個課程可以有多個學生選修,而一個學生也可以選修多門學科。老師與學生之間的關系,一個老師有多個學生,一個學生有多個老師。
以學生和課程之間的關系為例。
我們建立數(shù)據(jù)表的時候有兩種方案:
第一種:
在建立student數(shù)據(jù)表的時候,存放一個課程的外鍵字段,
在建立course數(shù)據(jù)表的時候,存放一個學生的外鍵字段。
但是這樣是有很大弊端的,那就是如果我要刪student表,卻有course表的外鍵字段,
同理,我想刪除course表的時候,卻有student表的外鍵字段,哎,不好辦啊。
第二種:
我們建立student和course表,在兩張表中分別存放各自的字段和記錄,
再常見一個student_course表,作為中間表,存放student和course的外鍵。
這樣我們刪除字表的時候很方便哦,所以采用這樣方案。
數(shù)據(jù)庫腳本
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
|
-- 多對多映射 -- 刪除數(shù)據(jù)庫 drop database if exists mybatis; -- 創(chuàng)建數(shù)據(jù)庫 create database if not exists mybatis default character set utf8; -- 選擇數(shù)據(jù)庫 use mybatis; -- 刪除數(shù)據(jù)表 drop table if exists student; drop table if exists course; drop table if exists student_course; -- 創(chuàng)建數(shù)據(jù)表 create table student( sid int ( 255 ), sname varchar( 32 ), constraint pk_sid primary key (sid) ); create table course( cid int ( 255 ), cname varchar( 32 ), constraint pk_cid primary key (cid) ); create table student_course( sid int ( 255 ), cid int ( 255 ), constraint pk_sid_cid primary key(sid,cid), constraint fk_sid foreign key (sid) references student(sid), constraint fk_cid foreign key (cid) references course(cid) ); -- 測試數(shù)據(jù) insert into student (sid,sname) values ( 1 , '哈哈' ); insert into student (sid,sname) values ( 2 , '呵呵' ); insert into course (cid,cname) values ( 1 , 'java' ); insert into course (cid,cname) values ( 2 , '.NET' ); insert into student_course (sid,cid) values ( 1 , 1 ); insert into student_course (sid,cid) values ( 1 , 2 ); insert into student_course (sid,cid) values ( 2 , 1 ); insert into student_course (sid,cid) values ( 2 , 2 ); |
新建many2many.Course.java類
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
|
package many2many; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * 課程 * @author Administrator * */ @SuppressWarnings ( "serial" ) public class Course implements Serializable{ private Integer cid; private String cname; private List<Student> students = new ArrayList<Student>(); public Integer getCid() { return cid; } public void setCid(Integer cid) { this .cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this .cname = cname; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this .students = students; } } |
新建many2many.Student.java類
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
|
package many2many; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * 學生類 * @author Administrator * */ @SuppressWarnings ( "serial" ) public class Student implements Serializable { private Integer sid; private String sname; private List<Course> courses = new ArrayList<Course>(); public Integer getSid() { return sid; } public void setSid(Integer sid) { this .sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this .sname = sname; } public List<Course> getCourses() { return courses; } public void setCourses(List<Course> courses) { this .courses = courses; } } |
新建StudentMapper.xml文件和CourseMapper.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace= "studentMapper" > <resultMap type= "many2many.Student" id= "studentMap" > <id property= "sid" column= "sid" /> <result property= "sname" column= "sname" /> </resultMap> </mapper> <?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace= "courseNamespace" > <resultMap type= "many2many.Course" id= "courseMap" > <id property= "cid" column= "cid" /> <result property= "cname" column= "cname" /> </resultMap> <!-- 查詢“哈哈”選修了那幾門課程 --> <select id= "findAllByName" parameterType= "string" resultMap= "courseMap" > select c.cname,c.cid from student s,course c,student_course sc where s.sid = sc.sid and c.cid = sc.cid and s.sname = #{sname}; </select> </mapper> |
新建持久層類StudentCourseDAO.java類
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
|
package many2many; import java.util.Iterator; import java.util.List; import one2many.Student; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import util.MyBatisUtil; public class StudentCourseDAO { /** * 查詢“哈哈”選修了那幾門課程 * @param name 學生的姓名 * @return * @throws Exception */ public List<Course> findAllByName(String name) throws Exception{ SqlSession sqlSession = null ; try { sqlSession = MyBatisUtil.getSqlSession(); return sqlSession.selectList( "courseNamespace.findAllByName" , name); } catch (Exception e) { e.printStackTrace(); throw e; } finally { MyBatisUtil.closeSqlSession(); } } @Test public void testFindAllByName() throws Exception{ StudentCourseDAO dao = new StudentCourseDAO(); List<Course> courses = dao.findAllByName( "哈哈" ); for (Course course : courses) { System.out.println(course.getCid()+ ":" +course.getCname()); } } } |
在mybatis.cfg.xml文件中加載配置文件
1
2
3
4
5
6
7
8
9
|
<!-- 加載映射文件 --> <mappers> <mapper resource= "one2one/CardMapper.xml" /> <mapper resource= "one2one/StudentMapper.xml" /> <mapper resource= "one2many/GradeMapper.xml" /> <mapper resource= "one2many/StudentMapper.xml" /> <mapper resource= "many2many/StudentMapper.xml" /> <mapper resource= "many2many/CourseMapper.xml" /> </mappers> |
以上所述是小編給大家介紹的MyBatis多對多映射初識教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!
原文鏈接:http://11841428.blog.51cto.com/11831428/1832278