复杂映射指的是使用xml来进行一对一,一对多和多对多的开发测试,其实要理解的是多对多的本质还是一对多,在数据库中的体现要借用中间表。
    涉及到的表有四个:

    • 一对一为订单表和用户表
    • 一对多为用户表和订单表
    • 多对多为用户表、权限表以及中间表

    表的具体内容查看码云
    具体代码如下:

    1. // 涉及到的实体表对应对象:不展示getset和tostring
    2. package com.wangzhi.pojo;
    3. public class Orders {
    4. private Integer id;
    5. private String name;
    6. private Integer userId;
    7. private String createTime;
    8. private User user;
    9. }
    10. package com.wangzhi.pojo;
    11. import java.util.List;
    12. public class User {
    13. private Integer id;
    14. private String name;
    15. private List<Orders> ordersList;
    16. private List<Role> roles;
    17. }
    18. public class Role {
    19. private Integer id;
    20. private String rolename;
    21. }
    22. // mapper接口
    23. public interface OrdersMapper {
    24. Orders selectOrderById(Integer id);
    25. List<Orders> selectAll();
    26. }
    27. public interface UserMapper {
    28. User selectById(Integer id);
    29. List<User> selectAll();
    30. List<User> selectUserRole();
    31. }
    32. // xml
    33. <?xml version="1.0" encoding="UTF-8" ?>
    34. <!DOCTYPE mapper
    35. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    36. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    37. <mapper namespace="com.wangzhi.mapper.OrdersMapper">
    38. <!--一对一的map-->
    39. <resultMap id="id_user" type="orders">
    40. <result column="id" property="id" javaType="int"/>
    41. <result column="name" property="name" javaType="string"/>
    42. <result column="userId" property="userId" javaType="int"/>
    43. <result column="createTime" property="createTime" javaType="string"/>
    44. <association property="user" javaType="com.wangzhi.pojo.User">
    45. <result column="userId1" property="id" javaType="int"/>
    46. <result column="userName" property="name" javaType="string"/>
    47. </association>
    48. </resultMap>
    49. <sql id="ALL_COLUMN">
    50. id, name, user_id, create_time
    51. </sql>
    52. <!--resultMap: 手动配置实体属性和表字段的映射关系,存在多个实体的对应关系的时候使用-->
    53. <select id="selectOrderById" resultMap="id_user" parameterType="int">
    54. SELECT
    55. orders.id AS id,
    56. orders.name AS name,
    57. orders.user_id AS userId,
    58. orders.create_time AS createTime,
    59. user.id AS userId1,
    60. user.name AS userName
    61. FROM orders INNER JOIN user ON orders.user_id = user.id
    62. WHERE orders.id = #{id}
    63. </select>
    64. <select id="selectAll" resultMap="id_user" parameterType="int">
    65. SELECT
    66. orders.id AS id,
    67. orders.name AS name,
    68. orders.user_id AS userId,
    69. orders.create_time AS createTime,
    70. user.id AS userId1,
    71. user.name AS userName
    72. FROM orders INNER JOIN user ON orders.user_id = user.id
    73. </select>
    74. </mapper>
    75. <?xml version="1.0" encoding="UTF-8" ?>
    76. <!DOCTYPE mapper
    77. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    78. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    79. <mapper namespace="com.wangzhi.mapper.UserMapper">
    80. <resultMap id="userMap" type="user">
    81. <result column="userId" property="id" javaType="int"/>
    82. <result column="userName" property="name" javaType="string"/>
    83. <!--一对多-->
    84. <collection property="ordersList" ofType="com.wangzhi.pojo.Orders">
    85. <result column="orderId" property="id" javaType="int"/>
    86. <result column="orderName" property="name" javaType="string"/>
    87. <result column="orderUserId" property="userId" javaType="int"/>
    88. <result column="orderCreateTime" property="createTime" javaType="string"/>
    89. </collection>
    90. </resultMap>
    91. <select id="selectAll" resultMap="userMap">
    92. SELECT
    93. user.id AS userId,
    94. user.name AS userName,
    95. orders.id AS orderId,
    96. orders.name AS orderName,
    97. orders.user_id AS orderUserId,
    98. orders.create_time AS orderCreateTime
    99. FROM user LEFT JOIN orders ON user.id = orders.user_id
    100. </select>
    101. <select id="selectById" resultMap="userMap" parameterType="int">
    102. SELECT
    103. user.id AS userId,
    104. user.name AS userName,
    105. orders.id AS orderId,
    106. orders.name AS orderName,
    107. orders.user_id AS orderUserId,
    108. orders.create_time AS orderCreateTime
    109. FROM user LEFT JOIN orders ON user.id = orders.user_id WHERE user.id = #{id}
    110. </select>
    111. <resultMap id="userRoleMap" type="user">
    112. <result column="userId" property="id" javaType="int"/>
    113. <result column="userName" property="name" javaType="string"/>
    114. <!--多对多,本质其实还是一对多-->
    115. <collection property="roles" ofType="role">
    116. <result column="roleId" property="id" javaType="int"/>
    117. <result column="roleName" property="rolename" javaType="string"/>
    118. </collection>
    119. </resultMap>
    120. <select id="selectUserRole" resultMap="userRoleMap">
    121. SELECT
    122. user.id AS userId,
    123. user.name AS userName,
    124. sys_role.id AS roleId,
    125. sys_role.rolename AS roleName
    126. FROM
    127. user LEFT JOIN sys_user_role ON USER.id = sys_user_role.userid
    128. LEFT JOIN sys_role ON sys_user_role.roleid = sys_role.id
    129. </select>
    130. </mapper>
    131. // 测试类
    132. public class TestOrderMapper {
    133. @Test
    134. public void testOneToOne() throws IOException {
    135. InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
    136. SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
    137. OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
    138. Orders orders = mapper.selectOrderById(1);
    139. System.out.println(orders);
    140. }
    141. @Test
    142. public void testOrdersAll() throws IOException {
    143. InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
    144. SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
    145. OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
    146. List<Orders> orders = mapper.selectAll();
    147. orders.forEach(System.out::println);
    148. }
    149. }
    150. public class TestUserMapper {
    151. @Test
    152. public void testOneToMany() throws IOException {
    153. InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
    154. SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
    155. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    156. List<User> users = mapper.selectAll();
    157. users.forEach(System.out::println);
    158. }
    159. @Test
    160. public void testManyToMany() throws IOException {
    161. InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
    162. SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
    163. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    164. List<User> users = mapper.selectUserRole();
    165. users.forEach(System.out::println);
    166. }
    167. }

    多对多的展示是看以哪方为主,两方都可以为主。重点是resultMap的使用以及collection和association的使用。