MyBatis——step1-xml文件实现一对多关系 collection嵌套 sql简单五表联查

mybatis xml文件实现一对多关系 collection嵌套 sql简单五表联查

权限管理 

<?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.mappers.UserMapper">
	<!-- 自定义结果类型 -->
	<resultMap type="com.entity.User" id="userMap">
		<id property="userid" column="userid"/>
		<result property="nickname" column="nickname"/>
		<result property="password" column="password"/>
		<result property="realname" column="realname"/>
	</resultMap>
	<resultMap type="com.entity.User" id="allMap">
		<id property="userid" column="userid"/>
		<result property="nickname" column="nickname"/>
		<result property="password" column="password"/>
		<result property="realname" column="realname"/>
		<collection property="listRole" javaType="ArrayList" ofType="com.entity.Role">
			<id property="roleid" column="roleid"/>
			<result property="rolename" column="rolename"/>
			<result property="rolecode" column="rolecode"/>
			<collection property="listModule" javaType="ArrayList" ofType="com.entity.Module">
				<id property="moduleid" column="moduleid"/>
				<result property="modulename" column="modulename"/>
				<result property="url" column="url"/>
				<result property="paretid" column="paretid"/>
			</collection>
		</collection>
	</resultMap>
	
	<!-- 数据库操作 -->
	<select id="doSelectBySome" resultMap="userMap">
		select * from user 
		<where>
			<if test="userid!=null and userid!=''">
				and userid = #{userid} 
			</if>
			<if test="realname!=null and realname!=''">
				and realname = #{realname} 
			</if>
			<if test="password!=null and password!=''">
				and password = #{password} 
			</if>
		</where>
	</select>
	<!-- 五表连接查询 -->
	<select id="doSelectAll" resultMap="allMap">
	select
	u.userid,u.nickname,u.realname,u.password,r.roleid,r.rolename,r.rolecode,m.moduleid,m.modulename,m.url,m.paretid
	from user u,role r,module m,user_role ur,role_module rm
	WHERE u.userid = ur.userid AND ur.roleid = r.roleid AND r.roleid=rm.roleid
	AND rm.moduleid = m.moduleid
	ORDER BY u.userid,r.roleid,m.paretid
</select>
</mapper>

ps:据说Javatype可以不写,是多对一实现的属性

猜你喜欢

转载自blog.csdn.net/Jason_A/article/details/82826090