假设要找用户姓名中即含“李”又含“香”的用户,mysql中用like可以解决,语句为:
如果要用JSP+Mybatis来实现的话,首先在UserMapper.java中声明一个selectIllegibilityByName方法:
search是一个由单个字符串组合成的字符串列表,这样我们可以将自己想要查询的字符都添加进去,方法为:
SELECT * FROM user WHERE u_name LIKE '%李%' AND u_name LIKE '%香%';如果要用JSP+Mybatis来实现的话,首先在UserMapper.java中声明一个selectIllegibilityByName方法:
List<User> selectIllegibilityByName(List<String> search);search是一个由单个字符串组合成的字符串列表,这样我们可以将自己想要查询的字符都添加进去,方法为:
String value = "李香";
List<String> search = new ArrayList<>();
for(int i=0;i<value.length();i++){
search.add(String.valueOf(value.charAt(i)));
}
而在UserMapper.xml中,我们可以这么实现:
<select id="selectIllegibilityByName" resultMap="BaseResultMap">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from user where
<foreach item="item" index="index" collection="list" open="(" separator="and" close=")">
u_name like CONCAT('%',#{item},'%')
</foreach>
order by id
</select>使用sql中的字符串实现like。
以上为博主在使用Mybatis所遇到的问题,如果大家对于博主所写的内容有疑问,请在下方评论,楼主将感激不尽!
本文介绍如何使用MyBatis实现复杂模糊查询功能,通过示例展示了如何在UserMapper接口和XML映射文件中实现对用户姓名包含特定字符的查询。
 like的使用&spm=1001.2101.3001.5002&articleId=51470853&d=1&t=3&u=f1d70bb714b94b9d97cc9bb2f3f52c30)
2万+

被折叠的 条评论
为什么被折叠?



