亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis如何使用注解實現(xiàn)一對多關聯(lián)查詢

 更新時間:2021年07月09日 08:44:27   作者:CoderYin  
這篇文章主要介紹了mybatis如何使用注解實現(xiàn)一對多關聯(lián)查詢的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

mybatis 注解實現(xiàn)一對多關聯(lián)查詢

@Select("select id,mockexam_section as section,id as sectionId"
			+ " from t_p_qb_mockexam_section"
			+ " where mockexam_charpter_id = #{charpterId} and is_delete = 0"
			+ " order by mockexam_section_idx asc")
@Results({
@Result(property = "questionList",column = "sectionId",many = @Many(select = "com.zikaoshu.baseinfo.mapper.BaseinfoQuestionMapper.listQuestionResDto"))})
List<SectionQuestionDto> listSectionQuestionDto(@Param("charpterId") Integer charpterId);
	
	
@Select("select id,type,discuss_title as discussTitle,stem1,material,a,b,c,d,e,answer,analysis,mockeaxm_section_id as sectionId"
			+ " from t_p_qb_question_mockexam"
			+ " where mockeaxm_section_id = #{id} and is_delete = 0"
			+ " order by q_sequence,gmt_create asc")
List<QuestionResDto> listQuestionResDto(@Param("id") Integer id);

mybatis多對多查詢(xml方式和注解方式)

前面總結了一對一,多對一和一對多的多表查詢,今天總結一下多對多的mybatis多表查詢。同樣有xml方式和注解方式,步驟和前兩種查詢差不多,最主要的區(qū)別就在表和sql語句上了。

數(shù)據(jù)庫表及關系

這里采用用戶和角色的例子

一個用戶可以有多個角色

一個角色可以賦予多個用戶

在進行多表查詢時,我們需要一張中間表,中間表中包含各自的主鍵,在中間表中是外鍵。

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

多對多查詢(xml方式)

這次我們首先清理一下思路,我們先在數(shù)據(jù)庫里把我們需要的數(shù)據(jù)查出來再寫代碼。

我們查詢用戶時要同時查出其對應的角色,借助中間表,根據(jù)UID查詢RID,再根據(jù)RID查詢角色表,中間表的數(shù)據(jù)我們不需要,所以不顯示。

這里我們可以用左外連接來進行多表的查詢,查詢所有用戶,用戶有角色信息就連接到該用戶后面,沒有則為空。

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 ur.rid = r.id

在這里插入圖片描述

當我們查詢角色想要得到相應的用戶時道理是一樣的,SQL語句也只要換一下連接順序。

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 ur.uid = u.id

在這里插入圖片描述

查詢出來結果后剩下的內(nèi)容就很簡單。

在User和role里加入多對多實體映射

public class Role implements Serializable {
    private String roleId;
    private String roleName;
    private String roleDesc;
    //多對多映射關系,一個角色有多個用戶
    private List<User> users;
    public List<User> getUsers() {
        return users;
    }
    public void setUsers(List<User> users) {
        this.users = users;
    }
    public String getRoleId() {
        return roleId;
    }
    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    public String getRoleDesc() {
        return roleDesc;
    }
    public void setRoleDesc(String roleDesc) {
        this.roleDesc = roleDesc;
    }
    @Override
    public String toString() {
        return "role{" +
                "roleId='" + roleId + '\'' +
                ", roleName='" + roleName + '\'' +
                ", roleDesc='" + roleDesc + '\'' +
                '}';
    }
}
public class User implements Serializable{
    private Integer id;
    private String username;
    private String address;
    private String sex;
    private Date birthday;
    //多對多映射關系,一個用戶具備多個角色
    private List<Role> roles;
    public List<Role> getRoles() {
        return roles;
    }
    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", address='" + address + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                '}';
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

然后配置xml,配置映射封裝和sql語句

<!--定義resultMap-->
    <resultMap id="userWithRole" type="user">
        <id property="id" column="id"></id>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>
        <!--配置角色映射-->
        <collection property="roles" ofType="role">
            <id property="roleId" column="rid"></id>
            <result property="roleName" column="role_name"></result>
            <result property="roleDesc" column="role_desc"></result>
        </collection>
    </resultMap>
    <!--查詢所有用戶信息-->
    <select id="findAll" resultMap="userWithRole">
        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 ur.rid = r.id
    </select>
<resultMap id="roleUserMap" type="role">
        <id property="roleId" column="rid"></id>
        <result property="roleName" column="role_name"></result>
        <result property="roleDesc" column="role_desc"></result>
        <collection property="users" ofType="user">
            <id property="id" column="id"></id>
            <result property="username" column="username"></result>
            <result property="address" column="address"></result>
            <result property="sex" column="sex"></result>
            <result property="birthday" column="birthday"></result>
        </collection>
    </resultMap>
    <!--查詢所有角色信息-->
    <select id="findAll" resultMap="roleUserMap">
        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 ur.uid = u.id
    </select>

測試結果

在這里插入圖片描述在這里插入圖片描述

注解方式

思路是一樣的,但我們使用注解時,不能像xml方式一樣只使用一條sql語句完成直接封裝,所以這里要按上面說的思路完成分步查詢。

public interface IUserDao {
    /**
     * 查詢所有操作,并攜帶賬戶信息
     * @return
     */
    @Select("select * from user")
    @Results(id = "userRoleMap",value = {
            //id表示主鍵
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "address",property = "address"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "birthday",property = "birthday"),
            @Result(property = "roles",column = "id",many = @Many(select = "com.itcc.dao.IRoleDao.findByUid",fetchType = FetchType.LAZY))
    })
    List<User> findAll();
    /**
     * 根據(jù)id查詢一個用戶
     * @param rid
     */
    @Select("select * from user where id in(select uid from user_role where rid = #{rid})")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "address",property = "address"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "birthday",property = "birthday")
    })
    List<User> findByRId(Integer rid);
}
public interface IRoleDao {
    /**
     * 查詢所有角色信息
     * @return
     */
    @Select("select * from role")
    @Results({
            @Result(id = true,column = "id",property = "roleId"),
            @Result(column = "role_name",property = "roleName"),
            @Result(column = "role_desc",property = "roleDesc"),
            @Result(property = "users",column = "id",many = @Many(select = "com.itcc.dao.IUserDao.findByRId",fetchType = FetchType.LAZY))
    })
    List<Role> findAll();
    @Select("select * from role where ID in(select rid from user_role where uid = #{uid})")
    @Results({
            @Result(id = true,column = "id",property = "roleId"),
            @Result(column = "role_name",property = "roleName"),
            @Result(column = "role_desc",property = "roleDesc")
    })
    List<Role> findByUid(String uid);
}

