SpringBoot整合Solr和Thymeleaf

6 篇文章 0 订阅
3 篇文章 0 订阅

效果图
在这里插入图片描述
thymeleaf
在这里插入图片描述pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hr</groupId>
    <artifactId>solr-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>solr-1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--依赖-->
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>4.10.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- freemark依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--热部署-->
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

server.port=8888
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=52Java

# thymeleaf
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.check-template-location=true
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.cache=false




常量类

package com.hr.solr1.constant;

/**
 * @ClassName SolrConstant
 * @Description: TODO
 * @Author 汤永红
 * @Date 2020/5/28 0028
 * @Version V1.0
 **/
public class SolrConstant {
    //把常量写在此类
    public static final String SOLR_SERVER_URL = "http://127.0.0.1:7778/solr/";
}

工具类

package com.hr.solr1.utils;

import com.hr.solr1.constant.SolrConstant;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ProductUtil
 * @Description: TODO
 * @Author 汤永红
 * @Date 2020/5/28 0028
 * @Version V1.0
 **/
@Component
public class ProductUtil {
    //1.连到solr服务器
    public SolrServer getSolrServer(){
        //1.1获取服务器路径 SolrConstant.SOLR_SERVER_URL;

        return new HttpSolrServer(SolrConstant.SOLR_SERVER_URL);//ctlr+shift+t查看子类

    }
    //2.获得查询对象
    public SolrQuery getSolrQuery(){
        return new SolrQuery();
    }
    //3. 对products索引库进行搜索(操作)
    public List<String> queryProducts(String field,String keyWords) throws Exception{
        //3.1 查询条件设置
        SolrQuery query = getSolrQuery();
        query.setQuery(field+":"+keyWords);// 查询的索引列(索引库中的)  q
        query.setHighlight(true);//点那个勾
        query.addHighlightField(field);//hl.fl  设置高亮的字段
        query.setHighlightSimplePre("<span style='color:red'>");//pre
        query.setHighlightSimplePost("</span>");//post
        //3.2获取带高亮的部分
        //先连服务器,并带上条件
        QueryResponse response = getSolrServer().query(query);
        SolrDocumentList documents = response.getResults();

        //再获取高亮
        Map<String, Map<String, List<String>>> map = response.getHighlighting();
        //先判断查到没
        List<String> lists=null;
        if(documents.getNumFound()>0){
            lists = new ArrayList<>();
            //只要有集合就会有循环
            for (SolrDocument document : documents) {
                    //获取field,就可以获取id  product_name
                Map<String, List<String>> listMap = map.get(document.get("id"));//
                List<String> product_name = listMap.get("product_name");//直接拿
                String name =product_name!=null ?product_name.get(0):"";//有可能为空
                lists.add(name);
            }

        }

        return lists;
    }

}

控制层

package com.hr.solr1.web;

import com.hr.solr1.utils.ProductUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName ProductController
 * @Description: TODO
 * @Author 汤永红
 * @Date 2020/5/28 0028
 * @Version V1.0
 **/
//     /products/get/小黄人
@Controller
@RequestMapping("/products")
public class ProductController {
    @Resource
    private ProductUtil productUtil;
    @RequestMapping(value = "/get/{keyWords}",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public List<String> getProducts(@PathVariable("keyWords")String keyWords){
        List<String> product_name=null;
        try {//ctrl+alt+t
             product_name = productUtil.queryProducts("product_name", keyWords);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return product_name;
    }
    //跳页面
    @RequestMapping(value = "/get2/{keyWords}",produces = {"application/json;charset=utf-8"})

    public String getProducts2(Model model, @PathVariable("keyWords")String keyWords){
        List<String> product_name=null;
        try {//ctrl+alt+t
            product_name = productUtil.queryProducts("product_name", keyWords);
            model.addAttribute("product_name",product_name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "list";
    }
}

list.jsp

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul>
    <li th:each="name : ${product_name}"><div th:utext="${name}"></div></li>
</ul>
</body>
</html>

当然,也可以用ajax请求JSON

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤永红

一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值