ssm+redis+solr小例子

Eclipse Mars Release (4.5.0)
jdk1.8
redis服务3.0
solr服务6.5.1
mysql
pom文件,这是我从另一个小项目里面复制出来的 关键的jar都有注释的


    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>javax.servlet-apiartifactId>
        <version>3.1.0version>
    dependency>
    
    <dependency>
    <groupId>javax.servlet.jspgroupId>
    <artifactId>javax.servlet.jsp-apiartifactId>
    <version>2.3.1version>
dependency>
    
    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>jstlartifactId>
        <version>1.2version>
    dependency>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-coreartifactId>
        <version>4.1.7.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-beansartifactId>
        <version>4.1.7.RELEASEversion>
    dependency>
    <dependency>
         <groupId>org.springframeworkgroupId>
         <artifactId>spring-txartifactId>
         <version>4.1.7.RELEASEversion>
        dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>4.1.7.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-context-supportartifactId>
        <version>4.1.7.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webartifactId>
        <version>4.1.7.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>4.1.7.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aopartifactId>
        <version>4.1.7.RELEASEversion>
    dependency>

<dependency>
    <groupId>commons-fileuploadgroupId>
    <artifactId>commons-fileuploadartifactId>
    <version>1.3.2version>
dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aspectsartifactId>
        <version>4.1.7.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>4.1.7.RELEASEversion>
    dependency>
      <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>1.2.3version>
    dependency>
    
    <dependency>
        <groupId>log4jgroupId>
        <artifactId>log4jartifactId>
        <version>1.2.17version>
    dependency>

    
     <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.3.0version>
    dependency>


    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.37version>
    dependency>

    <dependency>
        <groupId>org.apache.shirogroupId>
        <artifactId>shiro-coreartifactId>
        <version>1.2.4version>
    dependency>

    <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-log4j12artifactId>
        <version>1.7.12version>
    dependency>

    <dependency>
        <groupId>org.apache.shirogroupId>
        <artifactId>shiro-webartifactId>
        <version>1.2.4version>
    dependency>

    <dependency>
        <groupId>org.apache.shirogroupId>
        <artifactId>shiro-springartifactId>
        <version>1.2.4version>
    dependency>

<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>poiartifactId>
    <version>3.9version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-excelantartifactId>
<version>3.9version>
dependency>   

<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-apiartifactId>
    <version>1.7.21version>
dependency>

        <dependency>
    <groupId>org.apache.solrgroupId>
    <artifactId>solr-solrjartifactId>
    <version>6.5.1version>
dependency>
<dependency>
        <groupId>commons-logginggroupId>
        <artifactId>commons-loggingartifactId>
        <version>1.1.1version>
dependency>

redis支持的jar包
ssm+redis+solr小例子_第1张图片
目录结构
ssm+redis+solr小例子_第2张图片
每个包最后的名字对应的包作用

package cn.ssmsr.service.impl;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

import cn.ssmsr.po.Jd;
import cn.ssmsr.service.Searchservice;

public class jdproductsearch implements Searchservice{
    private static final String URL="http://172.16.163.203:8080/solr/jd_core"; //solr服务器地址
    private static HttpSolrClient solrClient = null;
    private void init(){
        solrClient = new HttpSolrClient.Builder(URL).build();
        solrClient.setConnectionTimeout(3000);
        }
    /**
     * 传入关键字查询索引 
     */
    public SolrDocumentList Searchproduct(String keyword){
        init();
        SolrQuery query=new SolrQuery();
        //排序规则
        query.addSort("id",ORDER.asc);
        //从什么id开始
        query.setStart(0);
        //要显多少条记录
        query.setRows(100);
        //查询关键字
        query.set("q","title:"+keyword);
        //是否高亮(好像不太管用)
        query.setHighlight(true);  
        query.addHighlightField("title"); 
        query.setHighlightSimplePre(""); 
        query.setHighlightSimplePost(""); 
        SolrDocumentList solrDocumentList=null;
        try {
            //查询
            QueryResponse response = solrClient.query(query);
            //返回索引文本集合
             solrDocumentList = response.getResults();
             /*for (SolrDocument doc : solrDocumentList) {
                 System.out.println(doc.get("id"));
             }*/
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        return solrDocumentList;
    }
    /**
     * 根据查询删除文档
     */
    public void queryDeleteDoc(String id){
        init();
        try {
            //根据id删除索引文档
            solrClient.deleteByQuery("id:"+id);
            solrClient.commit();
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 添加索引文本
     */
    public void addDoc(Jd jd){
        init();
        SolrInputDocument sd=new SolrInputDocument();
        sd.addField("id", jd.getId());
        sd.addField("title", jd.getTitle());
        sd.addField("link",jd.getLink());
        sd.addField("price", jd.getPrice());
        sd.addField("comment", jd.getComment());
        sd.addField("goodcomment", jd.getGoodcomment());
        sd.addField("poorcountstr", jd.getPoorcountstr());
        sd.addField("aftercount", jd.getAftercount());
        try {
            //添加对象为索引
            solrClient.add(sd);
            solrClient.commit();
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

不知道怎么写了感觉什么都要写又感觉什么都不用写;要看的到
https://github.com/394356808/SolrRedissolrdemo/tree/master/src/main/java/cn/ssmsr/controller看吧我把代码放上了,操作redis的类我复制别人的,为了尊重别人的劳动成果我就没删注释了;如有侵权的地方希望联系我删除 该怎么写欢迎大家提意见我重新写写发上来

你可能感兴趣的:(java)