手摸手教你优雅的实现电商项目中的Solr搜索功能,整合SSM框架和Shiro安全框架;教你用Vue.JS和ElementUI写出超漂亮的页面
技术栈
-
后端: SSM + Shiro + Redis + Solr
-
前端: Vue.JS + ElementUI
测试环境
IDEA + Maven + Tomcat8
项目设计
ssm-redis-solr
├── README
├── README.md
├── db
│ ├── sys_schema.sql -- 建表SQL
│ └── tb_item.sql -- 商品数据,共934条数据
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── cn
│ │ │ └── tycoding
│ │ │ ├── controller -- SpringMVC的控制层(web层)
│ │ │ ├── credentials -- shiro密码加密验证服务类CredentialsMatcher,用于登录错误次数限制
│ │ │ ├── entity -- javaBean实体类
│ │ │ ├── mapper -- mybatis-mapper层接口和XML映射文件
│ │ │ ├── realm -- shiro自定义Realm实现类
│ │ │ ├── service -- service服务层接口
│ │ │ └── utils -- 通用util工具类
│ │ ├── resources
│ │ │ ├── mybatis -- mybatis配置文件
│ │ │ ├── other -- 存放参数的配置文件
│ │ │ └── spring -- spring集成shiro,myabtis,mvc,redis,solr的配置文件
│ │ └── webapp
│ │ ├── WEB-INF
│ │ ├── goods.html -- 商品列表页
│ │ ├── index.html -- 项目首页
│ │ ├── login.html -- 登录页
│ │ └── static -- 前端静态依赖文件
│ └── test
写在前面
- 本项目中使用的8081端口。
- 你需要在本地或服务器上配置Solr和Redis,文档中都配置在本地演示的。其中:redis占用6379端口(默认)、solr配置在Tomcat中,你可以下载我在GitHub上发布的配置好的solr,solr占用8080端口。
- 使用命令
redis-server &
命令启动Redis,启动成功会显示一个大Logo。 - 启动部署了solr的Tomcat,默认使用8080端口,启动成功后用浏览器访问
http://localhost:8080/solr/index.html
,如果进入了solr的管理页面证明Solr配置、启动成功。 - 本例中将solr和redis部署在本地电脑上,如果你仔细阅读了这篇文档,启动项目应该是很容易的。如果你把solr或redis部署在其他地方,请自行修改
resource/spring/spring-solr.xml
和resource/other/redis-config.properties
配置文件信息。 - 请修改
resource/other/jdbc.properties
中连接数据库的信息。 - 只有完成了上述步骤后再启动项目,不然项目会因为连接不上solr或redis而报错。
准备
Shiro
关于Shiro,我这里写了详细的SSM框架整合Shiro安全框架的文档,利用SSM框架+Shiro框架实现用户-角色-权限管理系统;
Solr & Spring-Data-Solr
Solr需要单独部署到Tomcat服务器上,我这里提供自己已经安装和配置好的Tomcat和Solr: Github
注意事项:
-
部署Solr的Tomcat端口和本地项目的端口不能相同,会冲突。
-
注意Github仓库中
solr-tomcat/webapps/solr/WEB-INF/web.xml
中solrhome的位置要修改为自己的。
起步
启动Solr和Redis
如果访问localhost:8080/solr/index.html
出现Solr Admin页则启动成功。
初始化表结构
CREATE DATABASE ssm_redis DEFAULT CHARACTER SET utf8;
具体的数据库约束文件和表数据请看:ssm-redis-solr/db
这里我们模拟添加了934条商品数据
搭建SSM-Shiro集成环境
搭建SSM-Redis集成环境
集成SSM-Redis开发环境,首先你需要安装Redis并启动Redis-Server,然后就是在项目中搭建Spring-Data-Redis的运行环境:
创建redis-config.properties配置文件
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
创建spring-redis.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"
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">
<context:property-placeholder location="classpath:other/*.properties"/>
<!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="${redis.maxIdle}"/>
<!-- 连接时最大的等待时间(毫秒) -->
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<!-- 在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的 -->
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.pass}"/>
<property name="poolConfig" ref="poolConfig"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>