创建SpringBoot项目
在线创建方式
网址:https://start.spring.io/
然后创建Controller、Mapper、Service包
SpringBoot整合Redis
引入Redis依赖
org.springframework.boot
spring-boot-starter-data-redis
完整pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
com.cyb
chenyb-mobile-redis
0.0.1-SNAPSHOT
chenyb-mobile-redis
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
io.projectreactor
reactor-test
test
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-maven-plugin
设置Redis的Template
RedisConfig.java
package com.cyb.mobile.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @ClassName:RedisConfig
* @Description:Redis配置类
* @Author:chenyb
* @Date:2020/8/16 11:48 下午
* @Versiion:1.0
*/
@Configuration //当前类为配置类
public class RedisConfig {
@Bean //redisTemplate注入到Spring容器
public RedisTemplate redisTemplate(RedisConnectionFactory factory){
RedisTemplate redisTemplate=new RedisTemplate<>();
RedisSerializer redisSerializer = new StringRedisSerializer();
redisTemplate.setConnectionFactory(factory);
//key序列化
redisTemplate.setKeySerializer(redisSerializer);
//value序列化
redisTemplate.setValueSerializer(redisSerializer);
//value hashmap序列化
redisTemplate.setHashKeySerializer(redisSerializer);
//key hashmap序列化
redisTemplate.setHashValueSerializer(redisSerializer);
return redisTemplate;
}
}
设置Redis连接信息
# 连接的那个数据库
spring.redis.database=0
# redis服务的ip地址
spring.redis.host=192.168.199.142
# redis端口号
spring.redis.port=6379
# redis的密码,没设置过密码,可为空
spring.redis.password=12345678
Redis工具类
redisTemplate API
opsForValue ==》String
opsForSet ==》Set
opsForHash ==》hash
opsForZset ==》SortSet
opsForList ==》list队列
RedisUtils.java
package com.cyb.mobile.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @ClassName:RedisUtils
* @Description:Redis工具类
* @Author:chenyb
* @Date:2020/8/17 12:05 上午
* @Versiion:1.0
*/
@Service
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
private static double size = Math.pow(2, 32);
/**
* 写入缓存
*
* @param key
* @param offset 位 8Bit=1Byte
* @return
*/
public boolean setBit(String key, long offset, boolean isShow) {
boolean result = false;
try {
ValueOperations operations = redisTemplate.opsForValue();
operations.setBit(key, offset, isShow);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入缓存
*
* @param key
* @param offset
* @return
*/
public boolean getBit(String key, long offset) {
boolean result = false;
try {
ValueOperations operations = redisTemplate.opsForValue();
result = operations.getBit(key, offset);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value) {
boolean result = false;
try {
ValueOperations operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入缓存设置时效时间
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 批量删除对应的value
*
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 删除对应的value
*
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 判断缓存中是否有对应的value
*
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 读取缓存
*
* @param key
* @return
*/
public Object get(final String key) {
Object result = null;
ValueOperations operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 哈希 添加
*
* @param key
* @param hashKey
* @param value
*/
public void hmSet(String key, Object hashKey, Object value) {
HashOperations hash = redisTemplate.opsForHash();
hash.put(key, hashKey, value);
}
/**
* 哈希获取数据
*
* @param key
* @param hashKey
* @return
*/
public Object hmGet(String key, Object hashKey) {
HashOperations hash = redisTemplate.opsForHash();
return hash.get(key, hashKey);
}
/**
* 列表添加
*
* @param k
* @param v
*/
public void lPush(String k, Object v) {
ListOperations list = redisTemplate.opsForList();
list.rightPush(k, v);
}
/**
* 列表获取
*
* @param k
* @param l
* @param l1
* @return
*/
public List
控制层
RedisController.java
package com.cyb.mobile.controller;
import com.cyb.mobile.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName:TestController
* @Description:Redis控制器
* @Author:chenyb
* @Date:2020/8/17 12:07 上午
* @Versiion:1.0
*/
@RestController
public class RedisController {
@Autowired
private RedisUtils redisUtils;
@RequestMapping("setAndGet")
public String test(String k,String v){
redisUtils.set(k,v);
return (String) redisUtils.get(k);
}
}
SpringBoot整合Mybatis
添加依赖
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.3
mysql
mysql-connector-java
com.alibaba
druid
1.1.23
完整pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
com.cyb
chenyb-mobile-redis
0.0.1-SNAPSHOT
chenyb-mobile-redis
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
io.projectreactor
reactor-test
test
org.springframework.boot
spring-boot-starter-data-redis
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.3
mysql
mysql-connector-java
com.alibaba
druid
1.1.23
org.springframework.boot
spring-boot-maven-plugin
设置配置文件
application.properties
# 连接的那个数据库
spring.redis.database=0
# redis服务的ip地址
spring.redis.host=192.168.199.142
# redis端口号
spring.redis.port=6379
# redis的密码,没设置过密码,可为空
spring.redis.password=12345678
# 端口号
server.port=8081
# ========================数据库相关配置=====================
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/nba?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
# 使用阿里巴巴druid数据源,默认使用自带
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#开启控制台打印sql
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# mybatis 下划线转驼峰配置,两者都可以
# mybatis.configuration.mapUnderscoreToCamelCase=true
mybatis.configuration.map-underscore-to-camel-case=true
# 配置扫描
mybatis.mapper-locations=classpath:mapper/*.xml
# 实体类所在的包别名
mybatis.type-aliases-package=com.cyb.mobile.domain
启动类上添加扫描路径
NbaPlayer.java(实体类)
NbaPlayerMapper.xml
NbaPlayerMapper.java
NbaPlayerService.java
NbaPlayerServiceImpl.java
控制器(Controller)
测试
redis作为mybatis缓存
用户第一次访问的时候获取数据库的值,再次访问时直接从缓存中获取数据
设置缓存过期时间
代码演示
添加FastJSON依赖
com.alibaba
fastjson
1.2.73
@RequestMapping("test")
public Object test(){
//step1 先从redis中取
String strJson=(String) redisUtils.get("nbaPlayerCache");
if (strJson==null){
System.out.println("从db取值");
// step2如果拿不到则从DB取值
List listNbaPlayer=nbaPlayerService.ListNbaPlayer();
// step3 DB非空情况刷新redis值
if (listNbaPlayer!=null){
redisUtils.set("nbaPlayerCache", JSON.toJSONString(listNbaPlayer));
return listNbaPlayer;
}
return null;
}else
{
System.out.println("从redis缓存取值");
return JSONObject.parseArray(strJson,NbaPlayer.class);
}
}
注意
项目8080是对外端口(向外部暴露的端口),区别于内部进程号,查内部端口用ps -ef|grep port,查外部端口用lsof -i:port
压测工具
上面我们已经SpringBoot整合Redis和Mybatis,但是无法知道具体QPS多少,此时我们可以使用压测工具来测压,比如ab
资料下载
SpringBoot整合Redis、Mybatis下载#
链接: https://pan.baidu.com/s/198yGCWEG_vtJK6Q5DYIAnw 提取码: 65e7
作者:陈彦斌
出处:https://www.cnblogs.com/chenyanbin/
关注微信公众号:互联网架构师,在后台回复:2T,可以获取我整理的教程,都是干货。
猜你喜欢
1、GitHub 标星 3.2w!史上最全技术人员面试手册!FackBoo发起和总结
2、如何才能成为优秀的架构师?
3、从零开始搭建创业公司后台技术栈
4、程序员一般可以从什么平台接私活?
5、37岁程序员被裁,120天没找到工作,无奈去小公司,结果懵了...
6、滴滴业务中台构建实践,首次曝光
7、不认命,从10年流水线工人,到谷歌上班的程序媛,一位湖南妹子的励志故事
8、15张图看懂瞎忙和高效的区别
9、2T架构师学习资料干货分享