Course handout of Java product purchase

Keywords: solr Spring xml Junit

Getting started with spring datasolr
1.1 introduction to spring data Solr
While the ability to support any programming language has great market value, the question you may be interested in is: how can I
Is Solr's application integrated into Spring? Yes, Spring Data Solr is a framework developed to facilitate the development of Solr. The bottom layer is the encapsulation of SolrJ (official API).

1.2 Spring Data Solr getting started Demo1.2.1 building project
(1) Create maven project and introduce dependency in pom.xml

<dependencies>

<dependency>
<groupId>org.springframework.data</groupId>

<artifactId>spring-data-solr</artifactId>

<version>1.5.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>4.2.4.RELEASE</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.9</version>

</dependency>

</dependencies>

(2) Create applicationContext-solr.xml under src/main/resources

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:solr="http://www.springframework.org/schema/data/solr" xsi:schemaLocation="http://www.springframework.org/schema/data/solr
[url=http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd]http://www.springframework.org/s ... spring-solr-1.0.xsd[/url]
[url=http://www.springframework.org/schema/beans]http://www.springframework.org/schema/beans[/url] [url=http://www.springframework.org/schema/beans/spring-beans.xsd]http://www.springframework.org/schema/beans/spring-beans.xsd[/url] [url=http://www.springframework.org/schema/context]http://www.springframework.org/schema/context[/url] [url=http://www.springframework.org/schema/context/spring-context.xsd]http://www.springframework.org/schema/context/spring-context.xsd[/url]">
<!--  solr server address -->

<solr:solr-server  id="solrServer"  url="http://127.0.0.1:8080/solr"  />

<!--  solr Templates, using solr Templates can be used to CRUD Operation -->

<bean  id="solrTemplate"  class="org.springframework.data.solr.core.SolrTemplate">

<constructor-arg  ref="solrServer"  />

</bean>

</beans>

1.1.1 @Field annotation
Create cn.itcast.pojo package, copy the entity class of TbItem purchased by pinyougou into the project, and use @ Field annotation for attribute identification. If the property does not match the domain name defined in the configuration file, you need to specify the domain name in the annotation.

public  class  TbItem  implements  Serializable{

@Field

private  Long  id;

@Field("item_title")

private  String  title;
@Field("item_price")

private  BigDecimal  price;

@Field("item_image")

private  String  image;

@Field("item_goodsid")

private  Long  goodsId;

@Field("item_category")

private  String  category;

@Field("item_brand")

private  String  brand;

@Field("item_seller")

private  String  seller;

.......

}

1.1.1 add (modify)
Create test class TestTemplate.java

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations="classpath:applicationContext-solr.xml")

public  class  TestTemplate  {

@Autowired

private  SolrTemplate  solrTemplate;

@Test

public  void  testAdd(){

TbItem  item=new  TbItem(); item.setId(1L);
item.setBrand("HUAWEI");

item.setCategory("Mobile phone"); item.setGoodsId(1L);
item.setSeller("Huawei No.2 store");

item.setTitle("HUAWEI Mate9");

item.setPrice(new  BigDecimal(2000)); solrTemplate.saveBean(item); solrTemplate.commit();
}

}

Posted by fwegan on Thu, 02 Jan 2020 03:05:29 -0800