六、MyBatis多表查詢

1.一對一的查詢(查詢一個訂單,與此同時查詢出該訂單所屬的用戶)

數據庫表:

User實體類:

    private int id;
    private String username;
    private String password;
    private Date birthday;

    //描述的是當前用戶存在哪些訂單
    private List<Order> orderList;

Order實體類:

    private int id;
    private Date ordertime;
    private Double total;
    private User user; // 當前訂單屬於那一個用戶

sql語句:

SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id

查詢結果:

OrderMapper.xml配置:

<mapper namespace="com.example.mapper.OrderMapper">

    <resultMap id="orderMap" type="order">
    <!--手動指定字段與實體屬性的映射關係
        column:數據表的字段名稱
        property:實體的屬性名稱
    -->
        <id column="oid" property="id"></id>
        <result column="ordertime" property="ordertime"></result>
        <result column="total" property="total"></result>
        <result column="uid" property="user.id"></result>
        <result column="username" property="user.username"></result>
        <result column="password" property="user.password"></result>
        <result column="birthday" property="user.birthday"></result>
    </resultMap>

    <select id="findAll" resultMap="orderMap">
        SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
    </select>

</mapper>

或者

OrderMapper.xml配置:

<mapper namespace="com.example.mapper.OrderMapper">

    <resultMap id="orderMap" type="order">
    <!--手動指定字段與實體屬性的映射關係
        column:數據表的字段名稱
        property:實體的屬性名稱
    -->
        <id column="oid" property="id"></id>
        <result column="ordertime" property="ordertime"></result>
        <result column="total" property="total"></result>

        <!--
            property:當前實體(order)中的屬性名稱(private User user)
            javaType:當前實體(order)中的屬性類型(User)
        -->
        <association property="user" javaType="user">
            <id column="uid" property="id"></id>
            <result column="username" property="username"></result>
            <result column="password" property="password"></result>
            <result column="birthday" property="birthday"></result>
        </association>
    </resultMap>

    <select id="findAll" resultMap="orderMap">
        SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
    </select>

</mapper>

測試結果:

Order{id=1, ordertime=Mon Jul 06 00:00:00 CST 2020, total=2000.0, user=User{id=1, username='zhangsan', password='123', birthday=null, orderList=null}}
Order{id=2, ordertime=Tue Jul 07 00:00:00 CST 2020, total=3000.0, user=User{id=1, username='zhangsan', password='123', birthday=null, orderList=null}}
Order{id=3, ordertime=Mon Jul 06 00:00:00 CST 2020, total=2898.0, user=User{id=2, username='lisi', password='123', birthday=null, orderList=null}}

2.一對多的查詢(查詢一個用戶,與此同時查詢出該用戶具有的訂單)

sql語句:

SELECT *,o.id oid FROM USER u,orders o WHERE u.id=o.uid

查詢結果: 

UserMapper.xml配置:

<mapper namespace="com.example.mapper.UserMapper">

    <resultMap id="userMap" type="user">
        <id column="uid" property="id"></id>
        <result column="username" property="username"></result>
        <result column="password" property="password"></result>
        <result column="birthday" property="birthday"></result>
    <!--    配置集合信息
        property:集合名稱
        ofType:當前集合中的數據類型
    -->
        <collection property="orderList" ofType="order">
        <!--封裝order的數據-->
            <id column="oid" property="id"></id>
            <result column="ordertime" property="ordertime"></result>
            <result column="total" property="total"></result>
        </collection>
    </resultMap>

    <select id="findAll" resultMap="userMap">
        SELECT *,o.id oid FROM USER u,orders o WHERE u.id=o.uid
    </select>

</mapper>

 測試結果: 

User{id=1, username='zhangsan', password='123', birthday=null, orderList=[Order{id=1, ordertime=Mon Jul 06 00:00:00 CST 2020, total=2000.0, user=null}, Order{id=2, ordertime=Tue Jul 07 00:00:00 CST 2020, total=3000.0, user=null}]}
User{id=2, username='lisi', password='123', birthday=null, orderList=[Order{id=3, ordertime=Mon Jul 06 00:00:00 CST 2020, total=2898.0, user=null}]}

3.多對多的查詢 (查詢用戶同時查詢出該用戶的所有角色)

數據庫表:

User類:

    private int id;
    private String username;
    private String password;
    private Date birthday;
    
    //描述的是當前用戶具備哪些角色
    private List<Role> roleList;

sql語句:

SELECT * FROM USER u,user_role ur,role r WHERE u.id=ur.user_id AND ur.role_id=r.id

查詢結果:

 

UserMapper.xml配置:

    <!--多對多查詢-->

    <resultMap id="userRoleMap" type="user">
    <!--    user的信息-->
        <id column="user_id" property="id"></id>
        <result column="username" property="username"></result>
        <result column="password" property="password"></result>
        <result column="birthday" property="birthday"></result>
    <!--    user內部的roleList信息-->
        <collection property="roleList" ofType="role">
            <id column="role_id" property="id"></id>
            <result column="rolename" property="roleName"></result>
        </collection>

    </resultMap>

    <select id="findUserAndRoleAll" resultMap="userRoleMap">
        SELECT * FROM USER u,user_role ur,role r WHERE u.id=ur.user_id AND ur.role_id=r.id
    </select>

測試結果:

User{id=1, username='zhangsan', password='123', birthday=null, roleList=[Role{id=1, roleName='院長'}, Role{id=2, roleName='導員'}]}
User{id=2, username='lisi', password='123', birthday=null, roleList=[Role{id=3, roleName='老師'}, Role{id=2, roleName='導員'}]}

4.小結

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章