小编给大家分享一下Mybatis查询条件包含List的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
在mybatis中进行搜索时,有时候参数中包含了List,比如传入参数:
public class FileRequest{
//文件类型
private Integer fileType;
//状态
private List<Status> statusList;
}
public class Status{
//注册状态
private Integer registerStatus;
//会议状态
private Integer meetingStatus
}
<select id="findList" parameterType="FileRequest" resultMap="...">
select * from tableName where
1=1
<if test="fileType != null ">
and file_type = #{fileType}
</if>
<if test="statusList != null ">
and
<foreach collection="statusList" index="index" item="item" open"(" separator="or" close=")">
<if test="item.registerStatus != null ">
and register_status= #{item.registerStatus}
</if>
<if test="item.meetingStatus != null ">
and meeting_status= #{item.meetingStatus }
</if>
</foreach>
</if>
</select>
Mybatis查询条件带List和其他类型字段(Integer,String,...).
select * from table where type=?
and code in (?,?,?,?)
Mapper.java文件
List<BaseDictionary> selectByTypeAndCodes(
@Param("codes") List<Integer> codes,
@Param("type") Integer type);
Mapper.xml.
注意其中<foreach collection="codes"中的collection的值要和你定义的List别名@Param(“codes”)一致,
而不是只有一个list参数时的<foreach collection="list"
<select id="selectByTypeAndCodes" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from base_dictionary
where type = #{type}
AND code in
<foreach collection="codes" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
AND show_enable=1
AND obj_status=1
ORDER BY sort
</select>
执行结果:
BaseJdbcLogger.debug(BaseJdbcLogger.java:145)==> Preparing: select id, type, name, code, sort, show_enable, obj_remark, obj_status, obj_createdate, obj_createuser, obj_modifydate, obj_modifyuser from base_dictionary where type = ? AND code in ( ? , ? , ? ) AND show_enable=1 AND obj_status=1 ORDER BY sort
BaseJdbcLogger.debug(BaseJdbcLogger.java:145)==> Parameters: 34(Integer), 1(Integer), 2(Integer), 3(Integer)
BaseJdbcLogger.debug(BaseJdbcLogger.java:145)<== Total: 2
以上是“Mybatis查询条件包含List的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。