最終的測試結果和上面一樣。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 面試突擊之跨域問題的解決方案詳解

    面試突擊之跨域問題的解決方案詳解

    跨域問題本質(zhì)是瀏覽器的一種保護機制,它的初衷是為了保證用戶的安全,防止惡意網(wǎng)站竊取數(shù)據(jù)。那怎么解決這個問題呢?接下來我們一起來看
    2022-09-09
  • 深入解析Spring Bean初始化時和銷毀時的擴展點

    深入解析Spring Bean初始化時和銷毀時的擴展點

    在Bean進行初始化或者銷毀的時候,如果我們需要做一些操作,比如加載和銷毀一些資源或者執(zhí)行一些方法時,那么就可以使用Spring提供的一些擴展,今天主要分享初始化Bean時的三種方式和銷毀Bean時的三種方式,需要的朋友可以參考下
    2023-08-08
  • springboot中.yml文件的值無法讀取的問題及解決

    springboot中.yml文件的值無法讀取的問題及解決

    這篇文章主要介紹了springboot中.yml文件的值無法讀取的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java Applet查找素數(shù)小程序代碼實例

    Java Applet查找素數(shù)小程序代碼實例

    這篇文章主要介紹了Java Applet查找素數(shù)小程序代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot讀取外部配置文件的方法

    SpringBoot讀取外部配置文件的方法

    這篇文章主要介紹了SpringBoot讀取外部配置文件的方法,以端口配置為例,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • 搭建MyBatis-Plus框架并進行數(shù)據(jù)庫增刪改查功能

    搭建MyBatis-Plus框架并進行數(shù)據(jù)庫增刪改查功能

    這篇文章主要介紹了搭建MyBatis-Plus框架并進行數(shù)據(jù)庫增刪改查,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • SpringBoot接口中如何直接返回圖片數(shù)據(jù)

    SpringBoot接口中如何直接返回圖片數(shù)據(jù)

    這篇文章主要介紹了SpringBoot接口中如何直接返回圖片數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java Swing實現(xiàn)簡單的體重指數(shù)(BMI)計算器功能示例

    Java Swing實現(xiàn)簡單的體重指數(shù)(BMI)計算器功能示例

    這篇文章主要介紹了Java Swing實現(xiàn)簡單的體重指數(shù)(BMI)計算器功能,涉及Java Swing窗口組件布局、響應及數(shù)值運算相關操作技巧,需要的朋友可以參考下
    2017-12-12
  • Java8新特性之Stream API詳解

    Java8新特性之Stream API詳解

    這篇文章主要介紹了Java8新特性之StreamAPI詳解,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 淺談三分鐘學習Java泛型中T、E、K、V、?的含義

    淺談三分鐘學習Java泛型中T、E、K、V、?的含義

    這篇文章主要介紹了淺談三分鐘學習Java泛型中T、E、K、V、?的含義,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12

最新評論