Solr的安装
请详见:https://blog.csdn.net/u012453843/article/details/70482344
Solr集群搭建
请详见: https://blog.csdn.net/u012453843/article/details/70767178
中文分析器IK-Analyzer的使用
请详见:https://blog.csdn.net/u012453843/article/details/70991327
搜索服务搭建
请详见: https://blog.csdn.net/u012453843/article/details/70992238
搜索系统搭建
请详见:https://blog.csdn.net/u012453843/article/details/70994176
我们先来看看我们要导入数据的sql语句并且查看查询结果。
针对来自三张表的数据,我们最好使用一个pojo来接收这些数据,而且这个pojo还会作为查询结果的载体,因此服务层和表现层都会用到这个pojo,我们最好把它放到meiyou-common工程的pojo目录下。我们新建SearchItem类(记得要实现序列化,因为要进行网络传输)。如下图所示。
price字段类型之所以定义为long型是为了避免使用浮点数类型(float),价格精确到分,也就是由原来的以元单位的价格乘以100倍(数据库中的价格存储的都是以分为单位的价格)。
由于我们要导入的数据来自于三张表,用逆向工程生产成的代码已经解决不了问题了,需要我们手动来写Mapper文件。那么Mapper文件我们应该放到哪儿呢?可能有人认为应该放到meiyou-manager-dao工程下,但是这其实是不太合理的,我们搜索服务只是引用meiyou-manager-dao工程的一些东西,搜索服务的这个操作非常特例,别的工程都用不着,因此我们放到meiyou-manager-dao工程不太合适。我们直接放到meiyou-search-service工程下更合适。
我们在meiyou-search-service工程下新建一个"com.meiyou.search.mapper"包,并在该包下新建SearchItemMapper接口类,在接口中添加一个接口getSearchItemList,如下图所示。
我们把meiyou-manager-dao工程下的某个mapper.xml文件拷贝一份到com.meiyou.search.mapper包下,留下头部和其余的删掉,在mapper中定义一个sql语句。id为接口的名字,resultType是我们定义的pojo类的全类名。
代码如下:
<?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" >
<mapper namespace="com.meiyou.search.mapper.SearchItemMapper" >
<select id="getSearchItemList" resultType="com.meiyou.common.pojo.SearchItem">
SELECT
a.id,
a.title,
a.image,
a.price,
a.sell_point,
b.`name` as category_name,
c.item_desc
FROM
tb_item a,
tb_item_cat b,
tb_item_desc c
WHERE
a.cid = b.id
AND a.id = c.item_id
</select>
<select id="getItemById" parameterType="long" resultType="com.meiyou.common.pojo.SearchItem">
SELECT
a.id,
a.title,
a.sell_point,
a.price,
a.image,
b.`name` as category_name,
c.item_desc
FROM
tb_item a
LEFT JOIN tb_item_cat b ON a.cid = b.id
LEFT JOIN tb_item_desc c ON a.id = c.item_id
WHERE
a.`status` = 1
AND a.id = #{itemId}
</select>
</mapper>
导入商品数据-service层
首先,在meiyou-search-interface工程新建一个接口,如下图所示。
接着在meiyou-search-service工程新建实现类SearchItemServiceImpl,实现SearchItemService接口。如下图所示。
package com.meiyou.search.service.impl;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.meiyou.search.dao.SearchDao;
import com.meiyou.search.mapper.SearchItemMapper;
import com.meiyou.common.pojo.SearchItem;
import com.meiyou.common.pojo.SearchResult;
import com.meiyou.common.pojo.MeiyouResult;
import com.meiyou.search.service.SearchService;
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private SearchItemMapper mapper;
@Autowired
private SolrServer solrserver;
@Autowired
private SearchDao searchdao;
@Override
public MeiyouResult importAllSearchItems() throws Exception{
//1.注入mapper
//2.调用mapper的方法 查询所有的商品的数据
List<SearchItem> searchItemList = mapper.getSearchItemList();
//3.通过solrj 将数据写入到索引库中
//3.1创建httpsolrserver
//3.2 创建solrinputdocument 将 列表中的元素一个个放到索引库中
for (SearchItem searchItem : searchItemList) {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", searchItem.getId().toString());//这里是字符串需要转换
document.addField("item_title", searchItem.getTitle());
document.addField("item_sell_point", searchItem.getSell_point());
document.addField("item_price", searchItem.getPrice());
document.addField("item_image", searchItem.getImage());
document.addField("item_category_name", searchItem.getCategory_name());
document.addField("item_desc", searchItem.getItem_desc());
//添加到索引库
solrserver.add(document);
}
//提交
solrserver.commit();
return MeiyouResult.ok();
}
@Override
public SearchResult search(String queryString, Integer page, Integer rows) throws Exception {
//1.创建solrquery对象
// System.out.println("//1.创建solrquery对象");
SolrQuery query = new SolrQuery();
//2.设置主查询条件
if(StringUtils.isNotBlank(queryString)){
// System.out.println("query.setQuery(queryString);");
query.setQuery(queryString);
}else{
query.setQuery("*:*");
}
//2.1设置过滤条件 设置分页
if(page==null)page=1;
if(rows==null)rows=60;
query.setStart((page-1)*rows);//page-1 * rows
query.setRows(rows);
//2.2.设置默认的搜索域
query.set("df", "item_keywords");
//2.3设置高亮
query.setHighlight(true);
query.setHighlightSimplePre("<em style=\"color:red\">");
query.setHighlightSimplePost("</em>");
query.addHighlightField("item_title");//设置高亮显示的域
// System.out.println("调用dao的方法 返回的是SearchResult");
//3.调用dao的方法 返回的是SearchResult 只包含了总记录数和商品的列表
if(searchdao == null){
System.out.println("searchdao null");
}
SearchResult search = searchdao.search(query);
//4.设置SearchResult 的总页数
long pageCount = 0L;
pageCount = search.getRecordCount()/rows;
if(search.getRecordCount()%rows>0){
pageCount++;
}
search.setPageCount(pageCount);
//5.返回
return search;
}
}
在代码中要使用SearchItemMapper,Spring容器需要能够管理它才行,我们到applicationContext-dao.xml文件当中,原来的扫描范围是com.meiyou.mapper,而我们的Mapper文件所在的包是com.meiyou.search.mapper,因此需要增加一个扫描的范围,添加方式是以","分隔,如下图所示。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 数据源 -->
<!-- 数据库连接池 -->
<context:property-placeholder location="classpath:properties/*.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<!-- sqlsessionfactory -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.meiyou.search.mapper" />
</bean>
<!-- mapper扫描器 -->
</beans>
在实现类中还用到了SolrServer,而默认Spring是没有管理这个类的,因此我们需要配置一下。我们单独建个applicationContext-solr.xml文件来管理。如下图所示。
配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<bean class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg name="baseURL" value="http://192.168.25.130:8080/solr"></constructor-arg>
</bean>
</beans>
写完服务,下面要做的便是发布服务,发布配置:<dubbo:service interface=“com.meiyou.search.service.SearchItemService” ref=“searchItemServiceImpl” timeout=“300000”/>,如下图所示。
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="com.meiyou.search"></context:component-scan>
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="meiyou-search" />
<dubbo:registry protocol="zookeeper" address="192.168.25.130:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20882" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.meiyou.search.service.SearchService" ref="searchServiceImpl" timeout="300000"/>
</beans>
搜索服务Dao实现
我们根据在公司的实际操作场景来写,在实际开发中一般都是要有接口和实现类的,因此我们在meiyou-search-interface工程新建一个com.meiyou.search.dao包,并在该包下新建一个接口类SearchDao,如下图所示。
package com.meiyou.search.service;
import com.meiyou.common.pojo.SearchResult;
import com.meiyou.common.pojo.MeiyouResult;
public interface SearchService {
//导入所有的商品数据到索引库中
public MeiyouResult importAllSearchItems() throws Exception;
//根据搜索的条件搜索的结果
/**
*
* @param queryString 查询的主条件
* @param page 查询的当前的页码
* @param rows 每页显示的行数 这个在controller中写死
* @return
* @throws Exception
*/
public SearchResult search(String queryString ,Integer page,Integer rows) throws Exception;
}
我们在meiyou-search-service工程添加一个com.meiyou.search.dao.impl包并在该包下新建SearchDao的实现类SearchDaoImpl,如下图所示。
实现类代码如下:
package com.meiyou.search.service.impl;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.meiyou.search.dao.SearchDao;
import com.meiyou.search.mapper.SearchItemMapper;
import com.meiyou.common.pojo.SearchItem;
import com.meiyou.common.pojo.SearchResult;
import com.meiyou.common.pojo.MeiyouResult;
import com.meiyou.search.service.SearchService;
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private SearchItemMapper mapper;
@Autowired
private SolrServer solrserver;
@Autowired
private SearchDao searchdao;
@Override
public MeiyouResult importAllSearchItems() throws Exception{
//1.注入mapper
//2.调用mapper的方法 查询所有的商品的数据
List<SearchItem> searchItemList = mapper.getSearchItemList();
//3.通过solrj 将数据写入到索引库中
//3.1创建httpsolrserver
//3.2 创建solrinputdocument 将 列表中的元素一个个放到索引库中
for (SearchItem searchItem : searchItemList) {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", searchItem.getId().toString());//这里是字符串需要转换
document.addField("item_title", searchItem.getTitle());
document.addField("item_sell_point", searchItem.getSell_point());
document.addField("item_price", searchItem.getPrice());
document.addField("item_image", searchItem.getImage());
document.addField("item_category_name", searchItem.getCategory_name());
document.addField("item_desc", searchItem.getItem_desc());
//添加到索引库
solrserver.add(document);
}
//提交
solrserver.commit();
return MeiyouResult.ok();
}
@Override
public SearchResult search(String queryString, Integer page, Integer rows) throws Exception {
//1.创建solrquery对象
// System.out.println("//1.创建solrquery对象");
SolrQuery query = new SolrQuery();
//2.设置主查询条件
if(StringUtils.isNotBlank(queryString)){
// System.out.println("query.setQuery(queryString);");
query.setQuery(queryString);
}else{
query.setQuery("*:*");
}
//2.1设置过滤条件 设置分页
if(page==null)page=1;
if(rows==null)rows=60;
query.setStart((page-1)*rows);//page-1 * rows
query.setRows(rows);
//2.2.设置默认的搜索域
query.set("df", "item_keywords");
//2.3设置高亮
query.setHighlight(true);
query.setHighlightSimplePre("<em style=\"color:red\">");
query.setHighlightSimplePost("</em>");
query.addHighlightField("item_title");//设置高亮显示的域
// System.out.println("调用dao的方法 返回的是SearchResult");
//3.调用dao的方法 返回的是SearchResult 只包含了总记录数和商品的列表
if(searchdao == null){
System.out.println("searchdao null");
}
SearchResult search = searchdao.search(query);
//4.设置SearchResult 的总页数
long pageCount = 0L;
pageCount = search.getRecordCount()/rows;
if(search.getRecordCount()%rows>0){
pageCount++;
}
search.setPageCount(pageCount);
//5.返回
return search;
}
}
搜索功能Service实现
首先我们在meiyou-search-interface工程新建一个接口类SearchService,并在接口类中添加一个接口,如下图所示。
实现类代码如下:
package com.meiyou.search.service.impl;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.meiyou.search.dao.SearchDao;
import com.meiyou.search.mapper.SearchItemMapper;
import com.meiyou.common.pojo.SearchItem;
import com.meiyou.common.pojo.SearchResult;
import com.meiyou.common.pojo.MeiyouResult;
import com.meiyou.search.service.SearchService;
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private SearchItemMapper mapper;
@Autowired
private SolrServer solrserver;
@Autowired
private SearchDao searchdao;
@Override
public MeiyouResult importAllSearchItems() throws Exception{
//1.注入mapper
//2.调用mapper的方法 查询所有的商品的数据
List<SearchItem> searchItemList = mapper.getSearchItemList();
//3.通过solrj 将数据写入到索引库中
//3.1创建httpsolrserver
//3.2 创建solrinputdocument 将 列表中的元素一个个放到索引库中
for (SearchItem searchItem : searchItemList) {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", searchItem.getId().toString());//这里是字符串需要转换
document.addField("item_title", searchItem.getTitle());
document.addField("item_sell_point", searchItem.getSell_point());
document.addField("item_price", searchItem.getPrice());
document.addField("item_image", searchItem.getImage());
document.addField("item_category_name", searchItem.getCategory_name());
document.addField("item_desc", searchItem.getItem_desc());
//添加到索引库
solrserver.add(document);
}
//提交
solrserver.commit();
return MeiyouResult.ok();
}
@Override
public SearchResult search(String queryString, Integer page, Integer rows) throws Exception {
//1.创建solrquery对象
// System.out.println("//1.创建solrquery对象");
SolrQuery query = new SolrQuery();
//2.设置主查询条件
if(StringUtils.isNotBlank(queryString)){
// System.out.println("query.setQuery(queryString);");
query.setQuery(queryString);
}else{
query.setQuery("*:*");
}
//2.1设置过滤条件 设置分页
if(page==null)page=1;
if(rows==null)rows=60;
query.setStart((page-1)*rows);//page-1 * rows
query.setRows(rows);
//2.2.设置默认的搜索域
query.set("df", "item_keywords");
//2.3设置高亮
query.setHighlight(true);
query.setHighlightSimplePre("<em style=\"color:red\">");
query.setHighlightSimplePost("</em>");
query.addHighlightField("item_title");//设置高亮显示的域
// System.out.println("调用dao的方法 返回的是SearchResult");
//3.调用dao的方法 返回的是SearchResult 只包含了总记录数和商品的列表
if(searchdao == null){
System.out.println("searchdao null");
}
SearchResult search = searchdao.search(query);
//4.设置SearchResult 的总页数
long pageCount = 0L;
pageCount = search.getRecordCount()/rows;
if(search.getRecordCount()%rows>0){
pageCount++;
}
search.setPageCount(pageCount);
//5.返回
return search;
}
}
在上面的实现类中使用注解的方式注入SearchDao,能不能注入成功呢?答案是不行的,因为我们在taotao-search-service的spring配置文件中配置的扫描包的范围是com.taotao.search.service,而com.taotao.search.dao不在这个范围内,从而无法将dao这个包下的接口类扫描到spring容器当中,我们也无法注入成功,解决方法有两种,第一种是把扫描包的范围扩大,由原来的com.taotao.search.service改为com.taotao.search,这样com.taotao.search.dao和com.taotao.search.service两个包就都可以被扫描进spring容器当中了。第二种方法是在已有的扫描包后面再加一个扫描范围com.taotao.search.dao,中间以","分隔(base-package=“com.taotao.search.service,com.taotao.search.dao”)。
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="com.meiyou.search"></context:component-scan>
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="meiyou-search" />
<dubbo:registry protocol="zookeeper" address="192.168.25.130:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20882" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.meiyou.search.service.SearchService" ref="searchServiceImpl" timeout="300000"/>
</beans>
写完了Service,下面我们便要发布服务,我们在meiyou-search-service工程的applicationContext-service.xml文件中暴露搜索接口,如下图所示。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="com.meiyou.search"></context:component-scan>
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="meiyou-search" />
<dubbo:registry protocol="zookeeper" address="192.168.25.130:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20882" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.meiyou.search.service.SearchService" ref="searchServiceImpl" timeout="300000"/>
</beans>
商品搜索功能Controller实现
在meiyou-search-web工程需要添加对Search服务的引用,如下图所示。
springmvc.xml文件所有代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:property-placeholder location="classpath:resource/*.properties"/>
<context:component-scan base-package="com.meiyou.search.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 全局异常处理器的配置 -->
<bean class="com.meiyou.search.exception.GlobalExceptionReslover"></bean>
<!-- 引用dubbo服务 -->
<dubbo:application name="meiyou-search-web"/>
<dubbo:registry protocol="zookeeper" address="192.168.25.130:2181"/>
<dubbo:reference interface="com.meiyou.search.service.SearchService" id="searchService" timeout="300000"/>
</beans>
下面我们写一个Controller,如下图所示。
Controller代码如下:
package com.meiyou.search.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.meiyou.common.pojo.SearchResult;
import com.meiyou.search.service.SearchService;
@Controller
public class SearchController {
@Value("${ITEM_ROWS}")
private Integer ITEM_ROWS;
@Autowired
private SearchService service;
/**
* 根据条件搜索商品的数据
* @param page
* @param queryString
* @return
*/
@RequestMapping("/search")
public String search(@RequestParam(defaultValue="1") Integer page,@RequestParam(value="q") String queryString,Model model) throws Exception{
//1.引入
//2.注入
//3.调用
//处理乱码:
queryString = new String(queryString.getBytes("iso-8859-1"),"utf-8");
System.out.println("SearchResult result = service.search(queryString, page, ITEM_ROWS);");
SearchResult result = service.search(queryString, page, ITEM_ROWS);
//4.设置数据传递到jsp中
System.out.println("//4.设置数据传递到jsp中");
model.addAttribute("query", queryString);
model.addAttribute("totalPages", result.getPageCount());//总页数
model.addAttribute("itemList", result.getItemList());
model.addAttribute("page", page);
return "search";
}
}