mybatis关键字

一、collection的用法

场景:行业查询,一级行业,二级行业

1、POJO

@Getter
@Setter
public class XXXEntity implements Serializable {
    private Integer id;
    private String name;
    private Integer parentId;
    private Integer level;
    // 节点是否展开,true展开,false折叠
    private boolean open;
    private List<XXXEntity> second;
}

2、mapper

一对多的ResultMap

<resultMap id="SecondResultMap" type="com.xxx.xxx.entity.XXXEntit ">
    <id column="pkid" jdbcType="INTEGER" property="id"/>
    <result column="trade_tag" jdbcType="VARCHAR" property="name"/>
    <result column="parent_id" jdbcType="INTEGER" property="parentId"/>
    <result column="level" jdbcType="INTEGER" property="level"/>
    <collection property="seconds" ofType="com.xxx.xxx.entity.XXXEntity"
        column="pkid" select="getSecondList">
    </collection>
</resultMap>
<select id="getSecondList" resultMap="BaseResultMap" parameterType="java.lang.Integer">
    select
       <include refid="Base_Column_List"></include>
    from trade_info
    where level = 2 AND parent_id = #{pkid}
</select>

还有另一种写法,是将id,result在collection里面再重复一遍,个人不喜欢这种方式,觉得上面这种方式清晰明了

二、foreach

1、场景:循环ArrayList查询对象

2、用法:要判断size>0,collection="传的对象名" ,item="随便一个名称,和#{}一致"

<if test="ids != null and ids.size > 0">
	AND ui.pkid in
	<foreach collection="ids" item="item" separator="," open="(" close=")">
		#{item}
	</foreach>
</if>

猜你喜欢

转载自blog.csdn.net/qq_20552525/article/details/83271937
今日推荐