Spring boot7之整合Spring Data Redis

Spring boot整合Spring Data Redis

  • 配置pom.xml

    1 <?xml version="1.0" encoding="UTF-8"?>

    2 <project xmlns="http://maven.apache.org/POM/4.0.0"

    3                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    4                   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    5         <modelVersion>4.0.0</modelVersion>

    6 

    7         <groupId>com.wigin</groupId>

    8         <artifactId>springbootandredis</artifactId>

    9         <version>1.0-SNAPSHOT</version>

   10         <parent>

   11                 <groupId>org.springframework.boot</groupId>

   12 

   13                 <artifactId>spring-boot-starter-parent</artifactId>

   14                 <version>2.1.5.RELEASE</version>

   15         </parent>

   16 

   17         <dependencies>

   18                 <dependency>

   19                         <groupId>org.springframework.boot</groupId>

   20                         <artifactId>spring-boot-starter-web</artifactId>

   21                         <version>2.1.5.RELEASE</version>

   22                 </dependency>

   23                 <dependency>

   24                         <groupId>org.springframework.boot</groupId>

   25                         <artifactId>spring-boot-starter-data-redis</artifactId>

   26                         <version>2.1.15.RELEASE</version>

   27                 </dependency>

   28                 <dependency>

   29                         <groupId>org.apache.commons</groupId>

   30                         <artifactId>commons-pool2</artifactId>

   31                 </dependency>

   32 

   33                 <dependency>

   34                         <groupId>org.projectlombok</groupId>

   35                         <artifactId>lombok</artifactId>

   36                 </dependency>

   37 

   38         </dependencies>

   39 

   40 

   41 </project>

  • 创建实体类Student(redis没有表的结构不需要映射,但接受的都是json数据需要实现序列化接口)

    1 package com.wiggin.entity;

    2 

    3 import lombok.Data;

    4 

    5 import java.io.Serializable;

    6 import java.util.Date;

    7 

    8 @Data

    9 public class Student implements Serializable {

   10         private Long id;

   11         private String name;

   12         private int score;

   13         private Date birthday;

   14 }

  • 创建StudentHandler

    1 package com.wiggin.entity;

    2 

    3 import lombok.Data;

    4 

    5 import java.io.Serializable;

    6 import java.util.Date;

    7 

    8 @Data

    9 public class Student implements Serializable {

   10         private Long id;

   11         private String name;

   12         private int score;

   13         private Date birthday;

   14 }

  • 配置application.yml

    1 spring:

    2     redis:

    3         database: 0

    4         host: localhost

    5         port: 6379

    6         

  • 创建启动类Application

    1 package com.wiggin;

    2 

    3 import org.mybatis.spring.annotation.MapperScan;

    4 import org.springframework.boot.SpringApplication;

    5 import org.springframework.boot.autoconfigure.SpringBootApplication;

    6 

    7 @SpringBootApplication

    8 // 将包扫入ioc中,这样才能取出

    9 @MapperScan("com.wiggin.repository")

   10 public class Application {

   11         public static void main(String[] args) {

   12                 SpringApplication.run(Application.class,args);

   13         }

   14 }

 

posted @ 2020-08-13 22:43  wigginess  阅读(148)  评论(0编辑  收藏  举报