1,Mapper.xml中通过字符串搜索数据库
<!-- 通过出版社查找图书--> <select id="getAllBookByPress" parameterType="String" resultMap="BookWithPicAndShopResultMap"> select <include refid="BookWithPicAndShop_Column_List" /> from t_book b, t_shop s, t_picture p, t_categorytwo c where b.shopid = s.shopid and b.picid = p.picid and b.categoryid = c.twoid and b.pass = "通过" and b.press like '%${value}%' </select> <!-- 通过搜索图书名得到销售数量大于0的指定店家的图书数据 --> <!-- 其中hashmap是mybatis自己配置好的,用来传递多个不同类型的参数 --> <!-- 模糊查询要这样写:b.longname like concat(concat('%',#{bname}),'%') --> <select id="getSoldBookByShopidAndBookName" parameterType="java.util.HashMap" resultMap="BookWithPicAndShopResultMap"> select <include refid="BookWithPicAndShop_Column_List" /> from t_book b, t_shop s, t_picture p, t_categorytwo c where b.shopid = s.shopid and b.picid = p.picid and b.pass = "通过" and b.categoryid = c.twoid and b.sold > 0 and s.shopid = #{sid, jdbcType=INTEGER} and b.longname like concat(concat('%',#{bname}),'%') </select>2、字符串匹配问题,
如果使用
b.press like '%${value}%'的方式往往会抛出下方的异常
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'typeName' in 'class java.lang.String'。 所以一般写成下方的格式(模糊匹配)
b.longname like concat(concat('%',#{bname}),'%')可以写成下方这样
b.longname like concat(concat(#{bname}))也也可以写成这样:
b.longname = #{bname}如果是完全匹配的话,采用
FIND_IN_SET函数(正确) SELECT * FROM my_test WHERE FIND_IN_SET(1, ids);示例:
<!--通过typeName查询ProblemTypeDO --> <select id="getProblemTypeByName" parameterType="String" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from problem_type where FIND_IN_SET(#{typeName}, type_name) <!-- 字符串全匹配--> </select>待修改。。。