1. 程式人生 > >mybatis的collection一對多巢狀查詢

mybatis的collection一對多巢狀查詢

mybatis的collection對映

剛才在csdn上提了一個問題

三張表:

create table stu(
  sno int auto_increment primary key ,
  sname varchar(10)
);
create table class (
  cid   int auto_increment primary key,
  cname varchar(10)
); 
create table cs (
  cid int,
  sno int,
  foreign key(cid) references class(cid),
  foreign
key(sno) references stu(sno) );

資料:

insert into stu values (0,'stu1');
insert into stu values (0,'stu2');
insert into stu values (0,'stu3');
insert into stu values (0,'stu4');
insert into stu values (0,'stu5');

insert into class values (0,'class1');
insert into class values (0,'class2');

insert into
cs values (1,1);
insert into cs values (1,2); insert into cs values (2,3); insert into cs values (2,4); insert into cs values (2,5);

每個班級有多個學生

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.iails.site.dao.ClazzMapper"> <!-- 第一種方式 --> <resultMap id="getList2ResultMap" type="com.iails.site.entity.Clazz"> <id property="classId" column="cid"/> <result property="className" column="cname"/> <collection property="students" column="cid" ofType="com.iails.site.entity.Student" select="getList2Student"> <id property="studentId" column="sno"/> <result property="studentName" column="sname"/> </collection> </resultMap> <select id="getList2Student" resultType="com.iails.site.entity.Student"> select * from stu,cs where stu.sno=cs.sno and #{cid}=cs.cid </select> <select id="getList2" resultMap="getList2ResultMap"> select * from class </select> <!-- 第二種方式 --> <resultMap id="getListResultMap" type="com.iails.site.entity.Clazz"> <id property="classId" column="cid"/> <result property="className" column="cname"/> <collection property="students" column="cid" ofType="com.iails.site.entity.Student"> <id property="studentId" column="sno"/> <result property="studentName" column="sname"/> </collection> </resultMap> <select id="getList" resultMap="getListResultMap"> select * from stu, class, cs where class.cid=cs.cid and stu.sno=cs.sno </select> </mapper>

問題是:第二種方式沒問題,第一種方法查詢出來Class物件的Students列表有值,數目也符合,但是值為null

[{"classId":1,"className":"class1","students":[null, null]}, {"classId":2,"className":"class2","students":[null, null, null]}]

並且如果Student的屬性名和stu的欄位名完全一致或者把ofType和getList2Student的resultType改為”map”又可以對映成功。

最後

    <!-- 第三種方式 -->
    <resultMap id="getList3ResultMap" type="com.iails.site.entity.Clazz">
        <id property="classId" column="cid"/>
        <result property="className" column="cname"/>
        <collection property="students" column="cid" ofType="com.iails.site.entity.Student"
                    select="getList3Student">

        </collection>
    </resultMap>

    <resultMap id="getList3StudentResultMap" type="com.iails.site.entity.Student">
        <id property="studentId" column="sno"/>
        <result property="studentName" column="sname"/>
    </resultMap>

    <select id="getList3Student" resultMap="getList3StudentResultMap">
        select *
        from stu,cs
        where stu.sno=cs.sno and #{cid}=cs.cid
    </select>
    <select id="getList3" resultMap="getList3ResultMap">
        select *
        from class
    </select>
  • 也就是說:巢狀查詢時,在collection內部的property和colum不一致時需要為這個對映關係另外定義resultmap

坑。。我的積分啊

相關推薦

mybatis的collection一對查詢

mybatis的collection對映 剛才在csdn上提了一個問題 三張表: create table stu( sno int auto_increment primary key , sname varchar(10) ); cre

mybatis 高階結果對映關聯的查詢一對查詢

這是mybatis的官方例子,基本上看一遍就會了,一定要先去看官方例子,這裡介紹的不詳細 http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#select 我自己用的聯表查詢 實體類 public class MemberSaleR

Mongoose使用populate進行及深層查詢功能

mongoose populate 文件欄位說明 path «Object|String» 需要做表關聯的欄位路徑,或者包含所有引數的物件 [select] «Object|String» 表關聯查詢要選擇的欄位 [model] 

mongodb查詢

