引言:
本文主要分享了Redis如何在IDEA中部署,运行;模拟加入Redis的操作;
目录结构
1. 在IDEA中搭建Redis
1.1 创建项目
新建含有web的SpringBoot项目;
搭建项目参考:SpringBoot超详细笔记:https://blog.csdn.net/weixin_42601136/article/details/108396511
1.2 添加依赖(不够手动添加)
<dependencies>
<!-- 1、 Jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- 2、 Junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
1.3 测试环境是否搭建成功
@Test
public void test0() {
//连接Redis
Jedis jedis = new Jedis("127.0.0.1", 6379);
//获取kak
String kaka = jedis.get("kak");
log.info(kaka);
//创建datex
String datex = jedis.set("datex", new Date().toString());
//获取datex
String datex1 = jedis.get("datex");
log.info(datex1);
//关闭资源
jedis.close();
}
1.4 利用json存储数据
@Test
public void test1() {
JedisPool pool = new JedisPool("127.0.0.1", 6379);
Jedis jedis = pool.getResource();
Student student = new Student(1001, "kak", "man", "20");
String key = "student";
String field = "1001";
String s = JsonUtils.objectToJson(student);
String value = s;
jedis.hset(key, field, value);
String hget = jedis.hget(key, field);
log.info(hget);
jedis.close();
}
1.5 以byte的形式存储对象
@Test
public void test02() {
//连接redis
Jedis jedis = new Jedis("127.0.0.1", 6379);
String key = "Student";
Student value = new Student(1002, "taotao", "woman", "20");
//将key和value转换为byte[]
byte[] byteKey = SerializationUtils.serialize(key);
byte[] byteValue = SerializationUtils.serialize(value);
//将key和value存储到redis中
jedis.set(byteKey, byteValue);
//获取value值
byte[] bytes = jedis.get(byteKey);
//bytes反序列化为Student对象
Student stu = (Student) SerializationUtils.deserialize(bytes);
System.out.println(stu);
jedis.close();
}
1.6 加入连接池的操作
使用连接池操作,可以避免频繁的创建和销毁连接对象的消耗资源
@Test
public void test3(){
//创建连接池
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(100);//连接池中的最大活跃数
H:acyY[X[\]OB]\HHBH\H\]NBXYKXX[\B[\KX\\Y[X\\[\KY[[\KY[^[\N[\KXY[X[\K][][B[\K][UY\[[\^\[\[[Y]X[˙XK[][]]\Y[\[[Y]\[\KXB[\]K][\BB
X]YHB
XBBXX\Y[X[\[\[Y[Y[X]]\Y
\]Z\YH[CB]]HY[X\\Y[X\\iz!.byY\m]]\YB]]HUY\[Y\[B\YCBXX\Y[ [[Y[
H#Y\f9ake,B[Y[HY\[
Y[NYY[O[[
^[Y[Y\Nkh{l z(9khyg\Y[ Y[H][\Y[Y[\N]\Y[BCBb&蹪(yohCBY[^[\HY[^[\HH]Y[^[\J
N\Y[ Y[HY[X\\XQ^[\JY[^[\JN[Y[N^l!:f":/k9c%..ke,B[H][XY[/ai\Y\Y\[
Y[[]HY\NX
^[^
^[^Y\Y
JNCB]\Y[BCBCBOB]BL9ayaj9eyhmzghB]\HHBH\H\[BTH[ [[[[ΝH[Y[XYXY ]H\HUN ]I'mzgh]IXY BI]YH[[ XI SQI V [ XXHY[ ^HYH ^H[Y_H ^HH ^HYH XI]I[ OB]BLaTY[\B]\HHBH\H\]NBXYK\B[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