SimpleJDBCTemplate中使用LIKE子句

SimpleJDBCTemplate中使用LIKE子句

本文介绍了在SQL预准备语句,Spring,SimpleJDBCTemplate中使用LIKE子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下sql预准备语句:

I have the following sql prepared statement:

SELECT * FROM video WHERE video_name LIKE ?

我使用spring和jdbc.我有一个方法,其中term是搜索项,sjt是SimpleJdbcTemplate,VideoMapper是RowMapper,searchForTermQuery是上面的字符串

Im using spring and jdbc.i have a method, where term is a searchterm, sjt is a SimpleJdbcTemplate, VideoMapper is a RowMapper and searchForTermQuery is the string from above

...
return sjt.query(searchForTermQuery, new VideoMapper(), term);

我的桌子上有2个与字词匹配的视频.但是,当我运行查询时,找不到任何内容.我得到一个空列表.

My table has 2 videos that match the term.However when I run the query none is found. I get an empty List.

我尝试在问号附近使用%,但是它只给出了badGrammarExceptions.

I tried playing with % around the question mark, but it only gave badGrammarExceptions.

推荐答案

您需要将%放在值本身周围,而不是在占位符(问号)周围.

You need to put the % around the value itself, not around the placeholder (the question mark).

所以:

return sjt.query(searchForTermQuery, new VideoMapper(), "%" + term + "%");

这篇关于在SQL预准备语句,Spring,SimpleJDBCTemplate中使用LIKE子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:56