mybatis关联查询一对多问题解决,将表中对应的多条数据以list形式存储

<?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="GmsMapLayerMapper">

	<insert id="save" useGeneratedKeys="true" keyProperty="id">
		insert
		into gms_map_layer (
	    map_id,
	    layer_id,
	    name,
	    has_index,
	    datasource_id,
	    display_order,
	    geo_type,
	    visible,
	    create_time,
	    update_time,
	    data_layer_name,
	    editable,
	    category,
	    sub_category
		) values (
	    #{mapId},
	    #{layerId},
	    #{name},
	    #{hasIndex},
	    #{datasourceId},
	    #{displayOrder},
	    #{geoType},
	    #{visible},
	    #{createTime},
	    #{updateTime},
	    #{dataLayerName},
	    #{editable},
	    #{category},
	    #{subCategory}
		)
	</insert>

	<select id="query" resultMap="resultMap">
		select * from gms_map_layer
		<where>
			1=1
		    <if test="id != null">
		    	and  id=#{id}
		    </if>
		    <if test="mapId != null">
		    	and  map_id=#{mapId}
		    </if>
		    <if test="layerId != null">
		    	and  layer_id=#{layerId}
		    </if>
		    <if test="name != null and name != ''">
		    	and  name=#{name}
		    </if>
		    <if test="hasIndex != null">
		    	and  has_index=#{hasIndex}
		    </if>
		    <if test="datasourceId != null">
		    	and  datasource_id=#{datasourceId}
		    </if>
		    <if test="displayOrder != null">
		    	and  display_order=#{displayOrder}
		    </if>
		    <if test="visible != null and visible != ''">
		    	and  visible=#{visible}
		    </if>
		    <if test="geoType != null">
		    	and  geo_type=#{geoType}
		    </if>
		    <if test="createTime != null">
		    	and  create_time=#{createTime}
		    </if>
		    <if test="updateTime != null">
		    	and  update_time=#{updateTime}
		    </if>
		    <if test="dataLayerName != null and dataLayerName != ''">
		    	and  data_layer_name=#{dataLayerName}
		    </if>
		    <if test="editable != null">
		    	and  editable=#{editable}
		    </if>
		    <if test="category != null and category != ''">
		    	and  category=#{category}
		    </if>
		    <if test="subCategory != null and subCategory != ''">
		    	and  sub_category=#{subCategory}
		    </if>
		</where>
		order by display_order asc
	</select>
	
    <select id="queryByMapId" resultMap="OnlyLayerResultMap" parameterType="long">
		select * from gms_map_layer
		where map_id=#{mapId}
	</select>
	
	
	<select id="queryForTreeByMapId" resultMap="treeItemResultMap" parameterType="long">
		select l.id,l.name,'C_'||l.category as category,l.sub_category,
		min(s.min_zoom) as min_zoom,max(s.max_zoom) as max_zoom
		from gms_map_layer l,gms_map_layer_style s	
		where map_id=#{mapId} and l.id=s.map_layer_id
		and l.category not in ('40001') 
		and l.name not in ('10001_176','10001_177','10001_178','20014_2')
		group by l.id,l.name,l.category,l.sub_category
		order by l.category,l.sub_category
	</select>
	
	<select id="queryByMapIdCategory" resultMap="OnlyLayerResultMap" >
		select * from gms_map_layer
		where map_id=#{mapId} and category=#{category}
	</select>
	
	<select id="queryByIds" resultMap="resultMap">
		select * from gms_map_layer
		where
		id in
		<foreach item="item" index="index" collection="array" open="("
			separator="," close=")">
			#{item}
		</foreach>
	</select>
	

	<delete id="delete">
		delete from gms_map_layer
		where id = #{id}
	</delete>

	<delete id="batchDelete">
		delete from gms_map_layer
		where
		id in
		<foreach item="item" index="index" collection="array" open="("
			separator="," close=")">
			#{item}
		</foreach>
	</delete>

	<update id="update">
		update gms_map_layer
		<set>
		    <if test="id != null">
		    	id=#{id},
		    </if>
		    <if test="mapId != null">
		    	map_id=#{mapId},
		    </if>
		    <if test="layerId != null">
		    	layer_id=#{layerId},
		    </if>
		    <if test="name != null and name != ''">
		    	name=#{name},
		    </if>
		    <if test="hasIndex != null">
		    	has_index=#{hasIndex},
		    </if>
		    <if test="datasourceId != null">
		    	datasource_id=#{datasourceId},
		    </if>
		    <if test="displayOrder != null">
		    	display_order=#{displayOrder},
		    </if>
		    <if test="visible != null and visible != ''">
		    	visible=#{visible},
		    </if>
		    <if test="geoType != null">
		    	geo_type=#{geoType},
		    </if>
		    <if test="createTime != null">
		    	create_time=#{createTime},
		    </if>
		    <if test="updateTime != null">
		    	update_time=#{updateTime},
		    </if>
		    <if test="dataLayerName != null and dataLayerName != ''">
		    	data_layer_name=#{dataLayerName},
		    </if>
		    <if test="editable != null">
		    	editable=#{editable},
		    </if>
		    <if test="category != null and category != ''">
		    	category=#{category},
		    </if>
		    <if test="subCategory != null and subCategory != ''">
		    	sub_category=#{subCategory},
		    </if>
		</set>
		where id=#{id}
	</update>

	<resultMap type="GmsMapLayer" id="resultMap">
		<!-- 用id属性来映射主键字段 -->
		<id property="id" column="id" />
	    <result property="mapId" column="map_id" /> 
	    <result property="layerId" column="layer_id" /> 
	    <result property="name" column="name" /> 
	    <result property="hasIndex" column="has_index" /> 
	    <result property="datasourceId" column="datasource_id" /> 
	    <result property="displayOrder" column="display_order" /> 
	    <result property="geoType" column="geo_type" />
	    <result property="visible" column="visible" /> 
	    <result property="createTime" column="create_time" /> 
	    <result property="updateTime" column="update_time" />
	    <result property="dataLayerName" column="data_layer_name" /> 
	    <result property="editable" column="editable" /> 
	    <result property="category" column="category" />
	    <result property="subCategory" column="sub_category" />
	    <collection property="styles" column="id" ofType="GmsMapLayerStyle" select="GmsMapLayerStyleMapper.queryByMapLayerId"/>
	</resultMap>
	
	
	<resultMap type="TreeItem" id="treeItemResultMap">
		<!-- 用id属性来映射主键字段 -->
		<id property="id" column="id" />
	    <result property="pId" column="category" /> 
	    <result property="name" column="name" /> 
	    <result property="minZoom" column="min_zoom" />
	    <result property="maxZoom" column="max_zoom" />
	    <result property="subCategory" column="sub_category" />
	</resultMap>
	
	<resultMap type="GmsMapLayer" id="OnlyLayerResultMap">
		<!-- 用id属性来映射主键字段 -->
		<id property="id" column="id" />
	    <result property="mapId" column="map_id" /> 
	    <result property="layerId" column="layer_id" /> 
	    <result property="name" column="name" /> 
	    <result property="hasIndex" column="has_index" /> 
	    <result property="datasourceId" column="datasource_id" /> 
	    <result property="displayOrder" column="display_order" /> 
	    <result property="geoType" column="geo_type" />
	    <result property="visible" column="visible" /> 
	    <result property="mapId" column="map_id" /> 
	    <result property="updateTime" column="update_time" />
	    <result property="dataLayerName" column="data_layer_name" /> 
	    <result property="editable" column="editable" /> 
	    <result property="category" column="category" />
	    <result property="subCategory" column="sub_category" />
	</resultMap>
	
	<select id="indexFieldsByModelid" resultType="string">
		select columnname from gds_meta_field where modelid=#{modelid} and dbindex=1;
	</select>
	<select id="mapNameByLayerId" resultType="string">
			select gm.name gm_name
	   		 from gms_map_layer gl 
	    	join gms_map gm 
			on  gl.map_id=gm.id 
			where gl.layer_id=#{id};
	</select>
	<select id="queryDetailByLayerid" resultMap="detailMap" parameterType="long">
		select gl.createdate gl_createdate,gl.id gl_id,gl.layername gl_name,gmf.id gmf_id,
			gmf.fieldtitle fieldTitle,gmf.fieldname fieldName,gmf.columnname columnName,gmf.fieldtype fieldType,
			gmf.fieldsize fieldSize,gmf.fieldinput fieldInput,gmf.fieldinputfilter fieldInputFilter,
			gmf.defaultvalue defaultValue,gmf.readonly readOnly,gmf.fieldorder fieldOrder,gmf.remark remark,
			gmf.dbindex dbindex,gmf.modelid modelid 
		from gds_layer gl,gds_meta_field gmf
		where gl.modelid=gmf.modelid and gl.id=#{id};

