重点:select * from course where cast(courseId as char) like ‘%118%‘;
首先可以将int类型转换为string类型的值再进行模糊查询,用方法cast(num as char);这个可以将num(int类型)转化为char类型。完整的语句:假如我现在要在课程表(course)里面根据课程id(courseId)进行模糊查询:select * from course where cast(courseId as char) like ‘%118%‘; 118为我们模糊查询的数值。
我采用spring boot + mybatis注解的方式进行模糊查询,我的sql语句拼写一直不正确,要么差不到数据,我所采用的方法是,在service层传值的时候,就将自己所要查询的字符串两边加上%进行查询。
String courseId = "%"+String.valueOf(course.getCourseId())+"%";
String courseName = "%"+course.getCourseName()+"%";
return courseMapper.findfuzzy(courseId,courseName);
//模糊查询
@Select("select * from course where cast(coureId as char)=#{courseId} and courseName=#{courseName}")
public List<Course> findfuzzy(@Param("courseId")String courseId,@Param("courseName")String courseName);
原文地址:https://www.cnblogs.com/yonyong/p/10276332.html