本文创建gradle项目用来整合Spring Boot和Redis用到gradle + gson + lombok + redisTemplate技术
重要文件

文件名 说明
build.gradle gradle配置文件
Redis2Application.java 启动
Controller.java 控制器类
StudentService.java 业务逻辑
StudentDao.java DAO类,用于和Redis直接交互
Student.java 模型类,用于映射数据和Java对象
application.java 配置文件

初始化

https://start.spring.io/初始化

1

引入依赖

build.gradle里引入依赖

plugins {
	id 'java'
	id 'org.springframework.boot' version '2.7.8-SNAPSHOT'
	id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'com.xiaolong'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
	maven { url 'https://repo.spring.io/milestone' }
	maven { url 'https://repo.spring.io/snapshot' }
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-redis'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'com.google.code.gson:gson:2.10'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
	useJUnitPlatform()
}

配置文件启动

resources目录下的application.yml文件配置Redis数据库连接本地的6379端口

spring:
  redis:
    host:localhost
    port:6078

如果原来有其他配置文件可以删掉,新建application.yml

启动类,这里注解SpringBootApplication的exclude排除了数据库驱动程序,免去再去安装数据库驱动,Redis不需要数据库驱动类Redis2Application:

package com.xiaolong.redis2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Redis2Application {

	public static void main(String[] args) {
		SpringApplication.run(Redis2Application.class, args);
	}

}

控制类和业务模型

对外提供接口服务的Controller.java

package com.xiaolong.redis2.controller;

import com.xiaolong.redis2.model.Student;
import com.xiaolong.redis2.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
    @Autowired
    StudentService studentService;
    @RequestMapping("/saveStudent")
    public void saveStudent(){
        Student newStudent = new Student();
        newStudent.setId("student_001");
        newStudent.setName("Peter");
        newStudent.setScore("100");
        studentService.saveStudent(newStudent);
    }
    @RequestMapping("/findByID/{id}")
    public Student findByID(@PathVariable String id){
        return studentService.findByID(id);
    }
    @RequestMapping("/deleteByID/{id}")
    public void deleteByID(@PathVariable String id){
        studentService.deleteByID(id);
    }
}

这里注解@RestController注解设置控制器类,用注解@Autowired依赖注入业务逻辑类,用注解@RequestMapping映射路由

业务模型类:

package com.xiaolong.redis2.model;

import lombok.Data;
@Data
public class Student {
    private String id;
    private String name;
    private String score;
}

这里用到了lombok的注解@Data,无需再去写getset方法

业务逻辑

package com.xiaolong.redis2.service;

import com.xiaolong.redis2.StudentDao;
import com.xiaolong.redis2.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    public void saveStudent(Student student) {
        studentDao.saveStudent(student.getId(), 3600, student);
    }
    public Student findByID(String id){
        return studentDao.findByID(id);
    }
    public void deleteByID(String id){
        studentDao.deleteByID(id);
    }
}

通过注解@Service设置业务类,注解@Autowired自动注入DAO对象

Redis DAO数据库交互

package com.xiaolong.redis2;

import com.google.gson.Gson;
import com.xiaolong.redis2.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

@Repository
public class StudentDao {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    public void saveStudent(String id, int expireTime, Student student) {
        Gson gson = new Gson();
        redisTemplate.opsForValue().set(id, gson.toJson(student), expireTime, TimeUnit.SECONDS);
    }

    public Student findByID(String id) {
        Gson gson = new Gson();
        Student student = null;
        String studentJson = redisTemplate.opsForValue().get(id);
        if(studentJson != null &amp;&amp; !studentJson.equals("")) {
            student = gson.fromJson(studentJson, Student.class);
        }
        return student;
    }

    public void deleteByID(String id) {
        redisTemplate.opsForValue().getOperations().delete(id);
    }
}

这里使用redisTemplate与Redis交互,用GSON把数据对象保存json格式

结果

http://127.0.0.1:8080/saveStudent新建数据
1
查询
1

删除数据
1

1

原文地址:https://blog.csdn.net/lilongsy/article/details/128606525

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_12425.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注