本文章来自我的博客:http://iclyj.cn/blog/articles/98.html
根据上一篇的solr服务器的搭建完成先创建一个测试类测试一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Test
public
void
testSolrJ()
throws
Exception {
//创建连接
SolrServer solrServer =
new
HttpSolrServer(
"http://192.168.0.103:8080/solr"
);
//创建一个文档对象
SolrInputDocument document =
new
SolrInputDocument();
//添加域
document.addField(
"id"
,
"solrtest01"
);
document.addField(
"item_title"
,
"测试商品"
);
document.addField(
"item_sell_point"
,
"卖点"
);
//添加到索引库
solrServer.add(document);
//提交
solrServer.commit();
}
|
亲测可行
创建pojo类
创建mapper接口
编写mapper.xml
修改applicationContext-dao.xml
修改applicationContext-service.xml
创建service
创建controller层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package
com.taotao.search.controller;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.ResponseBody;
import
com.taotao.common.pojo.TaotaoResult;
import
com.taotao.search.service.ItemService;
import
com.taotaocommon.util.ExceptionUtil;
/**
* 导入索引库
* @author lyj
*
*/
@Controller
public
class
ItemController {
@Autowired
private
ItemService itemService;
@RequestMapping
(
"/importall"
)
@ResponseBody
public
TaotaoResult importALL(){
try
{
TaotaoResult result= itemService.importItems();
return
result;
}
catch
(Exception e) {
e.printStackTrace();
return
TaotaoResult.build(
500
, ExceptionUtil.getStackTrace(e));
}
}
}
|
测试:localhost:8083/search/importall
返回200即成功