最近在用SSM+maven做一个项目,项目里面有个搜索功能,现在把mybatis处理模糊查询的过程分享一下。
searchCondition.java
package com.cd.bean;
/**
* 搜索条件
*@Author zhk
*@Date 2018-2-5
**/
public class SearchCondition {
String keyword;//搜索关键词
News news;//news对象
Science science;
public SearchCondition() {
super();
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public News getNews() {
return news;
}
public void setNews(News news) {
this.news = news;
}
public Science getScience() {
return science;
}
public void setScience(Science science) {
this.science = science;
}
}
SearchMapper.java
package com.cd.dao;
import java.util.List;
import com.cd.bean.News;
import com.cd.bean.SearchCondition;
/**
* 搜索功能接口
*@Author zhk
*@Date 2018-2-4
**/
public interface SearchMapper {
/**
* 搜索
* @param keyword
* @return
*/
public List<SearchCondition> search(SearchCondition searchCondition);
}
SearchMapper.xml
<!-- 设置映射 -->
<resultMap type="com.cd.bean.News" id="newsMap">
<id property="NEWS_ID" column="NEWS_ID"/>
<result property="NEWS_TITLE" column="NEWS_TITLE"/>
<result property="NEWS_CONTENT" column="NEWS_CONTENT"/>
<result property="NEWS_IMG" column="NEWS_IMG"/>
<result property="PUBLISH_DATE" column="PUBLISH_DATE"/>
<result property="NEWS_LEVEL" column="NEWS_LEVEL"/>
</resultMap>
<!--searchCondition是个实体类,它封装了news对象-->
<select id="search" parameterType="searchCondition" resultMap="newsMap">
select
<include refid="Field"/>
from
<include refid="tableName"/>
<where>
<if test="keyword != null and keyword != ''">
NEWS_TITLE like
<if test="dbName == 'oracle'">'%'||#{keyword}||'%'</if>
<if test="dbName == 'mssql'">'%'+#{keyword}+'%'</if>
<if test="dbName == 'mysql'">concat('%',#{keyword},'%')</if>
</if>
</where>
</select>
SearchService.java
package com.cd.service.search.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cd.bean.News;
import com.cd.bean.SearchCondition;
import com.cd.dao.SearchMapper;
import com.cd.service.search.SearchService;
/**
* 搜索功能
*@Author zhk
*@Date 2018-2-5
**/
@Service("searchService")
public class SearchServiceImpl implements SearchService{
@Autowired
SearchMapper searchMapper;
/**
* 搜素
*/
@Override
public List<SearchCondition> search(SearchCondition searchCondition) throws Exception {
return searchMapper.search(searchCondition);
}
}
controller:
/**
* 搜索功能
* @return
* @throws Exception
*/
@RequestMapping("/search")
public ModelAndView search(@RequestParam(value = "pn",defaultValue="1")Integer pn,@RequestParam(value="keyword")String keyword) throws Exception {
ModelAndView mv = new ModelAndView();
System.out.println("控制层:"+keyword);
SearchCondition sc = new SearchCondition();
sc.setKeyword(keyword);
PageHelper.startPage(pn, 15);//开始分页
List<SearchCondition> list = searchService.search(sc);
if(list.isEmpty()){
mv.addObject("keyword", keyword);
mv.setViewName("search_error");
}else{
PageInfo page = new PageInfo(list,5);//设置分页信息
mv.addObject("pageInfo", page);
mv.setViewName("search_list");
}
return mv;
}
很简单吧。