六 多對多查詢

2021-09-24 18:02:55 字數 3367 閱讀 9516

實體類:

public

class

role

implements

serializable';

}}

public

class

user

implements

serializable';

}}

測查詢所有角色,同時獲取每個角色的使用者資訊

<?xml version="1.0" encoding="utf-8"?>

namespace

="com.liaoxiang.dao.iroledao"

>

"rolemap"

type

="role"

>

property

="roleid"

column

="rid"

>

id>

property

="rolename"

column

="role_name"

>

result

>

property

="roledesc"

column

="role_desc"

>

result

>

property

="users"

oftype

="user"

>

column

="id"

property

="id"

>

id>

column

="username"

property

="username"

>

result

>

column

="address"

property

="address"

>

result

>

column

="***"

property

="***"

>

result

>

column

="birthday"

property

="birthday"

>

result

>

collection

>

resultmap

>

"findall"

resultmap

="rolemap"

>

select u.*,r.id as rid,r.role_name,r.role_desc from role r

left outer join user_role ur on r.id = ur.rid

left outer join user u on u.id = ur.uid

select

>

>

@test

public

void

testfindall()

}角色資訊:role

使用者資訊:[user

, user

]角色資訊:role

使用者資訊:[user

]角色資訊:role

使用者資訊:[

]

查詢使用者資訊,同時查詢出每個使用者的角色資訊

<?xml version="1.0" encoding="utf-8"?>

namespace

="com.liaoxiang.dao.iuserdao"

>

"usermap"

type

="user"

>

property

="id"

column

="id"

>

id>

property

="username"

column

="username"

>

result

>

property

="address"

column

="address"

>

result

>

property

="***"

column

="***"

>

result

>

property

="birthday"

column

="birthday"

>

result

>

property

="roles"

oftype

="role"

>

property

="roleid"

column

="rid"

>

id>

property

="rolename"

column

="role_name"

>

result

>

property

="roledesc"

column

="role_desc"

>

result

>

collection

>

resultmap

>

"findall"

resultmap

="usermap"

>

select u.*,r.id as rid,r.role_name,r.role_desc from user u

left outer join user_role ur on u.id = ur.uid

left outer join role r on r.id = ur.rid

select

>

>

@test

public

void

testfindall()

}使用者資訊:user

使用者角色:[role

, role

]使用者資訊:user

使用者角色:[role

]使用者資訊:user

使用者角色:[

]使用者資訊:user

使用者角色:[

]

Hibernate多對多查詢

乙個老師教許多學生,乙個學生被許多老師教,乙個學生有好多書,同一種書被許多同學擁有.要查詢教擁有書 a 的學生的老師 hql如何寫呀?如何取值?classteacher classstudent classbook hql語句 select t from teacher t join t.stude...

mybatis 多對多查詢

查詢使用者及使用者購買商品資訊。查詢主表是 使用者表 user 關聯表 由於使用者和商品沒有直接關聯,通過訂單和訂單明細進行關聯,所以關聯表是 orders orderdetail items select orders.order表的唯一標識 user表的唯一標識 user.username,us...

django model 多對多查詢

看一下自己的models檔案 class hostlist models.model ip models.ipaddressfield unique true,verbose name u ip位址 hostname models.charfield max length 30,verbose na...