redis(12):springboot使用redis注解做缓存

1 新建springboot项目

redis(12):springboot使用redis注解做缓存_第1张图片

 

2 相关注解

@EnableCaching 在启动类上加上注解启动缓存

#作用在你要缓存的数据上

@Cacheable(key="#id",cacheNames="com.sxt.service.impl.MenuServiceImpl")

@Cacheput 解决脏读

@CachEvict(解决脏读)

@Cacheconfig(全局的配置缓存)

3 修改yml

#
spring:
  #数据源
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.222.132:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
    type: org.springframework.jdbc.datasource.DriverManagerDataSource
  #redis
  redis:
    host: 192.168.222.131
    password: 123456
    port: 6379
    jedis:
      pool:
        max-active: 20
        max-idle: 8
        min-idle: 0
        max-wait: 2000
#mybatis
mybatis:
  mapper-locations:
    - classpath:mapper/*Mapper.xml
  configuration:
    log-prefix: mybatis
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4 User类

package com.example.demo.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
    /**
     * 用户编号
     */
    private Integer id;

    /**
     * 用户姓名
     */
    private String name;

    /**
     * 用户地址
     */
    private String address;

    /**
     * 出生时间
     */
    private Date birth;

    /**
     * 是否删除1删除0未删除
     */
    private Integer flag;

    private static final long serialVersionUID = 1L;
}

5 创建UserMaper

package com.example.demo.mapper;


import com.example.demo.domain.User;

import java.util.List;

public interface UserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);

    List queryAllUser();
}

6 UserMapper.xml




    
        
        
        
        
        
        
        
    
    
        
        id, `name`, address, birth, flag
    
    
    
        
        delete from sys_user
        where id = #{id,jdbcType=INTEGER}
    
    
        
        insert into sys_user (`name`, address, birth,
        flag)
        values (#{name,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{birth,jdbcType=TIMESTAMP},
        #{flag,jdbcType=INTEGER})
    
    
        
        insert into sys_user
        
            
                `name`,
            
            
                address,
            
            
                birth,
            
            
                flag,
            
        
        
            
                #{name,jdbcType=VARCHAR},
            
            
                #{address,jdbcType=VARCHAR},
            
            
                #{birth,jdbcType=TIMESTAMP},
            
            
                #{flag,jdbcType=INTEGER},
            
        
    
    
        
        update sys_user
        
            
                `name` = #{name,jdbcType=VARCHAR},
            
            
                address = #{address,jdbcType=VARCHAR},
            
            
                birth = #{birth,jdbcType=TIMESTAMP},
            
            
                flag = #{flag,jdbcType=INTEGER},
            
        
        where id = #{id,jdbcType=INTEGER}
    
    
        
        update sys_user
        set `name` = #{name,jdbcType=VARCHAR},
        address = #{address,jdbcType=VARCHAR},
        birth = #{birth,jdbcType=TIMESTAMP},
        flag = #{flag,jdbcType=INTEGER}
        where id = #{id,jdbcType=INTEGER}
    

    

7 UserService

package com.example.demo.service;

import com.example.demo.domain.User;

import java.util.List;

public interface UserService{

    int deleteByPrimaryKey(Integer id);

    User insert(User user);

    User selectByPrimaryKey(Integer id);

    User updateByPrimaryKey(User user);

    List queryAllUser();
}

8 UserSerivceImpl

package com.example.demo.service.impl;


import com.example.demo.domain.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @CacheEvict(cacheNames = "user",key = "#id")
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return this.userMapper.deleteByPrimaryKey(id);
    }


    //    @CachePut(cacheNames = "user",key = "#user.id")
    @CachePut(cacheNames = "user",key = "#result.id+'-'+#result.flag")
    @Override
    public User insert(User user) {
        this.userMapper.insert(user);
        return user;
    }

    @Cacheable(cacheNames = "user",key = "#id")
    @Override
    public User selectByPrimaryKey(Integer id) {
        return this.userMapper.selectByPrimaryKey(id);
    }


    @CachePut(cacheNames = "user",key = "#user.id")
    @Override
    public User updateByPrimaryKey(User user) {
        int index = this.userMapper.updateByPrimaryKey(user);
        return user;
    }

    @Override
    public List queryAllUser() {
        return this.userMapper.queryAllUser();
    }
}

9 修改启动类

redis(12):springboot使用redis注解做缓存_第2张图片

 

9 测试

package com.example.demo;

import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;

@SpringBootTest
class ApplicationTests {

    @Autowired
    private UserService userService;

    @Test
    void contextLoads() {
        User user = new User(1,"小明","武汉",new Date(),1);
        userService.insert(user);
        System.out.println("操作成功");
    }

    @Test
    void update() {
        User user = new User(2,"小明","武汉北京",new Date(),0);
        userService.updateByPrimaryKey(user);
        System.out.println("操作成功");
    }

    @Test
    void query() {
        User user = userService.selectByPrimaryKey(2);
        System.out.println(user);
        System.out.println("操作成功");
    }

    @Test
    void delete(){
        this.userService.deleteByPrimaryKey(2);
        System.out.println("操作成功");
    }

}

你可能感兴趣的:(Redis,spring,boot,缓存,redis)