SpringBoot集成Redis

SpringBoot集成Redis

项目结构

SpringBoot集成Redis_第1张图片

配置文件

  1. pom.xml

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.0.4.RELEASEversion>
            <relativePath/> 
        parent>
        <groupId>com.examplegroupId>
        <artifactId>redis-demoartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>redis-demoname>
        <description>Demo project for Spring Bootdescription>
    
        <properties>
            <java.version>1.8java.version>
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-redisartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
        <repositories>
            <repository>
                <id>spring-milestonesid>
                <name>Spring Milestonesname>
                <url>https://repo.spring.io/milestoneurl>
            repository>
            <repository>
                <id>spring-snapshotsid>
                <name>Spring Snapshotsname>
                <url>https://repo.spring.io/snapshoturl>
                <snapshots>
                    <enabled>trueenabled>
                snapshots>
            repository>
        repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-milestonesid>
                <name>Spring Milestonesname>
                <url>https://repo.spring.io/milestoneurl>
            pluginRepository>
            <pluginRepository>
                <id>spring-snapshotsid>
                <name>Spring Snapshotsname>
                <url>https://repo.spring.io/snapshoturl>
                <snapshots>
                    <enabled>trueenabled>
                snapshots>
            pluginRepository>
        pluginRepositories>
    
    project>
    
    
  2. application.yml

    spring:
      redis:
        host: 192.168.79.128  # 端口默认6379, 所以我这里没写
    

启动类

  1. RedisDemoApplication

    package com.example.redisdemo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class RedisDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(RedisDemoApplication.class, args);
        }
    
    }
    

测试

  1. RedisDemoApplicationTests

    package com.example.redisdemo;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.BoundHashOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class RedisDemoApplicationTests {
    
        // @Autowired
        // private RedisTemplate redisTemplate;
    
        @Autowired
        private StringRedisTemplate redisTemplate;
    
        @Test
        public void contextLoads() {
    
            // 存储字符串
            redisTemplate.opsForValue().set("test", "hello world");
            String test = redisTemplate.opsForValue().get("test");
            System.out.println("test = " + test);
    
            // 存储hash
            BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps("user:123");
            ops.put("name", "role");
            ops.put("age", "21");
            // 获取值
            Object name = ops.get("name");
            System.out.println("name = " + name);
    
        }
    
    }
    

你可能感兴趣的:(#,JAVA-SpringBoot)