注意:此处生成的*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 回车
下图表示启动成功,可在redis-desktop-manager登陆查看(可有可无,看一下比较放心)
名称随意,没有密码(因为没设置所以没有密码)端口默认6379 测试连接
因为之前写的0
刚开始并没有zks这个key
访问 http://localhost:8080/redis/ 后如下图
现在说明我们项目是可以往redis中塞值的,下面我们结合数据库再看方法getPersonById
前提是我们要将我们的实体序列化
运行项目访问:http://localhost:8080/getPersonById/1
如下图:
后台打印:
重新刷新网页重新获取id为1的数据
查询效率显而易见。至此,完毕。
参考文档:
https://blog.csdn.net/qq_27317475/article/details/81188642#commentBox
https://blog.csdn.net/zhangcongyi420/article/details/82686702
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
![img](https://img-blog.csdnimg.cn/img_convert/5e63251d12107b70100ac7a6d31da616.jpeg)
最后
看完美团、字节、腾讯这三家的面试问题,是不是感觉问的特别多,可能咱们又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。
开篇有提及我可是足足背下了1000道题目,多少还是有点用的呢,我看了下,上面这些问题大部分都能从我背的题里找到的,所以今天给大家分享一下互联网工程师必备的面试1000题。
注意不论是我说的互联网面试1000题,还是后面提及的算法与数据结构、设计模式以及更多的Java学习笔记等,皆可分享给各位朋友
互联网工程师必备的面试1000题
而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题。
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门即可获取!
a学习笔记等,皆可分享给各位朋友
[外链图片转存中…(img-tRDLJxRz-1712074530972)]
互联网工程师必备的面试1000题
而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题。
[外链图片转存中…(img-qTMVeOX4-1712074530972)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门即可获取!