mybatis中LIKE模糊查询使用方式
方式一 : 使用 ${} 【平时尽量避免使用】<select id="searchChannelsByName" parameterType="java.lang.String" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from channelwhere deleted = '0
三种方式
name like "%"#{name,jdbcType=VARCHAR}"%"
name like concat('%',#{name}, '%')
name like '%${name}%'
方式一 : 使用 ${} 【平时尽量避免使用】
<select id="searchChannelsByName" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from channel
where deleted = '0'
and name like '%${name}%'
</select>

注意:由于$是参数直接注入的,导致这种写法,大括号里面不能注明jdbcType,不然会报错

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'VARCHAR' in 'class com.utry.ucsc.dao.bean.KnowledgeLibraryBean'
弊端:可能会引起sql的注入,平时尽量避免使用${...}
方式二 : 使用 #{}
<select id="searchChannelsByName" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from cms_channel
where deleted = '0'
and name like "%"#{name,jdbcType=VARCHAR}"%"
</select>
注意:因为#{...}解析成sql语句时候,会在变量外侧自动加单引号' ',所以这里 % 需要使用双引号" ",不能使用单引号 ' ',不然会查不到任何结果。

方式三:使用CONCAT()函数连接参数形式

原文链接:https://blog.csdn.net/zhenwei1994/article/details/81876278
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)