SpringBoot第 10 讲:SpringBoot+Redis

119 阅读1分钟

一、创建Maven项目

参考:SpringBoot第 1 讲:HelloWorld_秦毅翔的专栏-CSDN博客

二、修改pom.xml

添加jedis支持

<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>
	<!-- SpringBoot支持01、parent:Begin -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>
	<!-- SpringBoot支持01、parent:End -->
 
	<groupId>org.personal.qin.demos</groupId>
	<artifactId>redis_demo</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<!-- java版本 -->
		<java.version>1.8</java.version>
	</properties>
 
	<dependencies>
		<!-- 添加jedis:Begin -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
		</dependency>
		<!-- 添加jedis:End -->
		<!-- SpringBoot:Begin -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot:End -->
 
		<!-- SpringMVC:Begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<!-- SpringMVC:End -->
 
	</dependencies>
 
	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<!-- 资源文件拷贝插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- java编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- SpringBoot支持03、添加SpringBoot的插件支持:Begin -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- SpringBoot支持03、添加SpringBoot的插件支持:End -->
		</plugins>
	</build>
</project>

三、修改配置文件

application.properties

server.port = 8080
server.context-path=/
#----------------redis----------------
# redis服务器ip
spring.redis.host=10.211.55.7
# redis服务器端口号
spring.redis.port=6379
# redis数据库密码
spring.redis.password=Aa123123.
# redis连接池配置
spring.redis.jedis.pool.max-active=10000
spring.redis.jedis.pool.max-idle=50
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=0

四、自定义jedis工具类,用于操作redis

package demo.redis.utils;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
@Configuration
@ConfigurationProperties
public class RedisHelper {
 
	@Value("${spring.redis.host}")
	private String redisHost;
	
	@Value("${spring.redis.port}")
	private int redisPort;
	
	@Value("${spring.redis.password}")
	private String redisPassword;
	
	private JedisPool jedisPool; //连接池
	
	/**
	 * 向Redis服务器中添加数据
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public String setValue(String key, String value) {
		return getJedis().set(key, value);
	}
	
	/**
	 * 根据Key获取Redis中保存的数据
	 * 
	 * @param key
	 * @return
	 */
	public String getValue(String key) {
		return getJedis().get(key);
	}
	
	/**
	 * 根据Key删除Redis中保存的数据
	 * 
	 * @param key
	 * @return
	 */
	public Long removeValue(String key) {
		return getJedis().del(key);
	}
	
	/**
	 * 获得Jedis对象
	 * 
	 * @return
	 */
	private Jedis getJedis() {
//		Log.i(RedisHelper.class, "redisHost:"+redisHost);
//		Log.i(RedisHelper.class, "redisPort:"+redisPort);
//		Log.i(RedisHelper.class, "redisPassword:"+redisPassword);
		if(jedisPool == null) {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(100); //设置最大连接数
			jedisPoolConfig.setMaxIdle(10); //设置最大空闲连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisHost, redisPort); //ip+端口号
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.auth(redisPassword);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(jedis != null) {
				jedis.close(); //关闭jedis连接
			}
			if(jedisPool != null) {
				jedisPool.close(); //关闭jedisPool连接
				jedisPool = null;
			}
		}
		return jedis;
	}
}

五、启动BootApplication进行测试

package demo.redis;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import demo.redis.utils.RedisHelper;
 
@SpringBootApplication
@Controller
public class RedisBootApplication {
	
	@Autowired
	private RedisHelper redisHelper;
	
	/**
	 * 向redis中保存数据
	 * @return
	 */
	@RequestMapping("t1")
	@ResponseBody
	public String test1() {
		System.out.println("ddd");
		return redisHelper.setValue("hello", "hello redis");
	}
	
	/**
	 * 从redis中获取数据
	 * @return
	 */
	@RequestMapping("t2")
	@ResponseBody
	public String test2() {
		return redisHelper.getValue("hello");
	}
	
	/**
	 * 从redis中删除数据
	 * @return
	 */
	@RequestMapping("t3")
	@ResponseBody
	public String test3() {
		return redisHelper.removeValue("hello")+"";
	}
	
 
	public static void main(String[] args) {
		SpringApplication.run(RedisBootApplication.class, args);
	}
}