Mybatis中使用注解开发关于模糊查询写法

吃猫的鱼
2023-04-18 / 0 评论 / 181 阅读 / 正在检测是否收录...

前言

今天在进行业务开发的简单的搜索功能的时候,需要用到Like语句。

这里我使用的是Mybatis注解开发。

关于mysql模糊查询,请看我之前的文章:Mysql 模糊查询 like 语句

问题提出

一开始,业务逻辑,是需要通过关键词,找到数据库中两个字段中有的模糊关键词,于是我毫不犹豫使用了like。

写了下面这样一段"屎代码"(错误的,请不要直接复制使用)

    @Select("SELECT * FROM `nav_pages` WHERE CONCAT(`title`,`introduce`) '%#{keyword}%' ;")
    ArrayList<NavEntity> searchNav(@Param("keyword") String keyword);

结果是报错,报错如下:

### Error querying database.  Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='keyword', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
### The error may exist in com/nav/dao/NavDao.java (best guess)
### The error may involve com.nav.dao.NavDao.searchNav-Inline
### The error occurred while setting parameters
### SQL: SELECT * FROM `nav_pages` WHERE CONCAT(`title`,`introduce`) '%?%' ;
### Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='keyword', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

大概原因就是''中的#{keyword}无法被正确取值,导致报错。

解决方案

后面通过网上资料查询,两种解决方案:

【推荐,防sql注入】第一种方案,用sql语句中的CONCAT函数将字符串连接在一起

@Select("SELECT * FROM `nav_pages` WHERE CONCAT(`title`,`introduce`) LIKE CONCAT('%',#{keyword},'%');")

【有注入风险,建议在对变量进行过滤后使用】第二种方案:

@Select("SELECT * FROM `nav_pages` WHERE CONCAT(`title`,`introduce`) '%${keyword}%' ;")

2

评论 (0)

取消
友情链接 文章阅读: 网站地图