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);
模糊查询 ' '这个就是 表示不等于空字符串
"<if test='userName !=null and userName!= '' ' >" + " 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>
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网如果有地址失效了,可以私信我,后续看到会补充)

浙公网安备 33010602011771号