mybatis处理自关联

mybatis处理自关联

    何为自关联我就不多说,毕竟能找到这里的肯定不会再问什么是自关联:
    本例子亲测正确运行,主要是子类查找所属父类;

   先上实体类:
public class Right {
	private Integer rightno;

	private String rightname;

	private Integer rightstate;

	private Integer rightparent;

	private String rightremark;
	// 权限父类(关联)
	private Right right;

	//省略set get方法
	}

映射文件:
<?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.hansuo.traffic.dao.RightMapper">
	<resultMap id="BaseResultMap" type="com.hansuo.traffic.entity.Right">
		<id column="rightNo" property="rightno" jdbcType="INTEGER" />
		<result column="rightName" property="rightname" jdbcType="VARCHAR" />
		<result column="rightState" property="rightstate" jdbcType="INTEGER" />
		<result column="rightParent" property="rightparent" jdbcType="INTEGER" />
		<result column="rightRemark" property="rightremark" jdbcType="VARCHAR" />
	</resultMap>
	<!-- 自关联映射结果集 -->
	<resultMap type="Right" id="RightRightResultMap" extends="BaseResultMap">
		<association property="right" column="rightParent" select="findParent">
		</association>
	</resultMap>

	<sql id="Base_Column_List">
		rightNo, rightName, rightState, rightParent, rightRemark
	</sql>
	<sql id="likewhere">
		<where>
			<if test="rightno!=null">
				and right1.rightNo LIKE CONCAT(CONCAT('%', #{rightno}),
				'%')
			</if>
			<if test="rightname!=null">
				and right1.rightName LIKE CONCAT(CONCAT('%',
				#{rightname}), '%')
			</if>
			and right1.rightParent is not null
		</where>

	</sql>
	 <select id="findParent" parameterType="java.lang.Integer" resultType="Right">
	select * from b_right where rightNo =#{rightParent}
	</select> 
	
	<!--查找所有权限 -->
	<select id="allRightsearch" resultMap="RightRightResultMap"
		parameterType="Right">
		select * from b_right right1
		left join b_right right2
		on right1.rightParent = right2.rightNo
		<include refid="likewhere"></include> 
	</select>

</mapper>

  此例子仅供参考,如有其它更好的解决方案,欢迎交流告知。谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值