Mybatis關(guān)聯(lián)查詢之一對多和多對一XML配置詳解
平時在開發(fā)過程中dao、bean和XML文件都是自動生成的,很少寫XML的配置關(guān)系,今天記錄一下mybatis的關(guān)聯(lián)查詢中的多對一和一對多的情況。
首先是有兩張表(學(xué)生表Student和老師Teacher表,注:這里只是為了演示一對多和多對一的情況,請不要杠),為了更易懂,這里只設(shè)置了最簡單的幾個必要字段。表結(jié)構(gòu)如下圖
Student表:
Teacher表:
創(chuàng)建實體bean
Teacher.java:
import java.util.List; public class Teacher { private Integer id; private String name; private String className; private List<Student> students; // get、set方法省略 }
Sfudent.java
public class Student { private Integer id; private String name; private Integer teacherId; private String className; private Teacher teacher; // get、set方法省略 }
下面重點來了:配置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.tz.mybatis.dao.studentDao"> <!-- ///一對多的第一種寫法/// --> <resultMap type="Teacher" id="teacherMap"> <id column="id" property="id"/> <result column="name" property="name"/> <collection property="students" ofType="Student" column="id"> <!-- 這里的column對應(yīng)的是下面查詢的別名,而不是表字段名 --> <id column="sid" property="id"/> <!-- property對應(yīng)JavaBean中的屬性名 --> <result column="sname" property="name"/> <result column="className" property="className"/> </collection> </resultMap> <!-- 查詢所有的老師級各自的所有學(xué)生 --> <select id="getTeachers" parameterType="Teacher" resultMap="teacherMap"> SELECT t.id, t.NAME, t.class_Name, s.id AS sid, s. NAME AS sname, s.class_name as className FROM teacher t LEFT JOIN student s ON t.id = s.teacher_id </select> </mapper>
測試類:
package com.tz.test; import java.io.IOException; import java.io.InputStream; import java.util.List; 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.Before; import org.junit.Test; import com.tz.mybatis.bean.Student; import com.tz.mybatis.bean.Teacher; public class TeacherTest { private SqlSessionFactory sqlSessionFactory; @Before public void init() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void getTeachers() { SqlSession session = sqlSessionFactory.openSession(); List<Teacher> list = session.selectList("com.tz.mybatis.dao.studentDao.getTeachers"); System.out.println(list); } }
下面給出第二種寫法:
<!-- //一對多的第二種寫法/ --> <resultMap type="Teacher" id="teacherMaps"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="class_name" property="className"/> <collection property="students" ofType="Student" select="getStudents" column="id"> </collection> </resultMap> <!-- 查詢所有的老師級各自的所有學(xué)生 --> <select id="getAllTeacher" parameterType="Teacher" resultMap="teacherMaps"> SELECT t.id, t.NAME, t.class_name FROM teacher t </select> <select id="getStudents" parameterType="int" resultType="Student"> select s.id, s. NAME, s.class_name as className from student s where teacher_id = #{id} </select>
測試類:
@Test public void getTeachers2() { SqlSession session = sqlSessionFactory.openSession(); List<Teacher> list = session.selectList("com.tz.mybatis.dao.studentDao.getAllTeacher"); System.out.println(list); }
查詢學(xué)生信息(多對一):
首先還是配置文件:
<resultMap type="Student" id="studentMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="class_name" property="className"/> <result column="teacher_id" property="teacherId"/> <association property="teacher" select="getTeacher" column="teacher_id" javaType="Teacher"> <!-- 這里要注意的是column對應(yīng)的是student中的外鍵,而且需是表字段名 --> </association> </resultMap> <select id="getStudent" resultMap="studentMap"> SELECT s.id, s.name, s.class_name, s.teacher_id FROM student s </select> <select id="getTeacher" resultType="Teacher" parameterType="int"> SELECT t.id, t.name, t.class_name as className FROM teacher t where id = #{teacher_id} </select>
測試類:
@Test public void getStudents() { SqlSession session = sqlSessionFactory.openSession(); List<Student> list = session.selectList("com.tz.mybatis.dao.studentDao.getStudent"); System.out.println(list); }
最后:當(dāng)然如果不想配置這么麻煩的信息,可以直接寫一個關(guān)聯(lián)查詢的SQL語句,返回結(jié)果直接由Map接受即可。不過這樣就不太符合面向?qū)ο蟮睦砟盍恕?nbsp;
到此這篇關(guān)于Mybatis關(guān)聯(lián)查詢之一對多和多對一XML配置詳解的文章就介紹到這了,更多相關(guān)Mybatis關(guān)聯(lián)查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot熔斷機制之CircuitBreaker詳解
這篇文章主要介紹了SpringBoot熔斷機制之CircuitBreaker詳解,SpringBoot的熔斷機制在微服務(wù)架構(gòu)中扮演著重要角色,其中CircuitBreaker是其核心機制之一,用于防止服務(wù)的異常狀態(tài)影響到整個系統(tǒng)的運作,需要的朋友可以參考下2023-10-10MyBatis/mybatis-plus項目打印SQL的方法實現(xiàn)
SpringBoot項目中,經(jīng)常需要打印SQL語句及其參數(shù),本文就來介紹一下MyBatis/mybatis-plus項目打印SQL的方法實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-07-07java servlet結(jié)合mysql搭建java web開發(fā)環(huán)境
之前寫過一篇 servlet+oracle的文章,但是那是因為公司有可能接那么一個項目,然后我當(dāng)時也比較閑,所以隨便學(xué)了下,那玩意是白去研究了,因為公司后面并沒接到那項目。2015-12-12