一般模糊语句如下:
SELECT 字段 FROM 表 WHERE 某字段 Like 条件
其中关于条件,SQL提供了四种匹配模式:
1、%:表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。
比如 SELECT * FROM [user] WHERE u_name LIKE '%三%'
将会把u_name为“张三”,“张猫三”、“三脚猫”,“唐三藏”等等有“三”的记录全找出来。
另外,如果需要找出u_name中既有“三”又有“猫”的记录,请使用and条件
SELECT * FROM [user] WHERE u_name LIKE '%三%' AND u_name LIKE '%猫%'
若使用 SELECT * FROM [user] WHERE u_name LIKE '%三%猫%'
虽然能搜索出“三脚猫”,但不能搜索出符合条件的“张猫三”。
2、_: 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句:
比如 SELECT * FROM [user] WHERE u_name LIKE '三'
只找出“唐三藏”这样u_name为三个字且中间一个字是“三”的;
再比如 SELECT * FROM [user] WHERE u_name LIKE '三__';
只找出“三脚猫”这样name为三个字且第一个字是“三”的;
3、[ ]:表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
比如 SELECT * FROM [user] WHERE u_name LIKE '[张李王]三'
将找出“张三”、“李三”、“王三”(而不是“张李王三”);
如 [ ] 内有一系列字符(01234、abcde之类的)则可略写为“0-4”、“a-e”
SELECT * FROM [user] WHERE u_name LIKE '老[1-9]'
将找出“老1”、“老2”、……、“老9”;
4、[^ ] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。
比如 SELECT * FROM [user] WHERE u_name LIKE '[^张李王]三'
将找出不姓“张”、“李”、“王”的“赵三”、“孙三”等;
SELECT * FROM [user] WHERE u_name LIKE '老[^1-4]';
将排除“老1”到“老4”,寻找“老5”、“老6”、……
5、查询内容包含通配符时
由于通配符的缘故,导致我们查询特殊字符“%”、“_”、“[”的语句无法正常实现,而把特殊字符用“[ ]”括起便可正常查询。据此我们写出以下函数:
function sqlencode(str)
str=replace(str,"[","[[]") '此句一定要在最前
str=replace(str,"_","[_]")
str=replace(str,"%","[%]")
sqlencode=str
end function
首先在com.foreknow.bean 下 创建 Ad.java
package com.foreknow.bean;
public class Ad extends BaseBean {
private Long id;
private String title;
private String imgFileName;//文件的名称
private String link;
private Long weight;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImgFileName() {
return imgFileName;
}
public void setImgFileName(String imgFileName) {
this.imgFileName = imgFileName;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public Long getWeight() {
return weight;
}
public void setWeight(Long weight) {
this.weight = weight;
}
}
然后写出Bean 的实现类 AdDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace命名空间 ,它的作用是对SQL进行分类化管理 -->
<mapper namespace="com.foreknow.dao.AdDao">
</insert>
<select id="selectByPage" resultType="Ad" parameterType="Ad">
select id,title,img_file_name,link,weight from ad
<where>
<if test="title!=null and title!=''">
title like '%${title}%'
</if>
</where>
order by weight desc,id
</select>
</mapper>
3.写com.foreknow.service 下AdService.java 业务层的接口
public interface AdService {
/**
* 条件查询
* @param ad
* @return
*/
List<AdDto> searchByPage(AdDto adDto);
}
4.写出接口的实现类 com.foreknow.service.impl 包下AdServiceImpl.java
/**
* 模糊查询
*/
@Override
public List<AdDto> searchByPage(AdDto adDto) {
List<AdDto> result = new ArrayList<>();
Ad condition = new Ad();
// 以下操作复杂
// condition.setId(adDto.getId());
// condition.setTitle(adDto.getTitle());
// 将源对象复制到目标对象
BeanUtils.copyProperties(adDto, condition);
List<Ad> adList = adDao.selectByPage(condition);
// 要将List<Ad>类型转换为List<AdDto>类型
for (Ad ad : adList) {
// 目标对象
AdDto adDt = new AdDto();
BeanUtils.copyProperties(ad, adDt);
result.add(adDt);
}
return result;
}
5.创建Dto 在页面输入的值
package com.foreknow.dto;
import org.springframework.web.multipart.MultipartFile;
import com.foreknow.bean.Ad;
public class AdDto extends Ad {
private String img;// 路径
private MultipartFile imgFile;// MultipartFile是spring给我提供了文件上传的一个组件 imgFil要跟表单里name 一致
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public MultipartFile getImgFile() {
return imgFile;
}
public void setImgFile(MultipartFile imgFile) {
this.imgFile = imgFile;
}
}
写出一个com.foreknow.controller.content 控制器
AdController.java
@RequestMapping
public String init(ModelMap modelMap) {
//后期需要调用service获取广告信息
AdDto adDto = new AdDto();
modelMap.addAttribute("list", adService.searchByPage(adDto));
modelMap.addAttribute("searchParam", adDto);
return "/content/adList";
}
@RequestMapping("/search")
public String search(ModelMap modelMap,AdDto adDto) {
//后期需要调用service获取广告信息
modelMap.addAttribute("list", adService.searchByPage(adDto));
modelMap.addAttribute("searchParam", adDto);
return "/content/adList";
}
写出 对应JSP页面 adList.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="pager" tagdir="/WEB-INF/tags/" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE"/>
<title></title>
<link rel="stylesheet" type="text/css" href="${basePath}/css/all.css"/>
<link rel="stylesheet" type="text/css" href="${basePath}/css/pop.css"/>
<link rel="stylesheet" type="text/css" href="${basePath}/css/main.css"/>
<script type="text/javascript" src="${basePath}/js/common/jquery-1.8.3.js"></script>
<script type="text/javascript" src="${basePath}/js/common/common.js"></script>
<script type="text/javascript" src="${basePath}/js/content/adList.js"></script>
</head>
<body style="background: #e1e9eb;">
<form action="${basePath}/ad/search" id="mainForm" method="post">
<input type="hidden" id="id" name="id"/>
<input type="hidden" id="message" value="${pageCode.msg}"/>
<input type="hidden" id="basePath" value="${basePath}"/>
<input type="hidden" name="page.currentPage" id="currentPage" value="1"/>
<div class="right">
<div class="current">当前位置:<a href="#">内容管理</a> > 广告管理</div>
<div class="rightCont">
<p class="g_title fix">广告列表</p>
<table class="tab1">
<tbody>
<tr>
<td align="right" width="80">标题:</td>
<td>
<input name="title" id="title" value="" class="allInput" type="text"/>
</td>
<td style="text-align: right;" width="150">
<input class="tabSub" value="查询" onclick="search('1');" type="button"/>
<%--<t:auth url="/ad/addInit">--%>
<input class="tabSub" value="添加" onclick="location.href='${basePath}/ad/addInit'" type="button"/>
<%--</t:auth>--%>
</td>
</tr>
</tbody>
</table>
<div class="zixun fix">
<table class="tab2" width="100%">
<tbody>
<tr>
<th>序号</th>
<th>标题</th>
<th>链接地址</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="item" varStatus="s">
<tr>
<td>${s.index+1}</td>
<td>${item.title}</td>
<td>${item.link}</td>
<td>
<%--<t:auth url="/ad/modifyInit">--%>
<a href="javascript:void(0);" onclick="">修改</a>
<%--</t:auth>--%>
<%--<t:auth url="/ad/remove">--%>
<a href="javascript:void(0);" onclick="">删除</a>
<%--</t:auth>--%>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<pager:page jsMethodName="search" page="${searchParam.page}"></pager:page>
</div>
</div>
</div>
</form>
</body>
</html>
写出JS代码 adList.js 提交查询
//显示信息
$(function(){
common.showMessage($("#message").val())
})
//这个方法什么时候调用?
function search(currentPage){
//分页
$("#currentPage").val(currentPage);
//查询
$("#mainForm").submit();
}
然后 模糊查询功能就写完了!
网友评论