前言:
solr和spring整合其实很简单,只要注意导入依赖的配置文件即可。废话不多说,上代码。
第一步:编写maven项目的pom文件,导入依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://.millery.spring_solr.pojo.User;public class SpringSolrTest {private SpringSolr springSolr;br/>@Beforepublic void setUp() throws Exception {// 初始化Spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml", "applicationContext-solr.xml");//获取对象this.springSolr = applicationContext.getBean(SpringSolr.class);br/>}@Testpublic void test() throws SolrServerException {// 测试方法,输出结果User user = springSolr.getUser((long) 1);System.out.println(user);}}运行代码结果:
org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://127.0.0.1:8983/millery
这里抛异常时因为我本机上没有安装solr,无法连接solr,此时说明代码已经没有问题,可以执行查询操作了。
总结建工程时存在的小问题:
1、在建立工程时打包方式使用jar和war的选择可能存在纠结,只想说不用纠结,选哪个都是一样的。
2、在工程pom.xml配置文件配置完成后,可能会出现下图的报错问题,此时就需要简单的处理一下就可以了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。