• 2020090402springboot整合redis之代码(二)


    数据结构

    1在pom.xml中添加redis的依赖

    <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>springboot_redis</groupId>
    <artifactId>springboot_redis</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.3.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
    <java.version>1.8</java.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- web依赖的jar -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 热部署依赖的jar -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
    </dependency>

    <!-- 引入mybatis的starter的包 -->
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
    <!-- <scope>runtime</scope> -->
    </dependency>

    <!-- mysql的驱动包 -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <!-- <scope>runtime</scope> -->
    </dependency>

    <!-- redis的安装包 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <!-- poi导入和导出的jar -->
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>RELEASE</version>
    </dependency>

    <!-- junit测试jar -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <!-- <scope>test</scope> -->
    <exclusions>
    <exclusion>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    </dependencies>


    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    </project>

    2在application.properties中添加redis的配置

    #--------redis基础配置--------------
    spring.redis.database=0
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    #连接超时时间 单位 ms(毫秒)
    spring.redis.timeout=3000

    #-------------redis线程池配置-----------------
    #连接池中的最大空闲连接,默认值是8
    spring.redis.jedis.pool.max-idle=200

    #连接池中的最小空闲连接,默认值是0
    spring.redis.jedis.pool.min-idle=200

    #如果赋值为-1,则表示不限制;pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
    spring.redis.jedis.pool.max-active=2000

    #等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时
    spring.redis.jedis.pool.max-wait=1000

    3JsonData

    package springboot_redis.util;

    import java.io.Serializable;

    public class JsonData implements Serializable{

    private static final long serialVersionUID = 1L;
    /**
    * 状态码
    * 0表示成功;1表示处理中;-1表示失败
    */
    private Integer code;
    /**
    * 数据
    */
    private Object data;
    /**
    * 描述
    */
    private String msg;
    public JsonData(Integer code, Object data, String msg) {
    super();
    this.code = code;
    this.data = data;
    this.msg = msg;
    }
    /**
    * 成功,传入数据
    * @return
    */
    public static JsonData buildSuccess() {
    return new JsonData(0,null,null);
    }

    /**
    * 成功,传入数据
    * @return
    */
    public static JsonData buildSuccess(Object data) {
    return new JsonData(0,data,null);
    }
    /**
    * 成功,传入数据和状态码
    * @param data
    * @param code
    * @return
    */
    public static JsonData buildSuccess(Object data,Integer code) {
    return new JsonData(code,data,null);
    }
    /**
    * 成功,传入状态码和信息
    * @param msg
    * @param code
    * @return
    */
    public static JsonData buildSuccess(String msg,Integer code) {
    return new JsonData(code,msg,null);
    }


    /**
    * 失败,传入描述信息
    * @return
    */
    public static JsonData buildError(String msg) {
    return new JsonData(-1,null,msg);
    }

    /**
    * 失败,传入信息和状态码
    * @param msg
    * @param code
    * @return
    */
    public static JsonData buildError(String msg,Integer code) {
    return new JsonData(code,null,msg);
    }
    /**
    * 失败,出入数据和状态码
    * @param data
    * @param code
    * @return
    */
    public static JsonData buildError(Object data,Integer code) {
    return new JsonData(code,data,null);
    }

    public JsonData() {}


    public Integer getCode() {
    return code;
    }
    public void setCode(Integer code) {
    this.code = code;
    }
    public Object getData() {
    return data;
    }
    public void setData(Object data) {
    this.data = data;
    }
    public String getMsg() {
    return msg;
    }
    public void setMsg(String msg) {
    this.msg = msg;
    }
    @Override
    public String toString() {
    return "JsonData [code=" + code + ", data=" + data + ", msg=" + msg + "]";
    }



    }

    4SpringbootRedisController 

    package springboot_redis.controller;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import springboot_redis.util.JsonData;

    @RestController
    @RequestMapping("api/v1/redis")
    public class SpringbootRedisController {

    /**
    * StringRedisTemplate是redis的模版
    * 可以通过ctrl+鼠标左键查看对应的redis的实现方法
    * 用的比较多的是StringRedisTemplate.ops开头的方法
    */
    @Autowired
    private StringRedisTemplate redisImpl;

    @GetMapping(value = "add")
    public Object add() {
    redisImpl.opsForValue().set("name","李明");
    return JsonData.buildSuccess("ok");
    }

    @GetMapping(value = "get")
    public Object get() {
    String value=redisImpl.opsForValue().get("name");
    return JsonData.buildSuccess(value);
    }


    }

    5接口访问

    add:localhost:8080/api/v1/redis/set

    get:localhost:8080/api/v1/redis/get

  • 相关阅读:
    顶目群定义及项目群管理
    项目管理与项目组合管理的不同
    IT项目经理:人际关系技能和领导技能的重要性
    IT 项目经理的职业生涯
    Sharepoint2010 中隐藏 "快速启动"与"最近修改'
    3 个基本的IT项目组合种类
    项目成功的标志及决定因素
    HDL,你们作对了吗?
    JAVA代码编写的30条建议
    八款开源 Android 游戏引擎
  • 原文地址:https://www.cnblogs.com/zhushilai/p/13615584.html
Copyright © 2020-2023  润新知