<!-- 	select gl.name,gm.name gm_name -->
<!-- 	    from gms_map_layer gl  -->
<!-- 	    join gms_map gm  -->
<!-- 		on  gl.map_id=gm.id  -->
<!-- 		where gl.layer_id=#{id}; -->
	</select>
	
 	
	<resultMap type="LayerDetail" id="detailMap">
		<result property="createDate" column="gl_createdate" /> 
		<result property="name" column="gl_name" />
		<result property="id" column="gl_id" />
		<collection property="fields" ofType="GdsMetaField" javaType="list">
			<id property="id" column="gmf_id"/>
			<result property="fieldTitle" column="fieldTitle"/>
			<result property="modelId" column="modelid"/>
			<result property="fieldName" column="fieldName"/>
			<result property="columnName" column="columnName"/>
			<result property="fieldType" column="fieldType"/>
			<result property="fieldSize" column="fieldSize"/>
			<result property="fieldInput" column="fieldInput"/>
			<result property="fieldInputFilter" column="fieldInputFilter"/>
			<result property="defaultValue" column="defaultValue"/>
			<result property="readOnly" column="readOnly"/>
			<result property="fieldOrder" column="fieldOrder"/>
			<result property="remark" column="remark"/>
			<result property="dbindex" column="dbindex"/>
		</collection>
		<collection property="indexFields" column="modelid=modelid" ofType="string" javaType="list" select="indexFieldsByModelid">
			<result property="columnname" column="columnname"/>
		</collection>
		<collection property="maps" ofType="String" column="id=gl_id" javaType="list" select="mapNameByLayerId" >
			<result property="name" column="gm_name"/>
		</collection>
	</resultMap>
	
</mapper>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值