mybatis注解版in查询、字符串判空模糊匹配 、批量插入、插入返回主键、判断集合是否为空

 

IN查询

@Select({"<script> " +
            " select * "+
            " from business_threat bt \n" +
            " join abnormal_event_type aet on bt.event_type_id = aet.id " +
            " where 1=1 " +
            " <if test = ' ids != null'> " +
            " and bt.id in " +
            " <foreach item = 'item' index = 'index' collection = 'ids' open = '(' separator = ',' close = ')' > " +
            " #{item} " +
            " </foreach> " +
            " </if> " +
            "</script>"})
    List<BusinessThreatVO> getByBusinessThreadId(@Param("ids") List<Long> ids);

 

 

模糊查询  &apos;  &apos;这个就是 表示不等于空字符串

"<if test='userName !=null and userName!= &apos;&apos; ' >" +
            " and u.user_name like  CONCAT('%',#{userName},'%') " +
            " </if>" +

 

 

 

批量插入

@Insert({
 "<script>",
 "insert into table_name(column1, column2) values ",
 "<foreach collection='lists' item='item' index='index' separator=','>",
 "(#{item.实体属性1}, #{item.实体属性2})",
 "</foreach>",
 "</script>"
})
int insertCollectList(@Param(value="lists") List<Test> lists);

 

插入数据返回主键

 @Options(useGeneratedKeys = true,keyProperty ="id(主键的字段)")
    @Insert(" insert into user (id, user_name, ”)

 

判断集合是否为空

<!-- 检查集合是否不为空 -->
    <if test="yourList != null and yourList.size() > 0">
        AND a.column IN
        <foreach collection="yourList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </if>

 

posted @ 2021-04-03 01:26  yvioo  阅读(485)  评论(0编辑  收藏  举报