MyBatis 中兩表關聯查詢MYSQL (14)
阿新 • • 發佈:2021-10-19
MyBatis 中兩表關聯查詢MYSQL
1、建立資料庫表語句
2、插入測試資料
3、pom檔案內容
<?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> <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://127.0.0.1:3306/mybatis01"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <!-- jack.zhao 載入對映檔案 --> <mapper resource="com/mybatis03/mapper/personMapper.xml"/> <mapper resource="com/mybatis03/mapper/TeacherMapper.xml"/> </mappers> </configuration>
4、實體類
4.1教師表對應的實體類
package com.mybatis03.bean; /** * @author :jack.zhao * @description 教師實體類 * @date :2021-10-16 22:55 */ public class Teacher { /* 教師編號 */ private int teaNo; /* 課程編號 */ private int courseNo; /* 教師名稱 */ private String teaName; public int getCourseNo() {return courseNo; } public void setCourseNo(int courseNo) { this.courseNo = courseNo; } public int getTeaNo() { return teaNo; } public void setTeaNo(int teaNo) { this.teaNo = teaNo; } public String getTeaName() { return teaName; }public void setTeaName(String teaName) { this.teaName = teaName; } }
4.2 教師課程實體類
package com.mybatis03.bean; /** * @author :jack.zhao * @Describe: 教師課程類 * @date :2021-10-16 22:55 */ public class TeacherCourse { /* 課程編號 */ private int courseNo; /* 課程名稱 */ private String courseName; public int getCourseNo() { return courseNo; } public void setCourseNo(int courseNo) { this.courseNo = courseNo; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } }
4.3 教師實體類與課程實體類總的集合類(用於關聯資料庫表查詢結果集)
package com.mybatis03.bean; /** * @author :jack.zhao * @description 此類包含教師和課程屬性 * @date :2021-10-16 22:55 */ public class TeacherBusiness extends Teacher { public TeacherBusiness(){ super(); } /* 課程編號 */ private int courseNo; /* 課程名稱 */ private String courseName; public int getCourseNo() { return courseNo; } public void setCourseNo(int courseNo) { this.courseNo = courseNo; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } @Override public String toString() { return "TeacherBusiness{" + "courseNo=" + courseNo + ", courseName='" + courseName + ", teacherName='" + this.getTeaName() + ", teacherNO='" + this.getTeaNo() + '\'' + '}'; } }
5、介面類
package com.mybatis03.mapper; import com.mybatis03.bean.TeacherBusiness; import java.util.List; /** * @author :jack.zhao * @Describe: 操作mybatis介面 * @date :2021-10-16 22:55 */ public interface TeacherMapper { List<TeacherBusiness> queryTeacherBusinessInfoWithCourseNo(int courseNo); }
6、mapper.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="com.mybatis03.mapper.TeacherMapper"> <select id="queryTeacherBusinessInfoWithCourseNo" parameterType="int" resultType="com.mybatis03.bean.TeacherBusiness"> select s.*,c.* FROM teacher s INNER JOIN teachercourse c ON s.courseno = c.courseno where s.teano = #{teano} </select> </mapper>
7、測試類
package com.mybatis03.test; import com.mybatis03.bean.TeacherBusiness; import com.mybatis03.mapper.TeacherMapper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.jupiter.api.Test; import java.io.Reader; import java.util.List; /** * @author :jack.zhao * @Describe: 測試 * @date :2021-10-16 22:55 */ public class testTeacher01 { @Test public void queryTeacherBusinessInfoWithCourseNo() throws Exception{ Reader reader = Resources.getResourceAsReader("mybatis-03.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); int courseNo=1002; // (jack.zhao)動態代理 TeacherMapper personMapper = session.getMapper(TeacherMapper.class); List<TeacherBusiness> teacherBusinessList = personMapper.queryTeacherBusinessInfoWithCourseNo(courseNo); System.out.println("聯合查詢結果為:"+teacherBusinessList); } }
8、執行測試結果