spring-boot+mybatis整合redis缓存,Java自学教程

application.yml

redis:

host: 127.0.0.1

port: 6379

jedis: #可省略不写

pool:

max-active: 8 #连接池最大连接数(使用负值表示没有限制)

max-wait: -1ms #连接池最大阻塞等待时间(使用负值表示没有限制)

max-idle: 8 #连接池中的最大空闲连接

min-idle: 0 #连接池中的最小空闲连接

timeout: 10000ms #连接超时时间(毫秒)

mybatis-generator.xml

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

<jdbcConnection driverClass=“com.mysql.cj.jdbc.Driver”

connectionURL=“jdbc:mysql://localhost:3306/zkstest?serverTimezone=UTC”

userId=“root”

password=“123456”>

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

“false” enableSelectByExample=“false” selectByExampleQueryId=“false”>

注意:此处生成的*mapping.xml名称可能会是*mapper.xml注意自行更改

启动类ZhouApplication.java

package com.zks;

import org.apache.ibatis.annotations.Mapper;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cache.annotation.EnableCaching;

@MapperScan(“com.zks.mapper”)

@SpringBootApplication

@EnableCaching

public class ZhouApplication {

public static void main(String[] args) {

SpringApplication.run(ZhouApplication.class, args);

}

}

数据库表

RedisController.java

package com.zks.controller;

import com.zks.entity.TUser;

import com.zks.service.TUserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.data.redis.core.ValueOperations;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

import java.util.concurrent.TimeUnit;

/**

  • @author :zhoukaishun

  • @date :Created in 2019/5/31 15:08

  • @description:${description}

  • @modified By:

  • @version: v e r s i o n version version

*/

@RestController

public class RedisController {

@Autowired

StringRedisTemplate stringRedisTemplate;

@Autowired

TUserService tUserService;

@Autowired

RedisTemplate redisTemplate;

@GetMapping("/redis")

public String redis(){

stringRedisTemplate.opsForValue().set(“zks”,“zhoukaishun”);

return stringRedisTemplate.opsForValue().get(“zks”);

}

@GetMapping("/getPersonById/{id}")

public String getPersonById(Model model, @PathVariable Integer id){

String key = “user_” + id;

ValueOperations<String, TUser> operations = redisTemplate.opsForValue();

boolean hasKey = redisTemplate.hasKey(key);

if(hasKey){

System.out.println("==从缓存中获得数据begin:"+System.currentTimeMillis()+"=");

long begin = System.currentTimeMillis();

TUser tuser = operations.get(key);

long end = System.currentTimeMillis();

System.out.println("=从缓存中获得数据end:"+System.currentTimeMillis()+"=");

System.out.println(“缓存查询时长为:”+(end-begin));

return tuser.toString();

}else{

System.out.println(“从数据库去取值begin:"+System.currentTimeMillis()+"=”);

long begin = System.currentTimeMillis();

TUser tuser = tUserService.getPersonById(id);

long end = System.currentTimeMillis();

System.out.println(“从数据库去取值end:"+System.currentTimeMillis()+"=”);

System.out.println(“数据库查询时长为:”+(end-begin));

operations.set(key, tuser);

return tuser.toString();

}

}

}

注:此处可先看redis()方法,用于简单测试redis是否可捕获我们设置的值

代码书写后启动无误后,启动本地redis

启动方法打开本地redis目录下shift+鼠标右键,进入命令行,输入  ./redis-server.exe redis.windows.conf    回车

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值