mybatis处理自关联
2017-04-17 14:25
232 查看
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>
此例子仅供参考,如有其它更好的解决方案,欢迎交流告知。谢谢
相关文章推荐
- mybatis 查询多表数据使用collection处理一对多关联
- myBatis 中查询多表数据时使用association处理一对一关联
- mybatis 处理多表关联
- Mybatis实例 简单查询 事务处理 关联、集合查询 鉴别器 动态SQL及各种标签实例
- LINUX软中断处理如何与BH函数关联的
- EXCEL两个表关联的处理办法
- iBatis/MyBatis 主子表关联查询
- 在内存中处理2张表的数据关联运算
- 用户sa登录失败,该用户与可信sql server连接无关联 问题处理方法
- 处理两级关联的代码 [javascript]
- MyBatis高级映射之-一对一(一对多)关联映射
- Delphi Clientdataset处理关联出来的视图的问题
- Zend Framework的DB处理-表关联(转载)
- 选项卡(Tab)切换效果与关联事件处理实现
- spring多数据源的处理 mybatis实现跨库查询
- MyBatis关于LIKE的处理方法
- 主外键关联数据的处理方式,以及应用场景分析
- LR中用关联方法处理对服务集群压力测试问题
- rails中多对多表关联时处理方法总结
- C#中在窗体间使用消息来处理相关联的事件