> db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } ) { "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "w

laravel條件查詢(and,or查詢)

說明 在日常開發中,經常會需要寫多條件的資料庫查詢語句。在使用框架的情況下,單純使用原生sql查詢會導致結果與model無法對應,也就沒有辦法使用框架的一些便利的方法對結果集進行處理。尤其是laravel提供了非常多的對查詢結果集進行處理的工具。所以最好是使用

簡單的一對的sql查詢

log join nbsp 查詢 一個 clas ood brush 信息 一個品牌表 一個分類表 中外鍵為品牌的的id 查詢品牌的信息 相應的查出品牌下分類數量 sql 原sql: SELECT b.id, b. NAME, ( SELECT COUNT(*) F

hibernate 一對自關聯查詢(如選單表)

hibernate 有自帶的選單查詢功能,當查詢的節點有幾千條時,hibernate自帶的選單查詢會造成卡頓的情況,但一般情況下hibernate自帶的選單查詢減少程式設計師的程式碼量, 簡化了程式碼 相關測試資料(MySQL) -- 一對多雙向自關聯 -- 選單表 -- t_hib

【筆記】Mybatis高階查詢(小結)--查詢及延遲載入

<association>與<collection>標籤一對一、一對多,多對多查詢時用到的屬性 property:對應實體類中的屬性名,必填項。 javaType:屬性對應的型別。 resultMap:可以直接使用現有

【筆記】Mybatis高階查詢(五)--使用resultMap的<collection>進行查詢及延遲載入

下面例子通過<collection>實現一個通過使用者編號查詢使用者下面的角色及許可權的需求,支援延遲載入。下面以自下而上的過程來實現這樣的巢狀查詢功能。並且這個自下而上的過程中每一個方法都是獨立可用的方法。上層的結果都以下層方法為基礎。所有物件都設定為延遲載入。

【筆記】Mybatis高階查詢(三)--使用<association>標籤實現查詢及延遲載入

<association>標籤實現巢狀查詢,需要用到以下屬性: select:另一個對映查詢的ID,Mybatis會額外執行這個查詢獲取巢狀物件的結果。 column:列名或別名,將主查詢中列的結果作為巢狀查詢的引數,配置方式如column=

QSL match_phrase 查詢

match_phrase 巢狀查詢 { "query": { "bool": { "should": [ { "match_phrase": { "body": { "quer

MySQL的查詢

定義我就不說了,子查詢就是內部的查詢,包含子查詢就是外部查詢!也就是說巢狀查詢一個是內部查詢一個是外部查詢,而且查詢是由內向外的。 提示一下:在group by和order by中的子查詢是沒有意義的! 子查詢的分類: 標量子查詢—返回單個值的查詢 列子查詢—返

mybatis之查詢association和collection

Mybatis association是用於一對一和多對一,而collection是用於一對多的關係 一. Association 1. 巢狀查詢實現association一對一 public class Card implements Serializable{ private In

django 外來鍵查詢 一對 通過物件查詢和通過filter values 雙下劃線查詢

表結構: from django.db import models class Book(models.Model): name = models.CharField(max_length=32) price = models.IntegerField() pub_d

MySQL子查詢 查詢

子查詢:巢狀在其他查詢中的查詢。 有三張表分別如下: customers: 儲存顧客資訊 orderitems:只儲存訂單資訊,無客戶資訊 orders:儲存訂單號和顧客id   注意:一般在子查詢中,程式先執行在巢狀在最內層的語句,再執行外層。因此在寫子查

sql查詢再進行連線

SELECT * from ( SELECT * from tit_training_program WHERE level_id=#{levelId}) as p LEFT JOIN( se

mongoDB查詢

文件如下: {         "_id" : ObjectId("578f2122774326567ca82dfe"),         "person" : {                

Mongodb查詢及修改

Mongodb各文件中對巢狀查詢的介紹不知道藏在哪個地方,反正我是沒找到,一個偶然的機會發現網上的一個帖子,終於知道了巢狀查詢的用法。於是乎我們應用中的一個問題也隨之被解決了。不說廢話了,現在說下巢狀查詢的使用。 假設mongodb中存在某個collections,其資料如

Java實現Mongo查詢

我們可能都遇到過這樣的困擾,即如何巢狀查詢,舉個簡單的例子: { "_id" : ObjectId("56e8d3dee4b0c105488c3bfa"), "_class" : "com.x

【Mybatis】一對一與一對的關聯查詢

一、一對一關聯查詢 背景: 表1:user包含id,username。 表2:orders包含id,user_id 需求:查詢每個使用者下的訂單ID。( 現實中是一對多的關係,即一個使用者對應著多個訂單,主要是學習mybatis,這裡只是借用舉例) sql: select