springmvc整合redis(一)
程序员文章站
2022-03-25 22:04:33
最近在学习redis,虽然现在还不是很熟练。不过可以进行简单的框架整合开发。那么我就把我的springmvc整合redis的过程分享给大家。 IDE:我使用的是IDEA。 首先看一下我的工程结构: 第一步:在pox.xml中引入jar 第二步:配置web.xml 第三步:配置springmvc.xm ......
最近在学习redis,虽然现在还不是很熟练。不过可以进行简单的框架整合开发。那么我就把我的springmvc整合redis的过程分享给大家。
IDE:我使用的是IDEA。
首先看一下我的工程结构:
第一步:在pox.xml中引入jar
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jiangfx</groupId> <artifactId>springmvc-redis</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springmvc-redis Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.9.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.2.1</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/taglibs/standard --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency> <!-- spring-data-redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.2.RELEASE</version> </dependency> <!-- barchart-wrap-jackson --> <dependency> <groupId>com.barchart.wrap</groupId> <artifactId>barchart-wrap-jackson</artifactId> <version>1.8.6-build001</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <!-- <source>1.8</source> 源代码使用的开发版本 <target>1.8</target> 需要生成的目标class文件的编译版本 --> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build> </project>
第二步:配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 配置spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 编码过滤器 --> <filter> <filter-name>charsetEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>charsetEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置springmvc --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern><!-- 拦截所有请求 --> </servlet-mapping> </web-app>
第三步:配置springmvc.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.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-4.0.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.jiangfx.controller"/> <!-- 配置静态资源文件 --> <mvc:default-servlet-handler/> <!-- 开启注解模式 --> <mvc:annotation-driven/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp"></property><!-- 视图路径 --> <property name="suffix" value=".jsp"></property><!-- 视图后缀名 --> </bean> </beans>
第四步:配置applicationContext.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:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 配置扫描的包 --> <context:component-scan base-package="com.jiangfx.service"/> <!-- 引入spring-data-redis.xml --> <import resource="spring-data-redis.xml"/> </beans>
第五步:配置spring-data-redis.xml
redis.properties
# Redis Setting # Redis默认有16个库,序号是0-15,默认是选中的是0号数据库 spring.redis.database=0 # Redis服务器地址 spring.redis.host=(你的地址) # Redis服务器连接端口,默认是6379 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接池最大阻塞等待时间(使用负值表示没有限制),根据实际情况修改 spring.redis.pool.maxWaitMillis=-1 # 连接池中的最大空闲连接,根据实际情况修改 spring.redis.pool.maxIdle=8 # 连接池中的最小空闲连接,根据实际情况修改 spring.redis.pool.minIdle=0 # 连接超时时间(毫秒),根据实际情况修改 spring.redis.timeout=2000
spring-data-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:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:redis="http://www.springframework.org/schema/redis" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.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-4.0.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 载入redis.properties,这里要特别注意,如果有多个properties文件,必须用逗号分开,不能写成两个 <context:property-placeholder/> --> <context:property-placeholder location="classpath:redis.properties" /> <!-- 配置JedisPoolConfig相关参数 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${spring.redis.pool.maxIdle}"></property> <property name="minIdle" value="${spring.redis.pool.minIdle}"></property> <property name="maxWaitMillis" value="${spring.redis.pool.maxWaitMillis}"></property> </bean> <!-- 配置redis服务器信息 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="poolConfig"></property> <property name="hostName" value="${spring.redis.host}"></property> <property name="port" value="${spring.redis.port}"></property> <!--<property name="password" value="${spring.redis.password}"></property>--> <property name="database" value="${spring.redis.database}"></property> <property name="timeout" value="${spring.redis.timeout}"></property> </bean> <!-- 配置RedisTemplate --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory"></property> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean> </property> <!-- 使用JacksonJsonRedisSerializer需要引入jar包:barchart-wrap-jackson-1.8.6-build001.jar --> <!-- JacksonJsonRedisSerializer 需要一个有参的构造函数,因此需要配置constructor-arg --> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer"> <constructor-arg type="java.lang.Class" value="java.lang.Object"></constructor-arg> </bean> </property> </bean> <!-- 配置redis连接池 --> <bean class="redis.clients.jedis.JedisPool"> <constructor-arg ref="poolConfig" /> <constructor-arg value="${spring.redis.host}" /> <constructor-arg type="int" value="${spring.redis.port}" /> <constructor-arg type="int" value="${spring.redis.timeout}" /> <constructor-arg type="java.lang.String" value="${spring.redis.password}" /> <constructor-arg type="int" value="${spring.redis.database}" /> </bean> </beans>
第六步:测试
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.redis.core.RedisTemplate; /** * Created by jiangfeixiang on 2018/4/26 */ public class SpringmvcRedisTest { private static ApplicationContext applicationContext; static{ applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testApplicationContext(){ System.out.println("11111111111111111"+applicationContext); } @Test public void testRedisConnection(){ RedisTemplate redisTemplate = (RedisTemplate)applicationContext.getBean("redisTemplate"); redisTemplate.renameIfAbsent("k2", "k1");//如果key=k2存在,则将key修改为k1 System.out.println(redisTemplate); } }
如何查看是否成功?我这里是通过可视化软件:Redis Desktop Manager,你也可以直接在redis-cli客户端查看
以上就是springmvc整合redis的过程,如果有什么问题都可以联系我。
推荐阅读
-
Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数示例代码
-
Spring整合redis(jedis)实现Session共享的过程
-
SpringMVC整合SpringSession 实现sessiong
-
Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数示例代码
-
SpringBoot缓存详解并整合Redis架构
-
Spring Boot整合Redis的完整步骤
-
Redis集群与SSM整合使用方法
-
一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程
-
redis与ssm整合方法(mybatis二级缓存)
-
SpringMVC-简介和执行原理分析(一)