MyBatis 关联查询自关联(查询指定父节点的所有子节点)分析

java哥 阅读:300 2021-03-31 16:53:05 评论:0

Mapper.java 接口定义

 List<***> selectAllWrapper(Map<String, Object> paramter);

Mapper.xml 配置文件

<resultMap id="BaseResultMap" type="SysDataArch"> 
    <id column="sid" jdbcType="VARCHAR" property="sid" /> 
    <result column="assort_no" jdbcType="VARCHAR" property="assortNo" /> 
    <result column="assort_name" jdbcType="VARCHAR" property="assortName" /> 
    <result column="psid" jdbcType="VARCHAR" property="psid" /> 
    <result column="security_level_code" jdbcType="VARCHAR" property="securityLevelCode" /> 
    <result column="storage_type_code" jdbcType="VARCHAR" property="storageTypeCode" /> 
  </resultMap> 
   
  <resultMap id="BaseResultMapWrapper" type="SysDataArchWrapper" extends="BaseResultMap"> 
    	<collection property="children" ofType="SysDataArchAssort" select="selectChildrenByParent" column="sid"/> 
  </resultMap> 
 
 <select id="selectChildrenByParent" parameterType="java.lang.String" resultMap="BaseResultMapWrapper"> 
  		select  
  			<include refid="Base_Column_List" /> 
  		from sys_data_arch 
  		where psid = #{sid,jdbcType=VARCHAR} 
  </select> 
   
  <select id="selectAllWrapper" parameterType="map" resultMap="BaseResultMapWrapper"> 
  		select  
  			<include refid="Base_Column_List" /> 
  		from sys_data_arch 
  		where 1 = 1 
  		<include refid="condition" /> 
  </select>

Service.java 接口定义

 List<SysDataArchAssortWrapper> selectAllWrapper(Map<String, Object> paramter);

ServiceImpl.java 类实现

@Override 
	public List<SysDataArchWrapper> selectAllWrapper(Map<String, Object> paramter) { 
		// TODO Auto-generated method stub 
		return mapper.selectAllWrapper(paramter); 
	}

实体定义和拓展对象:

@Setter 
@Getter 
public class SysDataArch extends BaseModel { 
    private String sid; 
 
    private String assortNo; 
 
    private String assortName; 
 
    private String psid; 
 
    private String securityLevelCode; 
 
    private String storageTypeCode; 
}
@Setter 
@Getter 
public class SysDataArchWrapper extends SysDataArch{ 
	 
	private List<SysDataArch> children; 
}

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号