复杂映射指的是使用xml来进行一对一,一对多和多对多的开发测试,其实要理解的是多对多的本质还是一对多,在数据库中的体现要借用中间表。
涉及到的表有四个:
- 一对一为订单表和用户表
- 一对多为用户表和订单表
- 多对多为用户表、权限表以及中间表
表的具体内容查看码云。
具体代码如下:
// 涉及到的实体表对应对象:不展示getset和tostring
package com.wangzhi.pojo;
public class Orders {
private Integer id;
private String name;
private Integer userId;
private String createTime;
private User user;
}
package com.wangzhi.pojo;
import java.util.List;
public class User {
private Integer id;
private String name;
private List<Orders> ordersList;
private List<Role> roles;
}
public class Role {
private Integer id;
private String rolename;
}
// mapper接口
public interface OrdersMapper {
Orders selectOrderById(Integer id);
List<Orders> selectAll();
}
public interface UserMapper {
User selectById(Integer id);
List<User> selectAll();
List<User> selectUserRole();
}
// 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.wangzhi.mapper.OrdersMapper">
<!--一对一的map-->
<resultMap id="id_user" type="orders">
<result column="id" property="id" javaType="int"/>
<result column="name" property="name" javaType="string"/>
<result column="userId" property="userId" javaType="int"/>
<result column="createTime" property="createTime" javaType="string"/>
<association property="user" javaType="com.wangzhi.pojo.User">
<result column="userId1" property="id" javaType="int"/>
<result column="userName" property="name" javaType="string"/>
</association>
</resultMap>
<sql id="ALL_COLUMN">
id, name, user_id, create_time
</sql>
<!--resultMap: 手动配置实体属性和表字段的映射关系,存在多个实体的对应关系的时候使用-->
<select id="selectOrderById" resultMap="id_user" parameterType="int">
SELECT
orders.id AS id,
orders.name AS name,
orders.user_id AS userId,
orders.create_time AS createTime,
user.id AS userId1,
user.name AS userName
FROM orders INNER JOIN user ON orders.user_id = user.id
WHERE orders.id = #{id}
</select>
<select id="selectAll" resultMap="id_user" parameterType="int">
SELECT
orders.id AS id,
orders.name AS name,
orders.user_id AS userId,
orders.create_time AS createTime,
user.id AS userId1,
user.name AS userName
FROM orders INNER JOIN user ON orders.user_id = user.id
</select>
</mapper>
<?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.wangzhi.mapper.UserMapper">
<resultMap id="userMap" type="user">
<result column="userId" property="id" javaType="int"/>
<result column="userName" property="name" javaType="string"/>
<!--一对多-->
<collection property="ordersList" ofType="com.wangzhi.pojo.Orders">
<result column="orderId" property="id" javaType="int"/>
<result column="orderName" property="name" javaType="string"/>
<result column="orderUserId" property="userId" javaType="int"/>
<result column="orderCreateTime" property="createTime" javaType="string"/>
</collection>
</resultMap>
<select id="selectAll" resultMap="userMap">
SELECT
user.id AS userId,
user.name AS userName,
orders.id AS orderId,
orders.name AS orderName,
orders.user_id AS orderUserId,
orders.create_time AS orderCreateTime
FROM user LEFT JOIN orders ON user.id = orders.user_id
</select>
<select id="selectById" resultMap="userMap" parameterType="int">
SELECT
user.id AS userId,
user.name AS userName,
orders.id AS orderId,
orders.name AS orderName,
orders.user_id AS orderUserId,
orders.create_time AS orderCreateTime
FROM user LEFT JOIN orders ON user.id = orders.user_id WHERE user.id = #{id}
</select>
<resultMap id="userRoleMap" type="user">
<result column="userId" property="id" javaType="int"/>
<result column="userName" property="name" javaType="string"/>
<!--多对多,本质其实还是一对多-->
<collection property="roles" ofType="role">
<result column="roleId" property="id" javaType="int"/>
<result column="roleName" property="rolename" javaType="string"/>
</collection>
</resultMap>
<select id="selectUserRole" resultMap="userRoleMap">
SELECT
user.id AS userId,
user.name AS userName,
sys_role.id AS roleId,
sys_role.rolename AS roleName
FROM
user LEFT JOIN sys_user_role ON USER.id = sys_user_role.userid
LEFT JOIN sys_role ON sys_user_role.roleid = sys_role.id
</select>
</mapper>
// 测试类
public class TestOrderMapper {
@Test
public void testOneToOne() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
Orders orders = mapper.selectOrderById(1);
System.out.println(orders);
}
@Test
public void testOrdersAll() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
List<Orders> orders = mapper.selectAll();
orders.forEach(System.out::println);
}
}
public class TestUserMapper {
@Test
public void testOneToMany() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.selectAll();
users.forEach(System.out::println);
}
@Test
public void testManyToMany() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.selectUserRole();
users.forEach(System.out::println);
}
}
多对多的展示是看以哪方为主,两方都可以为主。重点是resultMap的使用以及collection和association的使用。