国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

MyBatis多對(duì)多映射初識(shí)教程

2019-11-26 13:56:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

MyBatis是一個(gè)支持普通SQL查詢(xún),存儲(chǔ)過(guò)程和高級(jí)映射的優(yōu)秀持久層框架。MyBatis消除了幾乎所有的JDBC代碼和參數(shù)的手工設(shè)置以及對(duì)結(jié)果集的檢索封裝。MyBatis可以使用簡(jiǎn)單的XML或注解用于配置和原始映射,將接口和Java的POJO(Plain Old Java Objects,普通的Java對(duì)象)映射成數(shù)據(jù)庫(kù)中的記錄。

在上篇文章給大家介紹MyBatis一對(duì)一映射初識(shí)教程

下面給大家說(shuō)下mybatis多對(duì)多映射知識(shí),具體詳情如下所示:

多對(duì)多的例子也不少,比如課程與學(xué)生之間的關(guān)系,一個(gè)課程可以有多個(gè)學(xué)生選修,而一個(gè)學(xué)生也可以選修多門(mén)學(xué)科。老師與學(xué)生之間的關(guān)系,一個(gè)老師有多個(gè)學(xué)生,一個(gè)學(xué)生有多個(gè)老師。
以學(xué)生和課程之間的關(guān)系為例。

我們建立數(shù)據(jù)表的時(shí)候有兩種方案:

第一種:

在建立student數(shù)據(jù)表的時(shí)候,存放一個(gè)課程的外鍵字段,

在建立course數(shù)據(jù)表的時(shí)候,存放一個(gè)學(xué)生的外鍵字段。

但是這樣是有很大弊端的,那就是如果我要?jiǎng)hstudent表,卻有course表的外鍵字段,

同理,我想刪除course表的時(shí)候,卻有student表的外鍵字段,哎,不好辦啊。

第二種:

我們建立student和course表,在兩張表中分別存放各自的字段和記錄,

再常見(jiàn)一個(gè)student_course表,作為中間表,存放student和course的外鍵。

這樣我們刪除字表的時(shí)候很方便哦,所以采用這樣方案。

數(shù)據(jù)庫(kù)腳本

-- 多對(duì)多映射-- 刪除數(shù)據(jù)庫(kù)drop database if exists mybatis;-- 創(chuàng)建數(shù)據(jù)庫(kù)create database if not exists mybatis default character set utf8;-- 選擇數(shù)據(jù)庫(kù)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) );-- 測(cè)試數(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類(lèi)

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類(lèi)

package many2many;import java.io.Serializable;import java.util.ArrayList;import java.util.List;/*** 學(xué)生類(lèi)* @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文件

<?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><!-- 查詢(xún)“哈哈”選修了那幾門(mén)課程 --><select id="findAllByName" parameterType="string" resultMap="courseMap">select c.cname,c.cidfrom student s,course c,student_course scwhere s.sid = sc.sid and c.cid = sc.cid and s.sname = #{sname};</select></mapper>

新建持久層類(lèi)StudentCourseDAO.java類(lèi)

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 {/*** 查詢(xún)“哈哈”選修了那幾門(mén)課程* @param name 學(xué)生的姓名* @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();}}@Testpublic 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文件中加載配置文件

<!-- 加載映射文件 --><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多對(duì)多映射初識(shí)教程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 达日县| 宝清县| 青海省| 阿克陶县| 名山县| 安塞县| 昌邑市| 平潭县| 建平县| 元谋县| 武强县| 沂南县| 内江市| 甘泉县| 宜都市| 淮阳县| 孙吴县| 读书| 札达县| 金堂县| 洞口县| 汪清县| 横山县| 壶关县| 临沧市| 海宁市| 商丘市| 镇原县| 扎赉特旗| 分宜县| 咸宁市| 西峡县| 承德县| 肥乡县| 嘉定区| 汕尾市| 石家庄市| 福建省| 石渠县| 东丽区| 周至县|