mybatis多对多映射【学生与课程】
1)如图
2)创建students.sql和courses.sql和middles.sql
drop table middles; drop table students; drop table courses; create table students( sid int(5) primary key, sname varchar(10) ); create table courses( cid int(5) primary key, cname varchar(10) ); create table middles( sid int(5), cid int(5), primary key(sid,cid) ); insert into students(sid,sname) values(1,'哈哈'); insert into students(sid,sname) values(2,'呵呵'); insert into courses(cid,cname) values(1,'java'); insert into courses(cid,cname) values(2,'net'); insert into middles(sid,cid) values(1,1); insert into middles(sid,cid) values(1,2); insert into middles(sid,cid) values(2,1); insert into middles(sid,cid) values(2,2); select * from students; select * from courses; select * from middles;
3)创建Student.java和Course.java
/** * 学生(多方)*/ public class Student { private Integer id; private String name; private List<Course> courseList = new ArrayList<Course>(); public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Course> getCourseList() { return courseList; } public void setCourseList(List<Course> courseList) { this.courseList = courseList; } } /** * 课程(多方) */ public class Course { private Integer id; private String name; private List<Student> studentList = new ArrayList<Student>(); public Course(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } }
4)创建StudentMapper.xml和CourseMapper.xml
StudentMapper.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="studentNamespace"> <resultMap type="loaderman.many2many.Student" id="studentMap"> <id property="id" column="sid" /> <result property="name" column="sname"/> </resultMap> <select id="findStudentByName" parameterType="string" resultMap="studentMap"> select s.sid,s.sname from students s,middles m,courses c where s.sid = m.sid and m.cid = c.cid and c.cname = #{name} </select> </mapper>
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="courseNamespace"> <resultMap type="loaderman.many2many.Course" id="courseMap"> <id property="id" column="cid" /> <result property="name" column="cname"/> </resultMap> <select id="findCourseByName" parameterType="string" resultMap="courseMap"> select c.cid,c.cname from students s,middles m,courses c where s.sid = m.sid and m.cid = c.cid and s.sname = #{name} </select> </mapper>
5)创建students.dao
public class StudentDao { /** * 查询哈哈选学的【课程】 */ public List<Course> findCourseByName(String name) throws Exception{ SqlSession sqlSession = null; try{ sqlSession = MybatisUtil.getSqlSession(); return sqlSession.selectList("courseNamespace.findCourseByName",name); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ MybatisUtil.closeSqlSession(); } } /** * 查询java课程有哪些【学生】 */ public List<Student> findStudentByName(String name) throws Exception{ SqlSession sqlSession = null; try{ sqlSession = MybatisUtil.getSqlSession(); return sqlSession.selectList("studentNamespace.findStudentByName",name); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ MybatisUtil.closeSqlSession(); } } public static void main(String[] args) throws Exception{ StudentCourseDao dao = new StudentCourseDao(); //List<Course> courseList = dao.findCourseByName("哈哈"); //for(Course c : courseList){ // System.out.println(c.getId()+":"+c.getName()); //} List<Student> studentList = dao.findStudentByName("java"); for(Student s : studentList){ System.out.println(s.getId()+":"+s.getName()); } } }
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!