06 广告模块.txt
UP 返回
1. 模块搭建
创建maven模块,取名pinyougou-content-interface,父工程选择pinyougou-parent,点击下一步(勾选create a simple project),打包类型为jar,点击finish。右键添加pojo的依赖
创建pinyougou-content-service模块同上,打包方式选为war,点击finish。复制sellerService的pom依赖到该模块中,将interface的依赖改为content的interface,同时修改端口为9002。
WEB-INF和spring文件夹也复制过来。spring下的dubbox配置文件applicationContext-service.xml需要修改端口 服务名和对应的包名
在manager的web模块中引入content的interface模块
创建门户模块pinyougou-portal-web,选择parent父工程,打包方式为war。复制shop-web模块的pom依赖到该模块中,修改端口为9103,去掉sellergoods,security的依赖,加上content的接口依赖。复制web.xml文件,并删除security的配置内容。
复制配置文件,删掉security的配置文件和文件上传,并清空application.properties的内容(即只保留springmvc.xml文件和空文件application.properties)。springmvc.xml中删除多媒体解析器的配置,修改dubbo的服务名称和包名
从静态文件中复制js css plugins index.html img文件夹,并从其他模块复制angular插件,base.js等文件
2. SpringDataRedis
创建maven工程springDataRedisDemo,pom中引入spring Junit 和redis的依赖。redis的为:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
复制配置文件:F:\2019年4月黑马程序员教程\05-黑马JavaEE49期全套\17品优购电商系统开发\资源\配置文件\SpringDataRedis 下的properties spring文件到资源目录
配置中的相关属性含义:
maxIdle :最大空闲数
maxWaitMillis:连接时的最大等待毫秒数
testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的
创建测试类即可操作:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestValue {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testAll() {
redisTemplate.boundValueOps("name").set("itcast");// 操作String值
System.out.println((String) redisTemplate.boundValueOps("name").get());
redisTemplate.delete("name");// 删除key
redisTemplate.boundSetOps("nameSet").add("曹操");// 操作set集合并添加元素
redisTemplate.boundSetOps("nameSet").add("刘备");
redisTemplate.boundSetOps("nameSet").add("孙权");
System.out.println(redisTemplate.boundSetOps("nameSet").members());
redisTemplate.boundSetOps("nameSet").remove("孙权");// 移除set集合中的某个元素
System.out.println(redisTemplate.boundSetOps("nameSet").members());
redisTemplate.delete("nameSet");// 删除key
redisTemplate.boundListOps("nameList1").rightPush("刘备");// 操作list右压栈
redisTemplate.boundListOps("nameList1").rightPush("关羽");
redisTemplate.boundListOps("nameList1").rightPush("张飞");
redisTemplate.boundListOps("nameList1").leftPush("庞统");// 操作list左压栈
System.out.println(redisTemplate.boundListOps("nameList1").range(0, 10));// 取list元素
System.out.println(redisTemplate.boundListOps("nameList1").index(1));// 查询索引元素
redisTemplate.boundListOps("nameList1").remove(2, "关羽");// 删除list中两个"关羽"
System.out.println(redisTemplate.boundListOps("nameList1").range(0, 10));
redisTemplate.delete("nameList1");
redisTemplate.boundHashOps("nameHash").put("a", "唐僧");// 操作map塞值
redisTemplate.boundHashOps("nameHash").put("b", "悟空");
redisTemplate.boundHashOps("nameHash").put("c", "八戒");
redisTemplate.boundHashOps("nameHash").put("d", "沙僧");
System.out.println(redisTemplate.boundHashOps("nameHash").keys());// 获取mapkeys
System.out.println(redisTemplate.boundHashOps("nameHash").values());// 获取mapvalues
System.out.println(redisTemplate.boundHashOps("nameHash").get("b"));// 根据map的key取值
redisTemplate.boundHashOps("nameHash").delete("d");// 删除key
redisTemplate.delete("nameHash");
}
}
广告模块中引入redis,具体方式看项目代码
3. solr Solr是一个开源搜索平台,用于构建搜索应用程序
3.1 安装
独自解压一个tomcat,本次使用的是 D:\EnvironmentExtends\apache-tomcat-Solr
将 F:\2019年4月黑马程序员教程\05-黑马JavaEE49期全套\17品优购电商系统开发\配套软件\Solr\solr-4.10.3\dist 下的war包solr-4.10.3.war复制到tomcat的webapps下并改名为solr.war,暂时不考虑版本
本文件夹其实是 F:\2019年4月黑马程序员教程\05-黑马JavaEE49期全套\17品优购电商系统开发\配套软件\Solr 下的solr-4.10.3.zip解压得到的。solr-4.10.3.tgz.tgz是对应的linux版本
进入 D:\EnvironmentExtends\apache-tomcat-Solr\bin 启动startup.bat,就会将war包解压出文件夹
复制扩展jar包:
将 \配套软件\Solr\solr-4.10.3\example\lib\ext 下的jar全部复制到 D:\EnvironmentExtends\apache-tomcat-Solr\webapps\solr\WEB-INF\lib 下
创建solrhome:
将文件夹 \配套软件\Solr\solr-4.10.3\example\solr 复制到 D:\EnvironmentExtends 改名为solrhome ,用于solr存放数据库。里面的collection1就可以看成是一个数据库,这个库中的conf文件夹下的schema.xml是一个很重要的配置文件,用于存放数据库类型的字段定义
进入 D:\EnvironmentExtends\apache-tomcat-Solr\webapps\solr\WEB-INF 中的 web.xml 修改属性,指定solr的存储位置:
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>D:\EnvironmentExtends\solrhome</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
重新启动tomcat,访问 http://localhost:8080/solr/#/ 即可看到页面,选择collection1,里面的Query就是用来查询的页面,点击Execute Query可以查询所有数据;q框中表示的是查询表达式,冒号左边的是key 右边的是条件
在Analysis菜单中可以输入一句话,再选择其类型(比如text),点击分析可以分析出句子中的每一个词。中文分词不太行
3.2 中文分词器
将F:\2019年4月黑马程序员教程\05-黑马JavaEE49期全套\17品优购电商系统开发\配套软件\Solr\IK Analyzer 2012FF_hf1 (通过IK Analyzer 2012FF_hf1.zip复制而来)
中的IKAnalyzer2012FF_u1.jar 复制到tomcat中的 D:\EnvironmentExtends\apache-tomcat-Solr\webapps\solr\WEB-INF\lib 文件夹下
在D:\EnvironmentExtends\apache-tomcat-Solr\webapps\solr\WEB-INF 下创建classes文件夹,将配置文件IKAnalyzer.cfg.xml,个人词典mydict.dic,停止词典ext_stopword.dic复制进来(这些文件都可以文本编辑。停止词典即其中的字不作为词语识别)
在schema.xml文件中添加配置:
<!-- 表示使用类IKAnalyzer来分析 -->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
重启tomcat,,此时在分类中选择之前配置的text_ik,点击分析即可
3.3 配置介绍
域:配置文件schema.xml中有一个field属性,里面的值代表的意义如下:
• name:指定域的名称
• type:指定域的类型
• indexed:是否索引 为true表示对应的字段需要作为检索字段
• stored:是否存储
• required:是否必须
• multiValued:是否多值
所以可以添加如下配置:
<field name="item_goodsid" type="long" indexed="true" stored="true"/>
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="double" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category" type="string" indexed="true" stored="true" />
<field name="item_seller" type="text_ik" indexed="true" stored="true" />
<field name="item_brand" type="string" indexed="true" stored="true" />
复制域:复制域的作用在于将某一个Field中的数据复制到另一个域中,可以用来对多个字段同时查找
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> 这个字段stored是false,代表无需存储,只是一个中间辅助字段
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_category" dest="item_keywords"/>
<copyField source="item_seller" dest="item_keywords"/>
<copyField source="item_brand" dest="item_keywords"/>
动态域:当我们需要动态扩充字段时,我们需要使用动态域。对于品优购,规格的值是不确定的,所以我们需要使用动态域来实现。需要实现的效果如下:
<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />
3.4 SpringDataSolr (solr提供的官方api是solrj,spring对此进行了封装)
创建maven工程springdatasolrDemo,引入依赖
<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>
将 \资源\配置文件\SpringDataSolr 下的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/schema/context/spring-context.xsd">
<!-- solr服务器地址 -->
<solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" />
<!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
项目地址参 D:\ProjectCodes\eclipse_pyg\springdatasolrDemo,其中属性使用@Field注解标识。如果属性与配置文件定义的域名称不一致,需要在注解中指定域名称Field("XXXX")
执行test方法,可以在solr的query中看到已经存在对应对象了
3.5 项目数据导入Solr
上一步关于solr的一些操作方法可以参项目,这里不写了。这小结涉及到手动启动spring项目的方法,参模块pinyougou-solr-util ,我们利用这个模块来导入数据。这个模块也手动展示了spring的启动方式:
@Component
public class SolrUtil {
@Autowired
private TbItemMapper itemMapper;
public void importItemData() {
TbItemExample example = new TbItemExample();
Criteria criteria = example.createCriteria();
criteria.andStatusEqualTo("1");// 审核通过的才导入
List<TbItem> itemList = itemMapper.selectByExample(example);
System.out.println("---商品列表---");
for (TbItem item : itemList) {
System.out.println(item.getId() + " " + item.getTitle() + " " + item.getPrice());
}
}
public static void main(String[] args) {
// ▶加载spring配置文件。后一个*号可以保证加载当前项目的所有符合该名称的配置文件;前一个*号可以保证继续加载引入的jar包中的配置
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext*.xml");
SolrUtil solrUtil = (SolrUtil) context.getBean("solrUtil");
solrUtil.importItemData();
}
}
将3中springdatasolrDemo的TbItem类复制到pinyougou-pojo中,同时在pojo模块中添加solr依赖。启动spring调用方法,方法代码自己去项目看
4. 搜索模块 此模块针对solr有很多内容
创建pinyougou-search-interface模块jar,引入pojo依赖。创建对应的service模块war
创建pinyougou-search-service模块war,参考seller模块引入需要的pom依赖,引入/pinyougou-search-service/src/main/webapp/WEB-INF,引入资源配置文件(记得修改各个文件的端口等)
注:seller-service模块不小心被删了,重新补入了一些代码。今后若有异常可以重新看视频再纠正一下
创建pinyougou-search-web模块war,按需复制其他项目的配置即可
solr的搜索匹配原则是先进行分词,然后对所有分词按或的规则去检索,这样就尽可能多的搜索一些结果出来。比如搜“三星手机”得到的结果可能会比“三星”单个词更多
当然若词中间有空格,就无法匹配到这么多了,所以搜索模块后台现将关键词的空格去除了
DOWN 返回