前言:本项目是使用idea进行开发,数据库使用的是Mysql。
其中,多对多的关系是Student--------Course-------screlation(关系表)
student
course
screlation
1.搭建一个maven项目
2.引入mybatis需要的依赖
习惯性建立java、resources、test等文件夹,建立文件夹的时候注意标记文件夹的用途(不然编译器无法识别,无法编译代码)
pom.xml(注意要将实体包下面的xml文件设置可编译)
由于本作者比较懒,前面一对一和一对多已经用到了这个pom.xml详情可参照https://blog.csdn.net/qq_41931566/article/details/86466985
3.项目模块图
3.在resources目录下新建mybatis的核心配置文件mybatis.xml
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--日志配置文件-->
<settings>
<setting name="logImpl" value="LOG4J2"/>
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments>
<!--配置实体映射关系-->
<mappers>
<mapper class="com.joy.mapper.StudentMapper"></mapper>
<mapper class="com.joy.mapper.CourseMapper"></mapper>
</mappers>
</configuration>
4.建立与数据库对应的实体包
Student.java
package com.joy.model;
import java.util.List;
public class Student {
private Integer sid;
private String sname;
private List<Course> courses;
public Student(Integer sid, String sname, List<Course> courses) {
this.sid = sid;
this.sname = sname;
this.courses = courses;
}
public Student() {
}
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;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", courses=" + courses +
'}';
}
}
Course.java
package com.joy.model;
import java.util.List;
public class Course {
private Integer cid;
private String cname;
private List<Student> students;
public Course(Integer cid, String cname, List<Student> students) {
this.cid = cid;
this.cname = cname;
this.students = students;
}
public Course() {
}
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;
}
@Override
public String toString() {
return "Course{" +
"cid=" + cid +
", cname='" + cname + '\'' +
'}';
}
}
5.建立Mapper包,在包里建接口
StudentMapper.java
package com.joy.mapper;
import com.joy.model.Student;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
public interface StudentMapper {
//根据sid查询学生
@Select("select * from student where sid=#{sid}")
@Results({
@Result(property = "sid",column = "sid",id = true),
@Result(property = "courses",column = "sid",many = @Many(select = "com.joy.mapper.CourseMapper.findStudentAndCourseBySid"))
})
public Student findStudentBySid(int sid);
//多对多查询(根据id查询学生及其所对应的课程)
@Select("select * from student s INNER JOIN screlation sc on s.sid=sc.sid where sc.cid=#{cid}")
public Student findStudentAndCourseBySid(int cid);
}
CourseMapper.java
package com.joy.mapper;
import com.joy.model.Course;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
public interface CourseMapper {
//通过学生id查询课程信息
@Select("select * from course where cid=#{cid}")
@Results({
@Result(property = "cid",column = "cid",id = true),
@Result(property = "students",column = "cid",many = @Many(select = "com.joy.mapper.StudentMapper.findStudentAndCourseBySid"))
})
public Course findCourseByCid(int cid);
//多对多查询(根据id查询学生及其所对应的课程)
@Select("select * from course c INNER JOIN screlation sc on c.cid=sc.cid where sc.sid=#{sid}")
public Course findStudentAndCourseBySid(int sid);
}
6.在test文件夹下新建com.joy.test文件夹,并且新建单元测试类StudentMapperTest.java
package com.joy.test;
import com.joy.mapper.CourseMapper;
import com.joy.mapper.StudentMapper;
import com.joy.model.Course;
import com.joy.model.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class StudentMapperTest {
private SqlSession sqlSession;
@Before
public void before(){
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(this.getClass().getClassLoader().getResourceAsStream("mybatis.xml"));
sqlSession = sqlSessionFactory.openSession();
}
@Test
public void test(){
//通过学生id查询学生及其所选修的课程
// StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
// Student student= studentMapper.findStudentBySid(1);
// System.out.println(student);
//通过课程id查询课程及其选修了这门课的学生
CourseMapper courseMapper=sqlSession.getMapper(CourseMapper.class);
Course course= courseMapper.findCourseByCid(2);
System.out.println(course);
for (Student s : course.getStudents()) {
System.out.println(s);
}
}
}
7.取消注释,运行测试类即可
最后的最后:写文章不容易啊,亲若是喜欢或者对你有帮助记得点赞、+关注或者收藏呦~就酱,啾咪~