mybatis多对多关联实战教程(推荐)
MyBatis3.0添加了association和collection标签专门用于对多个相关实体类数据进行级联查询,但仍不支持多个相关实体类数据的级联保存和级联删除操作
一、创建student、teacher和stu_teach_rel三张张表
DROPTABLEIFEXISTS`student`; CREATETABLE`student`( `id`int(11)NOTNULL, `name`varchar(255)DEFAULTNULL, `age`int(11)DEFAULTNULL, `gender`varchar(255)DEFAULTNULL, PRIMARYKEY(`id`) )ENGINE=InnoDBDEFAULTCHARSET=utf8; INSERTINTO`student`VALUES('1','刘德华','55','0'); INSERTINTO`student`VALUES('2','张惠妹','49','1'); INSERTINTO`student`VALUES('3','谢霆锋','35','0'); INSERTINTO`student`VALUES('4','王菲','47','1'); INSERTINTO`student`VALUES('5','汪峰','48','0'); INSERTINTO`student`VALUES('6','章子怡','36','1');
DROPTABLEIFEXISTS`teacher`; CREATETABLE`teacher`( `id`int(11)NOTNULL, `name`varchar(255)DEFAULTNULL, `gender`varchar(255)DEFAULTNULL, `subject`varchar(255)DEFAULTNULL, `degree`varchar(255)DEFAULTNULL, PRIMARYKEY(`id`) )ENGINE=InnoDBDEFAULTCHARSET=utf8; INSERTINTO`teacher`VALUES('1','王晶','0','CHINESE','大专'); INSERTINTO`teacher`VALUES('2','冯小刚','0','ENGLISH','本科'); INSERTINTO`teacher`VALUES('3','吴京','0','MATHEMATICS','大专'); INSERTINTO`teacher`VALUES('4','王倦','1','MATHEMATICS','研究生');
DROPTABLEIFEXISTS`stu_teach_rel`; CREATETABLE`stu_teach_rel`( `id`int(11)NOTNULL, `stu_id`int(11)NOTNULL, `teach_id`int(11)NOTNULL, PRIMARYKEY(`id`) )ENGINE=InnoDBDEFAULTCHARSET=utf8; INSERTINTO`stu_teach_rel`VALUES('1','1','1'); INSERTINTO`stu_teach_rel`VALUES('2','2','1'); INSERTINTO`stu_teach_rel`VALUES('3','1','2'); INSERTINTO`stu_teach_rel`VALUES('4','2','2'); INSERTINTO`stu_teach_rel`VALUES('5','2','3'); INSERTINTO`stu_teach_rel`VALUES('6','3','1'); INSERTINTO`stu_teach_rel`VALUES('7','3','2'); INSERTINTO`stu_teach_rel`VALUES('8','1','3'); INSERTINTO`stu_teach_rel`VALUES('9','4','1'); INSERTINTO`stu_teach_rel`VALUES('10','4','2'); INSERTINTO`stu_teach_rel`VALUES('11','5','3'); INSERTINTO`stu_teach_rel`VALUES('12','5','4'); INSERTINTO`stu_teach_rel`VALUES('13','6','1'); INSERTINTO`stu_teach_rel`VALUES('14','6','3');
二、新建和表相关的实体类
packagecom.yihaomen.mybatis.model; importcom.yihaomen.mybatis.enums.Gender; importjava.util.List; publicclassStudent{ privateStringid; privateStringname; privateintage; privateGendergender; privateListteachers; setters&getters }
packagecom.yihaomen.mybatis.model; importcom.yihaomen.mybatis.enums.Gender; importcom.yihaomen.mybatis.enums.Subject; importjava.util.List; publicclassTeacher{ privateintid; privateStringname; privateGendergender; privateSubjectsubject; privateStringdegree; privateListstudents; setters&getters }
三、新建映射关系
packagecom.yihaomen.mybatis.dao; importcom.yihaomen.mybatis.model.Student; importorg.springframework.stereotype.Repository; importjava.util.List; @Repository publicinterfaceStudentMapper{ ListselectStudents(); }
student.xml
SELECT s.id,s.name,s.gender,t.idteach_id,t.nametname,t.gendertgender,t.subjecttsubject,t.degreetdegree FROM students LEFTJOIN stu_teach_relstr ON s.id=str.stu_id LEFTJOIN teachert ON t.id=str.teach_id
四、在configuration.xml中配置相关mapper
五、测试
packagecom.yihaomen.service.student; importcom.yihaomen.mybatis.dao.StudentMapper; importcom.yihaomen.mybatis.model.Student; importcom.yihaomen.mybatis.model.Teacher; importcom.yihaomen.service.BaseTest; importorg.apache.ibatis.session.SqlSession; importorg.apache.ibatis.session.SqlSessionFactory; importjava.util.List; publicclassTestStudentextendsBaseTest{ publicstaticvoidtestStuTeachRela(){ SqlSessionFactorysqlSessionFactory=getSession(); SqlSessionsession=sqlSessionFactory.openSession(); StudentMappermapper=session.getMapper(StudentMapper.class); Listlist=mapper.selectStudents(); for(Students:list){ System.out.println("------------------"); System.out.println(s.getName()+","+s.getAge()+","+s.getGender()); for(Teachert:s.getTeachers()){ System.out.println(t.getName()+","+t.getGender()+","+t.getSubject()); } } } publicstaticvoidmain(String[]args){ testStuTeachRela(); } }
以上这篇mybatis多对多关联实战教程(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。