springboot+Mybatis+redis整合CRUD

Spring boot 整合 redis

一、添加依赖

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

 
 
  • 1
  • 2
  • 3
  • 4

二、配置yml文件

server:
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root

  redis:
    host: #此处应填写服务器ip 通过上述命令 ip addr 可查看
    #redis端口
    port: 6379
    # Redis数据库索引(默认为0) 密码之类的 默认是没有的可以写
    database: 0

# 打印SQL语句
logging:
  level:
  	#改为自己的mapper包路径
    com.nebula.springboot_redis.mapper: debug

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

三、启动类中加入注解

@MapperScan(value = "com.nebula.springboot_redis.mapper") //指定要扫描的mapper包路径

@SpringBootApplication
public class SpringbootRedisApplication {

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

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

项目结构如下:

四、加入RedisConfig文件

@Configuration
public class RedisConfig {
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

实体类
@Data
public class Student implements Serializable {

    //编号
    private Integer id;

    //姓名
    private String name;

    //性别
    private String sex;

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Mapper
@Mapper
public interface StudentMapper {

    /**
     * 查询所有
     *
     * @return
     */
    @Select("select * from student")
    List<Student> findAll();

    /**
     * 根据id查询学生信息
     *
     * @param id id
     * @return
     */
    @Select("select * from student where id=#{id}")
    Student findById(Integer id);

    /**
     * 根据id删除学生信息
     *
     * @param id id
     * @return
     */
    @Delete("delete from student where id =#{id}")
    int del(Integer id);

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    @Update("update student set name =#{name},sex=#{sex} where id=#{id}")
    int modify(Student student);

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

Service
public interface StudentService {

    /**
     * 查询所有
     *
     * @return
     */
    List<Student> findAll();

    /**
     * 根据id查询学生信息
     *
     * @param id id
     * @return
     */
    Student findById(Integer id);


    /**
     * 根据id删除学生信息
     *
     * @param id id
     * @return
     */
    int del(Integer id);

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    int modify(Student student);
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

实现层
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    StudentMapper studentMapper;

    @Resource
    RedisTemplate redisTemplate;

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Student> findAll() {
        String key = "student";
        ListOperations<String, Student> operations = redisTemplate.opsForList();

        //缓存存在
        if (redisTemplate.hasKey(key)) {
            return operations.range(key, 0, -1);
        } else {
            //得到学生集合
            List<Student> list = studentMapper.findAll();
            operations.leftPushAll(key, list);
            return list;
        }
    }

    /**
     * 根据id查询学生信息
     *
     * @param id id
     * @return
     */
    @Override
    public Student findById(Integer id) {
        String key = "student_" + id;
        ValueOperations<String, Student> operations = redisTemplate.opsForValue();

        //缓存存在
        if (redisTemplate.hasKey(key)) {
            return operations.get(key);
        } else {
            //得到学生对象
            Student student = studentMapper.findById(id);
            //添加到缓存
            operations.set(key, student);
            return student;
        }
    }


    /**
     * 根据id删除学生信息
     *
     * @param id id
     * @return
     */
    @Override
    public int del(Integer id) {
        //删除数据库中的数据
        int count = studentMapper.del(id);

        //缓存存在
        String key = "student_" + id;
        if (redisTemplate.hasKey(key)) {
            //删除对应缓存
            redisTemplate.delete(key);
        }
        return count;
    }

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    @Override
    public int modify(Student student) {
        //修改数据库中的数据
        int count = studentMapper.modify(student);

        ValueOperations operations = redisTemplate.opsForValue();
        //缓存存在
        String key = "student_" + student.getId();
        if (redisTemplate.hasKey(key)) {
            //更新缓存
            Student stu = studentMapper.findById(student.getId());
            operations.set(key, stu);
        }
        return count;
    }
}


 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

控制层
@RestController
public class StudentController {

    @Resource
    StudentService studentService;

    @GetMapping("/student")
    public List<Student> findAll() {
        return studentService.findAll();
    }

    @GetMapping("/student/{id}")
    public Student findById(@PathVariable("id") Integer id) {
        return studentService.findById(id);
    }

    @PostMapping("/student/{id}")
    public int del(@PathVariable("id") Integer id) {
        return studentService.del(id);
    }

    @PutMapping("/student")
    public int modify(Student student) {
        return studentService.modify(student);
    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

运行,使用 Postman 请求查询所有方法

请求了一次数据库

查看 Redis 可视化工具

完成!!!下次再查询所有时将无需再重新请求数据库,而是直接从缓存中取数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值