mybatis多对多关联

来源:互联网 发布:博雅斗地主源码 编辑:程序博客网 时间:2024/04/29 22:39

一.创建数据库表

create table t_student( id int primary key auto_increment, student_name varchar(20));create table t_courses(id  int primary key auto_increment,courses_name varchar(20));create table t_stu_cou( id int primary key auto_increment, fk_stu_id int, fk_cou_id int );
二.创建接口以及映射文件
public CoursesBean findCouById(int id);public CoursesBean findCouAndStu(int id);}public interface StudentMapper {public StudentBean findStuById(int id);public StudentBean findStuAndCou(int id);}<mapper namespace="com.dao.CoursesMapper"><resultMap type="CoursesBean" id="coursesMap"><id property="id" column="id" javaType="java.lang.Integer" /><result property="name" column="courses_name" javaType="java.lang.String" /></resultMap><resultMap type="CoursesBean" id="couAndStu"><id property="id" column="id" javaType="java.lang.Integer" /><result property="name" column="courses_name" javaType="java.lang.String" /><collection property="student" column="id"select="findStudentByCourses"></collection></resultMap><select id="findCouById" resultMap="coursesMap">select * from t_courses whereid=#{id}</select><select id="findStudentByCourses" resultMap="com.dao.StudentMapper.studentMap">select * fromt_student where id in (select fk_stu_id from t_stu_cou wherefk_cou_id=#{id})</select><select id="findCouAndStu" resultMap="couAndStu">select * from t_courseswhere id=#{id}</select></mapper><mapper namespace="com.dao.StudentMapper"><resultMap type="StudentBean" id="studentMap"><id property="id" column="id" javaType="java.lang.Integer" /><result property="name" column="student_name" javaType="java.lang.String" /></resultMap><resultMap type="StudentBean" id="studentAndCourses"><id property="id" column="id" javaType="java.lang.Integer" /><result property="name" column="student_name" javaType="java.lang.String" /><collection property="courses" column="id"select="findCoursesByStudent"></collection></resultMap><select id="findStudentById" resultMap="studentMap">select * from t_studentwhere id = #{id}</select><select id="findStuAndCou" resultMap="studentAndCourses">select * from t_studentwhere id = #{id}</select><select id="findCoursesByStudent" resultMap="com.dao.CoursesMapper.coursesMap">select * fromt_courses where id in (select fk_cou_id from t_stu_cou wherefk_stu_id= #{id})</select></mapper>
三.编写单元测试
public class ManyToManyTest {SqlSession session = null;@Beforepublic void setUp() throws Exception {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);session = sqlSessionFactory.openSession();}@Afterpublic void tearDown() throws Exception {session.commit();session.close();}@Testpublic void findCoursesByStudent() {StudentMapper sm = session.getMapper(StudentMapper.class);StudentBean sb = sm.findStuAndCou(1);System.out.println(sb);}@Testpublic void findStudentByCourses() {CoursesMapper cm = session.getMapper(CoursesMapper.class);CoursesBean cb = cm.findCouAndStu(2);System.out.println(cb);}}
源码链接:https://github.com/wangjianyangchn/myBatis/tree/master/AssociationManytoMany

参考文档:http://www.mybatis.org/mybatis-3/zh/index.html

0 0
原创粉丝点击