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 回车