数据库表在一对一映射中。
在数据库sys_user_role中新增一条记录
一个用户可以有多个角色。查询出所有用户和所对应的角色。
1.collection集合的嵌套结果映射
<!-- SysUserMapper.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="test.dao.SysUserMapper"> <resultMap id="BaseResultMap" type="test.model.SysUser"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> <id column="id" jdbcType="BIGINT" property="id" /> <result column="user_password" jdbcType="VARCHAR" property="userPassword" /> <result column="user_name" jdbcType="VARCHAR" property="userName" /> <result column="user_email" jdbcType="VARCHAR" property="userEmail" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <result column="user_info" jdbcType="LONGVARCHAR" property="userInfo" /> <result column="head_img" jdbcType="LONGVARBINARY" property="headImg" /> </resultMap> <!-- 继承上面的UserResultMap --> <resultMap type="test.model.SysUser" id="userRoleResultMap" extends="BaseResultMap"> <!-- roleList相关属性 --> <collection property="roleList" resultMap="test.dao.SysRoleMapper.BaseResultMap" columnPrefix="role_"> </collection> </resultMap> <select id="selectUserRoleListAll" resultMap="userRoleResultMap"> select u.id, u.user_name, u.user_password, u.user_email, u.create_time, u.user_info, u.head_img, r.id role_id, r.role_name role_role_name, r.enabled role_enabled, r.create_by role_create_by, r.create_time role_create_time from sys_user u inner join sys_user_role ur on u.id=ur.user_id inner join sys_role r on ur.role_id=r.id </select> </mapper>