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

时间:2022-12-28 15:11:52

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, ”)