开发人员如何快速定制化实现一个基于Solr的搜索引擎( 三 )


import java.io.Serializable;
public class TbItemResult implements Serializable {
private long id;
private long tbTtemCid;
private String tbItemCname;
private String tbItemTitle;
private String tbItemSellPoint;
private String tbItemDesc;
public long getId(){
return id;
}
public void setId(long id){
this.id=id;
}
public long getTbTtemCid(){
return tbTtemCid;
}
public void setTbTtemCid(long tbTtemCid){
this.tbTtemCid=tbTtemCid;
}
public String getTbItemCname(){
return tbItemCname;
}
public void setTbItemCname(String tbItemCname){
this.tbItemCname=tbItemCname;
}
public String getTbItemTitle(){
return tbItemTitle;
}
public void setTbItemTitle(String tbItemTitle){
this.tbItemTitle=tbItemTitle;
}
public String getTbItemSellPoint(){
return tbItemSellPoint;
}
public void setTbItemSellPoint(String tbItemSellPoint){
this.tbItemSellPoint=tbItemSellPoint;
}
public String getTbItemDesc(){
return tbItemDesc;
}
public void setTbItemDesc(String tbItemDesc){
this.tbItemDesc=tbItemDesc;
}
}
创建搜索服务提供者

  • 创建myshop-service-search-provider服务提供者项目
  • MyShopServiceSearchProviderApplication
package com.oxford.myshop.service.search.provider;
@EnableHystrix
@EnableHystrixDashboard
@SpringBootApplication(scanBasePackages="com.oxfrod.myshop")
@MapperScan(basePackages="com.oxford.myshop.service.search.provider.mapper")
public class MyShopServiceSearchProviderApplication {
public static void main(String[] args) {
SpringApplication.run(MyShopServiceSearchProviderApplication.class,args);
Main.main(args);
}
}
  • 在项目中创建TbItemResultMapper接口用于查询MySQL中的数据,用于插入到Solr数据库中
package com.oxford.myshop.service.search.provider.mapper;
@Respository
public interface TbItemResultMapper {
List<TbItemResult> selectAll();
}
Spring的四大注解:
1. @Controller
2. @Service
3. @Component
4. @Repository
  • 在resource中创建mapper包用于创建TbContentCategoryMapper.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">
<mapper namespace="com.oxford.myshop.service.search.provider.mapper.TbItemResultMapper">
<resultMap id="BaseResultMap" type="com.oxford.myshop.service.search.domainTbItemResult">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="tb_item_cid" jdbcType="BIGINT" property="tbItemCid" />
<result column="tb_item_cname" jdbcType="VARCHAR" property="tbItemCname" />
<result column="tb_item_title" jdbcType="VARCHAR" property="tbItemTitle" />
<result column="tb_item_sell_point" jdbcType="VARCHAR" property="tbItemSellPoint" />
<result column="tb_item_desc" jdbcType="VARCHAR" property="tbItemDesc" />
</reslutMap>
<select id="selectAll" resultMap="BaseResultMap">
select
a.id,
a.title as tb_item_title,
a.sell_point as tb_item_sell_point,
a.cid as tb_item_cid,
b.name as tb_item_cname,
c.item_desc as tb_item_desc
from
tb_item as a
left join tb_item_cat as b
on a.cid=b.id
left join tb_item_desc as c
on a.id=c.item_id
</select>
</mapper>
初始化Solr:
public void initSolr() {
List<TbItemResult> tbItemResult=tbItemResultMapper.selectAll();
try{
SolrInputDocument document=null;
for(TbItemResult tbItemResult:tbItemResults){
document=new SolrInputDocument();
document.addFiled("id",tbItemResult.getId());
document.addFiled("tb_item_cid",tbItemResult.getTbItemCid());
document.addFiled("tb_item_cname",tbItemResult.getTbItemCname());
document.addFiled("tb_item_title",tbItemResult.getTbItemTitle());
document.addFiled("tb_item_sell_point",tbItemResult.getTbItemSellPoint());
document.addFiled("tb_item_desc",tbItemResult.getTbItemDesc());
solrClient.add(document);
solrClient.commit();
}
}catch(SolrServerException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


推荐阅读