全文检索工具 solr:第一章:理论知识,消息队列 kafka 入门教程
<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)在 src/main/resources 下创建 ?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: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
http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
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/s
《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
chema/context/spring-context.xsd">
<solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" />
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
@Field 注解
属性使用 @Field(“域名”)注解标识 。 ??如果属性与配置文件定义的域名称不一致,需要在注解中指定域名称。
public class SkuInfo implements Serializable{
String id;
String spuId;
@Field("sku_price")
BigDecimal price;
@Field("sku_name")
String skuName;
BigDecimal weight;
@Field("sku_desc")
String skuDesc;
String catalog3Id;
@Field("sku_default_img")
String skuDefaultImg;
.......
}
增加(修改)
对于修改:如果 id 存在就认为是修改,若不存在,就认为是新增
创建测试类 TestTemplate.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-solr.xml")
public class TestTemplate {
@Autowired
private SolrTemplate solrTemplate;
@Test
public void testAdd(){
SkuInfo skuInfo = new SkuInfo();
skuInfo.setId("1");
skuInfo.setSkuName("华为");
skuInfo.setPrice(new BigDecimal(3000.01));
skuInfo.setCatalog3Id("2");
skuInfo.setSkuDefaultImg("www.baidu,com");
skuInfo.setSkuDesc("非常好");
skuInfo.setSpuId("3");
solrTemplate.saveBean(skuInfo);
solrTemplate.commit();
}
}
按主键查询
<table border="1" cellspacing="0" style="width:426.1pt;"><tbody><tr><td style="vertical-align:top;width:426.1pt;"><p><span style="color:#1e1c11;"><span style="color:#646464;">@</span><span style="color:#646464;">Test</span></span></p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>void</strong></span></strong><span style="color:#000000;"> testFindOne(){</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;"> SkuInfo sku = template.getById("1",SkuInfo.class);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;"> System.out.printf(sku.getSkuName());</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p></td></tr></tbody></table>
按主键删除
<table border="1" cellspacing="0" style="width:426.1pt;"><tbody><tr><td style="vertical-align:top;width:426.1pt;"><p><span style="color:#1e1c11;"><span style="color:#646464;">@Test</span></span></p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>void</strong></span></strong><span style="color:#000000;"> testDelete(){</span></span></p><p><span style="color:#1e1c11;"><span style="color:#0000c0;">solrTemplate</span><span style="color:#000000;">.deleteById(</span><span style="color:#2a00ff;">"1"</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#0000c0;">solrTemplate</span><span style="color:#000000;">.commit();</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p></td></tr></tbody></table>
分页查询
首先循环插入 100 条测试数据
<table border="1" cellspacing="0" style="width:426.1pt;"><tbody><tr><td style="vertical-align:top;width:426.1pt;"><p><span style="color:#1e1c11;"><span style="color:#646464;">@Test</span></span></p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>void</strong></span></strong><span style="color:#000000;"> testAddList(){</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">List<TbItem> </span><span style="color:#6a3e3e;">list</span><span style="color:#000000;">=</span><strong><u><span style="color:#7f0055;"><strong><u>new</u></strong></span></u></strong><u><span style="color:#000000;"><u> ArrayList()</u></span></u><span style="color:#000000;">;</span></span></p><p> </p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>for</strong></span></strong><span style="color:#000000;">(</span><strong><span style="color:#7f0055;"><strong>int</strong></span></strong> <span style="color:#6a3e3e;">i</span><span style="color:#000000;">=0;</span><span style="color:#6a3e3e;">i</span><span style="color:#000000;"><100;</span><span style="color:#6a3e3e;">i</span><span style="color:#000000;">++){</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">TbItem </span><span style="color:#6a3e3e;">item</span><span style="color:#000000;">=</span><strong><span style="color:#7f0055;"><strong>new</strong></span></strong><span style="color:#000000;"> TbItem();</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setId(</span><span style="color:#6a3e3e;">i</span><span style="color:#000000;">+1L);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setBrand(</span><span style="color:#2a00ff;">"华为"</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setCategory(</span><span style="color:#2a00ff;">"手机"</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setGoodsId(1L);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setSeller(</span><span style="color:#2a00ff;">"华为 2 号专卖店"</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setTitle(</span><span style="color:#2a00ff;">"华为 Mate"</span><span style="color:#000000;">+</span><span style="color:#6a3e3e;">i</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setPrice(</span><strong><span style="color:#7f0055;"><strong>new</strong></span></strong><span style="color:#000000;"> BigDecimal(2000+</span><span style="color:#6a3e3e;">i</span><span style="color:#000000;">));</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">list</span><span style="color:#000000;">.add(</span><span style="color:#6a3e3e;">item</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p><p> </p><p><span style="color:#1e1c11;"><span style="color:#0000c0;">solrTemplate</span><span style="color:#000000;">.saveBeans(</span><span style="color:#6a3e3e;">list</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#0000c0;">solrTemplate</span><span style="color:#000000;">.commit();</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p></td></tr></tbody></table>
总结
我们总是喜欢瞻仰大厂的大神们,但实际上大神也不过凡人,与菜鸟程序员相比,也就多花了几分心思,如果你再不努力,差距也只会越来越大。实际上,作为程序员,丰富自己的知识储备,提升自己的知识深度和广度是很有必要的。
评论