1、在pom.xml中加入所需api
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>2.1.13.RELEASE</version>
</dependency>
2、增加applicationContext-solr.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr.xsd">
<bean id="httpSolrClient" class="org.apache.solr.client.solrj.impl.HttpSolrClient">
<constructor-arg name="baseURL" value="http://10.10.33.13:8983/solr"/>
</bean>
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="httpSolrClient"/>
</bean>
</beans>
3、增加bean
public class SolrEntity {
private String id;
private String name;
private String age;
private String context;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
4、增加service
@Service
public class SolrService {
@Resource
private SolrTemplate solrTemplate;
public ScoredPage<SolrEntity > queryAll() {
// 查询所有
Query query = new SimpleQuery("*:*");
//设置分页
query.setOffset(0); //开始索引(默认0)
query.setRows(15); //每页记录数(默认10)
ScoredPage<SolrEntity > pages = solrTemplate.queryForPage("edu",query, SolrEntity .class);
System.out.println("pages.getTotalElements() = " + pages.getTotalElements());
List<SolrEntity > content = pages.getContent();
for (SolrEntity item : content) {
System.out.println("item = " + item);
}
return pages;
}
}
solr 可以直接把mysql的数据导入,导入后通过以上方式进行查询。
spring data solr 配置
最新推荐文章于 2022-01-17 09:34:45 发布