Mybatis之多對一處理
阿新 • • 發佈:2021-11-30
Mybatis
10、多對一處理
10.1、資料庫
-
USE ibatis; CREATE TABLE teacher( teacher_id INT(10) NOT NULL, teacher_name VARCHAR(30) DEFAULT NULL, PRIMARY KEY(teacher_id) )ENGINE = INNOBASE DEFAULT CHARSET=utf8; INSERT INTO teacher(teacher_id,teacher_name) VALUES (1,'jcooling'); CREATE TABLE student( student_id INT(10) NOT NULL, student_NAME VARCHAR(30) DEFAULT NULL, teacher_id INT(10) DEFAULT NULL, PRIMARY KEY(student_id), KEY fkteacher_id (teacher_id), CONSTRAINT fkteacher_id FOREIGN KEY(teacher_id) REFERENCES teacher(teacher_id) )ENGINE = INNOBASE DEFAULT CHARSET=utf8; INSERT INTO student(student_id,student_NAME,teacher_id) VALUES(1, '小明', '1'); INSERT INTO student(student_id,student_NAME,teacher_id) VALUES(2, '小紅', '1'); INSERT INTO student(student_id,student_NAME,teacher_id) VALUES(3, '小張', '1'); INSERT INTO student(student_id,student_NAME,teacher_id) VALUES(4, '小李', '1'); INSERT INTO student(student_id,student_NAME,teacher_id) VALUES(5, '小王', '1');
10.2、環境搭建
-
匯入lombok。
-
新建實體類Teacher,Student。
-
@NoArgsConstructor @AllArgsConstructor @Data @ToString public class Student { private int studentId; private String studentName; /*學生需要關聯一個老師!*/ private Teacher teacher; }
-
@NoArgsConstructor @AllArgsConstructor @Data @ToString public class Teacher { private Integer teacherId; private String teacherName; }
-
-
建立Mapper介面。
-
public interface StudentMapper { }
-
public interface TeacherMapper { Teacher getTeacher(@Param("teacherId") int teacherId); }
-
-
寫*Mapper.xml。
-
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.jcooling.mapper.TeacherMapper"> <select id="getTeacher" resultType="Teacher"> select * from teacher where teacher_id= #{teacherId}; </select> </mapper>
-
-
建立*Mapper.xml檔案在核心配置檔案中繫結註冊我們的Mapper介面或者檔案!
-
<!--每一個Mapper.XML都需要在Mybatis核心配置檔案中註冊!--> <mappers> <package name="com.jcooling.mapper"></package> </mappers>
-
-
測試查詢是否能夠成功!
-
@Test public void testGetTeacher(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacher = mapper.getTeacher(1); System.out.println(teacher); }
-
-
實體類屬性使用駝峰命名,為了能使資料庫欄位與Java實體類屬性一一對映,需要做一下配置。
-
<settings> <!--<setting name="logImpl" value="STDOUT_LOGGING"/>--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
-
10.3、按照查詢巢狀處理
-
<!--思路: 1.查詢所有的學生資訊。 2.根據查詢出來的學生的teacher_id,尋找對應的老師!--> <select id="getStudent" resultMap="StudentTeacher"> select * from student </select> <resultMap id="StudentTeacher" type="Student"> <!--<result property="studentId" column="student_id"></result>--> <!--<result property="studentName" column="student_name"></result>--> <!--複雜的屬性,我們需要單獨處理 物件:association 集合:colLection--> <association property="teacher" column="teacher_id" javaType="Teacher" select="getTeacher"></association> </resultMap> <select id="getTeacher" resultType="Teacher"> select * from teacher where teacher_id=#{teacherId} </select>
10.4、按照結果巢狀處理
-
<!--按照結果巢狀處理--> <select id="getStudent2" resultMap="StudentTeacher2"> select s.student_id sid,s.student_NAME sname,t.teacher_name tname from student s,teacher t where s.teacher_id=t.teacher_id; </select> <resultMap id="StudentTeacher2" type="Student"> <result property="studentId" column="sid" ></result> <result property="studentName" column="sname" ></result> <association property="teacher" javaType="Teacher"> <result property="teacherName" column="tname"></result> </association> </resultMap>
-
回顧Mysql
- 多對一查詢方式:
- 子查詢。
- 聯表查詢。
- 多對一查詢方式: