mybatis聯合查詢


1.有學生實體

@Component
@Scope("prototype")
public class StudentInfo {
    private Integer studentId;//學號
    private String studentName;//學生姓名
    private String studentAccount;//學生賬號
    private String studentPwd;//學生密碼
    private ClassInfo classInfo;//所在班級
    private GradeInfo grade;//所在年級
}

  班級對象和年級對象是學生實體的屬性

 2.有數據接口

public StudentInfo getStudentByAccountAndPwd(String studentAccount);

 3.數據接口實現

 3.1 定義resultmap

為什么要定義resultmap

告訴mybatis將從結果集中取出的數據轉換成開發者需要的對象

resultMap="queryStudent"

 表示引用定義好的resultmap進行數據庫表和返回類型對象的映射

<resultMap type="com.taohan.online.exam.po.StudentInfo" id="queryStudent">
   <id column="studentId" property="studentId"/>
   <result column="studentName" property="studentName"/>
   <result column="studentAccount" property="studentAccount"/>
   <result column="studentPwd" property="studentPwd"/>
</resultmap>

id:resultMap的唯一標識符號

type:resultmap實際返回的類型

id:表示主鍵

column:表示數據庫表的列名,property表示數據庫列映射到返回類型的屬性,這種情況可以解決數據庫字段和實體類屬性不匹配的問題

因為class和grade對象是學生對象的屬性,所以使用resultmap去映射返回類型

<association property="classInfo" javaType="com.taohan.online.exam.po.ClassInfo">			
   <id column="classId" property="classId"/>
<result column="className" property="className"/> </association>

column:數據庫的列名

property:返回類型Student的屬性名classId

javaType:該屬性對應的類型名稱,本次表示的是ClassInfo類型

<resultMap type="com.taohan.online.exam.po.StudentInfo" id="queryStudent">
<id column="studentId" property="studentId"/>
<result column="studentName" property="studentName"/>
<result column="studentAccount" property="studentAccount"/>
<result column="studentPwd" property="studentPwd"/> <association property="classInfo" javaType="com.taohan.online.exam.po.ClassInfo">
<id column="classId" property="classId"/>
<result column="className" property="className"/> </association> <association property="grade" javaType="com.taohan.online.exam.po.GradeInfo"> <id column="gradeId" property="gradeId"/> <result column="gradeName" property="gradeName"/> </association> </resultMap>

 3.2 查詢數據

<select id="getStudentByAccountAndPwd" parameterType="java.lang.String" resultMap="queryStudent">
	SELECT a.*,b.className,c.gradeId,c.gradeName FROM StudentInfo a
	INNER JOIN ClassInfo b ON a.classId=b.classId
	INNER JOIN GradeInfo c ON b.gradeId=c.gradeId
	WHERE studentAccount=#{studentAccount}
</select>

 3.3 多表連接

SELECT a.*,b.className,c.gradeId,c.gradeName FROM StudentInfo a
INNER JOIN ClassInfo b ON a.classId=b.classId
INNER JOIN GradeInfo c ON b.gradeId=c.gradeId

 查詢學生表的所有信息,b表的班級名,c表的年級號,c表的年級名

內連接INNER JOIN:在表中存在至少一個匹配時,INNER JOIN 關鍵字返回行。

4.一般的一個學生只對應一個班級,但是一個班級往往對應很多學生,所以有

private List<Student> students;//setter,getter略

 所以存在mapper

<resultMap type="com.taohan.online.exam.po.ClassInfo" id="queryStudent">
<id column="classId" property="classId"/> <result column="className" property="className"/> <collection
<!-- 返回的屬性名students-->
property="students"
<!--返回的實例一個集合-->
javaType="ArrayList"
<!--表示使用參數id作為參數進行之后的select語句查詢-->
column="id"
<!-- 表示集合中的類型-->
ofType="org.taohan.online.exam.po.StudentInfo> <!--表示執行一條select語句-->
<!--select="selectStudentWithId"/> </resultMap>
<select id="selectStudentWithId" resultType="org.taohan.online.exam.po.StudentInfo">
select * from tb_student where classId=#{id}
</select>
<select id=selectClazz" resultMap="clazzReultMap">
select * from tb_clazz
<select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM