国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Mybatis中Collection集合標簽的使用詳解

瀏覽:135日期:2023-10-23 18:33:28

mybatis簡單的CURD就不用多說了,網上相關博客文檔一大堆。分析一下Mybatis里面的collection聚集查詢。 假設一個班級有多名學生為例,通過班級號查詢出該班級的信息,和班級里面的所有學生的信息,一般的做法就是通過班級號把班級的信息查詢出來,再通過班級ID號把該班級里面的所有學生查詢出來,我們不用這種通用的方法

1.班級實體類可以定義為這樣:

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; }}

學生實體類定義:

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.數據庫建表語句:

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中的學生信息列表StudentEntity,通過mybatis中的collection標簽來配置,其中,ofType是查詢返回的學生信息對應的實體類,select為要執行的查詢學生列表的查詢語句,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 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 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 resultMap='ClazzResultMap' parameterType='java.lang.Integer' > select classID,className from class_t where classID = #{clazzID}</select><select resultMap='StudentResultMap' parameterType='java.lang.Integer' > select stuID,stuName,stuAge,stuAddress,classID from student_t where classID = #{clazzID}</select></mapper>

這樣就可以查到一個班級的信息,和班級里面的所有學生信息:

ClazzEntity [clazzID=1, clazzName=junior, studentList=[StudentEntity [stuID=1001, stuName=wanghai, stuAge=18, stuAddress=beijing], StudentEntity [stuID=1002, stuName=zhangdong, stuAge=20, stuAddress=shanghai]]]

到此這篇關于Mybatis中Collection集合標簽的使用詳解的文章就介紹到這了,更多相關Mybatis中Collection集合標簽內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 亚洲精品一二三四区 | 免费观看久久 | 可以免费看黄色的网站 | 精品无人区一区二区三区a 精品午夜国产在线观看不卡 | 青青草国产免费一区二区 | 成人黄页| 性视频亚洲 | 亚洲精品视频在线观看视频 | 日韩在线高清视频 | 99精品热女视频专线 | 美女扒开双腿让男人桶 | 6080伦理久久亚洲精品 | 亚洲女人在线 | 久久国内精品自在自线400部o | 国产激情一区二区三区在线观看 | 特及毛片 | 欧美一级大黄特黄毛片视频 | 国内自拍网站 | 美女黄视频在线观看 | 亚洲精品无码专区在线播放 | 日韩一级欧美一级在线观看 | 亚洲线精品一区二区三区 | 成人69 | 好吊操这里只有精品 | 成人a毛片在线看免费全部播放 | 亚洲欧美天堂 | www.91亚洲| 欧美精品hdvdeosex4k | 亚洲天堂网站在线 | 欧美丝袜xxxxx在线播放 | 国产精品怡红院在线观看 | 国产乱纶| 亚洲天堂美女视频 | 日本欧美色 | 国产精品麻豆一区二区三区v视界 | 久久黄色影片 | 欧美成人高清在线视频大全 | 91色综合综合热五月激情 | 在线不卡一区二区三区日韩 | heyzo北条麻妃久久 | 精品一区二区三区免费毛片爱 |