当前位置:   article > 正文

Sringboot整合mybatis-plus+Redis_spring boot mybatispuls redis

spring boot mybatispuls redis

1.Mybatis自动生成代码
Windows下安装Redis
2.导入Redis相关依赖

		<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.编写application.yml文件

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/redis?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 1041370063
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    password: 123456
    timeout: 2000s
    lettuce:
      pool:
        max-wait: 60s
        max-idle: 10
        min-idle: 10
        max-activ: 8
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

4.编写RedisConfig配置类

@Configuration
public class RedisConfig {
    @Bean
    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

5.编写一个类,将MySQL数据库的数据添加到Redis缓存

@SpringBootTest
public class MySqlToRedis {
    @Autowired
    PeopleService peopleService;

    @Resource
    private RedisTemplate redisTemplate;
    private static final String ALL_USER="ALL_USER_LIST";
    @Test
    public void Initialized(){
        //查询数据库所有的用户
        List<People> peopleList=peopleService.list();
        //清除缓存中的用户数据
        redisTemplate.delete(ALL_USER);
        //将数据放到Redis缓存之中
        redisTemplate.opsForList().leftPushAll(ALL_USER,peopleList);

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

6.启动测试类,打开RDM可以查看到数据库的数据已存在于缓存中
在这里插入图片描述

在这里插入图片描述
7.Redis+MybatisPlus的增删查改

  • 7.1 mapper层:

    在PeopleMapper.java类添加注解@Repository

  • 7.2 service.impl层:重写mybatis-plus相关方法

@Service
@Slf4j
public class PeopleServiceImpl extends ServiceImpl<PeopleMapper, People> implements PeopleService {
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String ALL_USER="ALL_USER_LIST";
    @Autowired
    PeopleMapper peopleMapper;

    /*
    * 获取用户
    * */
    @Override
    public People getById(Serializable id) {
        //查询Redis缓存中的所有数据
        List<People> peopleList=redisTemplate.opsForList().range(ALL_USER,0,-1);
        if (peopleList!=null&&peopleList.size()>0) {
            for (People people : peopleList) {
                if (String.valueOf(people.getId()).equals(id)) {
                    log.info("Redis");
                    return people;
                }
            }
        }
        //查询数据库中的数据
        People people=peopleMapper.selectById(id);
        if(people!=null){
            //将数据插入Redis缓存之中
            redisTemplate.opsForList().leftPush(ALL_USER,people);
        }
        log.info("MySQL");
        return people;
    }

    /*
    * 删除用户
    * */
    @Override
    public boolean removeById(Serializable id) {
        //先删除数据库用户
        People people=peopleMapper.selectById(id);
        int result=peopleMapper.deleteById(id);
        //删除缓存
        if(result!=0){
            redisTemplate.opsForList().remove(ALL_USER,0,people);
            log.info("Redis");
            return true;
        }
        return false;
    }

    /*
    * 更新用户
    * */

    @Override
    public boolean updateById(People newPeople) {
        //更新数据库
        People people=peopleMapper.selectById(newPeople.getId());
        int result=peopleMapper.updateById(newPeople);
        if (result!=0){
            //先删除原来的缓存
            redisTemplate.opsForList().remove(ALL_USER,0,people);
            //将更新的用户加入缓存
            redisTemplate.opsForList().leftPush(ALL_USER,newPeople);
            return true;
        }
        return false;
    }
}

  • 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
  • 7.3 controller层
@RestController
public class PeopleController {
    @Autowired
    PeopleService peopleService;
    @GetMapping("/all")
    public List<People> getAllPeople(){
        return peopleService.list();
    }
    @GetMapping("/get/{id}")
    public People getPeople(@PathVariable Serializable id){
        return peopleService.getById(id);
    }
    @GetMapping("/delete/{id}")
    public boolean deletePeople(@PathVariable Serializable id){
        return peopleService.removeById(id);
    }
    @GetMapping("/update")
    public boolean updatePeople(){
        People people=new People();
        people.setId(1);
        people.setName("小末");
        people.setGender("女");
        return peopleService.updateById(people);
    }
}

  • 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

项目实例

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/962246
推荐阅读
相关标签
  

闽ICP备14008679号