Mybatis中Collection集合標(biāo)簽的使用詳解
mybatis簡(jiǎn)單的CURD就不用多說了,網(wǎng)上相關(guān)博客文檔一大堆。分析一下Mybatis里面的collection聚集查詢。
假設(shè)一個(gè)班級(jí)有多名學(xué)生為例,通過班級(jí)號(hào)查詢出該班級(jí)的信息,和班級(jí)里面的所有學(xué)生的信息,一般的做法就是通過班級(jí)號(hào)把班級(jí)的信息查詢出來,再通過班級(jí)ID號(hào)把該班級(jí)里面的所有學(xué)生查詢出來,我們不用這種通用的方法
1.班級(jí)實(shí)體類可以定義為這樣:
import java.util.List;
public class ClazzEntity {
private int clazzID;
private String clazzName;
private List<StudentEntity> studentList;
public int getClassID() {
return clazzID;
}
public int getClazzID() {
return clazzID;
}
public void setClazzID(int clazzID) {
this.clazzID = clazzID;
}
public String getClazzName() {
return clazzName;
}
public void setClazzName(String clazzName) {
this.clazzName = clazzName;
}
public List<StudentEntity> getStudentList() {
return studentList;
}
public void setStudentList(List<StudentEntity> studentList) {
this.studentList = studentList;
}
}
學(xué)生實(shí)體類定義:
package com.cn.hnust.pojo;
public class StudentEntity {
private int stuID;
private String stuName;
private int stuAge;
private String stuAddress;
public int getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuAddress() {
return stuAddress;
}
public void setStuAddress(String stuAddress) {
this.stuAddress = stuAddress;
}
}
2.數(shù)據(jù)庫建表語句:
CREATE TABLE student_t ( stuno INT PRIMARY KEY, stuname VARCHAR(20), stuage INT, stuaddress VARCHAR(20) , classid INT ); CREATE TABLE class_t ( classid INT PRIMARY KEY, classname VARCHAR(20) );
3.查詢ClazzEntity中的學(xué)生信息列表StudentEntity,通過mybatis中的collection標(biāo)簽來配置,其中,ofType是查詢返回的學(xué)生信息對(duì)應(yīng)的實(shí)體類,select為要執(zhí)行的查詢學(xué)生列表的查詢語句,mybatis的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.cn.hnust.dao.InfoManageDao" >
<resultMap id="ClazzResultMap" type="com.cn.hnust.pojo.ClazzEntity" >
<id column="classID" property="clazzID" jdbcType="INTEGER" />
<result column="className" property="clazzName" jdbcType="VARCHAR" />
<collection property="studentList" column="classID" javaType="ArrayList"
ofType="com.cn.hnust.pojo.StudentEntity" select="getStudentByClassID"/>
</resultMap>
<resultMap id="StudentResultMap" type="com.cn.hnust.pojo.StudentEntity">
<id property="stuID" column="stuID" />
<result property="stuName" column="stuName" />
<result property="stuAge" column="stuAge" />
<result property="stuAddress" column="stuAddress" />
</resultMap>
<select id="getClassByID" resultMap="ClazzResultMap" parameterType="java.lang.Integer" >
select classID,className
from class_t
where classID = #{clazzID}
</select>
<select id="getStudentByClassID" resultMap="StudentResultMap" parameterType="java.lang.Integer" >
select stuID,stuName,stuAge,stuAddress,classID
from student_t
where classID = #{clazzID}
</select>
</mapper>
這樣就可以查到一個(gè)班級(jí)的信息,和班級(jí)里面的所有學(xué)生信息:
ClazzEntity [clazzID=1, clazzName=junior, studentList=[StudentEntity [stuID=1001, stuName=wanghai, stuAge=18, stuAddress=beijing], StudentEntity [stuID=1002, stuName=zhangdong, stuAge=20, stuAddress=shanghai]]]
到此這篇關(guān)于Mybatis中Collection集合標(biāo)簽的使用詳解的文章就介紹到這了,更多相關(guān)Mybatis中Collection集合標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis中的collection兩種使用方法及效率比較
- mybatis?實(shí)現(xiàn)多層級(jí)collection嵌套
- 解決mybatis 中collection嵌套collection引發(fā)的bug
- 詳解mybatis中association和collection的column傳入多個(gè)參數(shù)問題
- mybatis利用association或collection傳遞多參數(shù)子查詢
- Mybatis中collection和association的使用區(qū)別詳解
- Mybatis使用collection標(biāo)簽進(jìn)行樹形結(jié)構(gòu)數(shù)據(jù)查詢時(shí)攜帶外部參數(shù)查詢
相關(guān)文章
在Mybatis中association標(biāo)簽多層嵌套的問題
這篇文章主要介紹了在Mybatis中association標(biāo)簽多層嵌套的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java實(shí)現(xiàn)中國(guó)象棋的示例代碼
中國(guó)象棋是起源于中國(guó)的一種棋,屬于二人對(duì)抗性游戲的一種,在中國(guó)有著悠久的歷史。由于用具簡(jiǎn)單,趣味性強(qiáng),成為流行極為廣泛的棋藝活動(dòng)。本文將利用Java實(shí)現(xiàn)這一經(jīng)典游戲,需要的可以參考一下2022-02-02
IDEA中JetBrains Mono字體的正確安裝姿勢(shì)
在 JetBrains Mono 的設(shè)計(jì)階段,它就充分考慮到了長(zhǎng)時(shí)間工作可能導(dǎo)致的眼睛疲勞問題,比如字母的大小和形狀、空間量、自然等寬平衡、不必要的細(xì)節(jié)、連字、以及難以區(qū)分的符號(hào)等,從而最終設(shè)計(jì)出了這么一款字體2021-06-06
SpringBoot整合spring-retry實(shí)現(xiàn)接口請(qǐng)求重試機(jī)制及注意事項(xiàng)
今天通過本文給大家介紹我們應(yīng)該如何使用SpringBoot來整合spring-retry組件實(shí)現(xiàn)重試機(jī)制及注意事項(xiàng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08
java類中使用Jfreechart的簡(jiǎn)單實(shí)例
這篇文章介紹了java類中使用Jfreechart的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-08-08
Springboot搭建JVM監(jiān)控(Springboot + Prometheus +&n
在應(yīng)用開發(fā)時(shí),監(jiān)控報(bào)警必不可少,本文主要介紹了Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana),具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
springboot整合redis實(shí)現(xiàn)發(fā)送郵箱并驗(yàn)證
大家好,本篇文章主要講的是springboot整合redis實(shí)現(xiàn)發(fā)送郵箱并驗(yàn)證,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01

