一、一对多
Dao层写法
List<User> selAll();
UserMapper.xml写法
<select id="selAll" resultMap="userMap1">
SELECT u.*,r.*,m.* FROM t_user u LEFT JOIN t_user_role tur ON u.uid=tur.uid
LEFT JOIN t_role r ON tur.rid=r.rid LEFT JOIN t_role_menu trm
ON r.rid=trm.rid LEFT JOIN t_menu m ON trm.mid=m.mid
</select>
<resultMap id="userMap1" type="com.yy.beans.User">
<id column="uid" property="uid"></id>
<result column="uname" property="uname"></result>
<result column="upwd" property="upwd"></result>
<result column="sex" property="sex"></result>
<collection column="uid" property="roles" ofType="com.yy.beans.Role" resultMap="RoleMap1">
<id column="rid" property="rid"></id>
<result column="rname" property="rname"></result>
</collection>
</resultMap>
<resultMap id="RoleMap1" type="com.yy.beans.Role">
<id column="rid" property="rid"></id>
<result column="rname" property="rname"></result>
<collection column="rid" property="menus" ofType="com.yy.beans.Menu">
<id column="mid" property="mid"></id>
<result column="mname" property="mname"></result>
<result column="url" property="url"></result>
<result column="parentid" property="parentid"></result>
</collection>
</resultMap>
二、多对一
-
多对一关联写法
Dao层写法
User getUserById(int id);
UserMapper.xml写法
<!--多对一-->
<select id="getUserById" parameterType="java.lang.Integer" resultMap="userMap">
SELECT u.id id,u.username username,u.pwd pwd,u.sex sex,u.age age,u.user_role user_role,g.id gid,g.gname gname FROM t_user u
LEFT JOIN t_group g ON u.user_role=g.id WHERE u.id=#{id}
</select>
<resultMap id="userMap" type="com.yy.beans.User">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="pwd" property="pwd"></result>
<result column="sex" property="sex"></result>
<result column="age" property="age"></result>
<result column="user_role" property="user_role"></result>
<association column="user_role" property="group" javaType="com.yy.beans.Group">
<id column="gid" property="id"></id>
<result column="gname" property="gname"></result>
</association>
</resultMap>
多对一关系:
select 标签
resultMap属性:当查询到的字段是多个类中的信息的时候使用
parameterType属性:Dao层方法中的参数类型
resultMap标签
id属性:当前resulitMap标签的唯一标识
type属性:最终需要封装的类型(当前resultMap所对应的方法的返回值)
id子标签:用于标明字段中的主键字段
column属性:主键字段在数据库中的名称
property属性:主键字段在实体类中的名称
result子标签:用于标明字段中的除了主键字段外的其他字段
column属性:该字段在数据库中的名称
property属性:改字段在实体类中的名称
association子标签:用于多对一关系中的关联映射
column属性:多对一中的关联字段
property属性:多方实体类中的对方的属性
-
多对一分批查询写法
Dao层写法
User getUserById2(int id);
UserMapper.xml写法
<select id="getUserById2" parameterType="java.lang.Integer" resultMap="UserMap1">
select * from t_user where id=#{id}
</select>
<resultMap id="UserMap1" type="com.yy.beans.User">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="pwd" property="pwd"></result>
<result column="sex" property="sex"></result>
<result column="age" property="age"></result>
<association column="user_role" property="group" javaType="com.yy.beans.Group" select="com.yy.dao.GroupDao.getGroup"></association>
</resultMap>
Group getGroup(int id);
<mapper namespace="com.yy.dao.GroupDao">
<select id="getGroup" resultType="com.yy.beans.Group" parameterType="java.lang.Integer">
select * from t_group where id=#{id};
</select>
</mapper>