模糊搜索sql语句
要说到模糊搜索,大家首先想到的肯定是模糊搜索的sql语句,下面就来看一下模糊搜索的sql怎么写
@Override
public PageBean deviceQueryByKey(int pagenum,int pagesize,String conditionKey) throws SPMException{
String sql="select * from v_m3s_device where concat(`device_id`,`type_name`," +
"`manufacturer_name`,`use_date`) like concat('%',?,'%') order by device_id desc";
return queryMapResult(sql, pagenum, pagesize, conditionKey);
}
注释:首先解释参数,pagenum是分页每页显示几条数据,pagesize是当前页,conditionKey是搜索框里的关键字;然后就是sql语句,like直接在数据库中查找可以查到所有所需数据,concat将指定字符串连接到此字符串的结尾,’%’,?,’%’是模糊匹配搜索内容,?就是关键字conditionKey。
控制层
if(searchPhrase!=null && !searchPhrase.equals("")){ //模糊搜索 this.pageBean=this.deviceQueryService.deviceQueryByKey(current,rowCount,searchPhrase);
mapData.put("device_id", searchPhrase);
mapData.put("manufacturer_name", searchPhrase);
mapData.put("type_name", searchPhrase);
mapData.put("use_date", searchPhrase);
mapData.put("service_date",searchPhrase);
}
注释:控制层只需一个简单的if语句即可,加载页面的时候,搜索内容是空,所以就不走里面的程序,提高性能;current,rowCount,searchPhrase分别就是刚才的pagenum,pagesize,conditionKey,当然得写个全局变量,此处省略,然后就大功告成了,测试即可。