jedis连接mysql_Java中的使用及连接Redis数据库(附源码)

引言:

本文主要分享了Redis如何在IDEA中部署,运行;模拟加入Redis的操作;

目录结构

df9e51b4a0ada88e70c50ce40c6f7bec.png

1. 在IDEA中搭建Redis

1.1 创建项目

新建含有web的SpringBoot项目;

搭建项目参考:SpringBoot超详细笔记:https://blog.csdn.net/weixin_42601136/article/details/108396511

af2f74b106d1c9b6b5186af60d12c91f.png

1.2 添加依赖(不够手动添加)

redis.clients

jedis

2.9.0

junit

junit

4.12

org.springframework.boot

spring-boot-starter-jdbc

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.1.2

mysql

mysql-connector-java

runtime

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

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();

}

ed6dc6b92fbab6a11dbfa60e7dda7ab0.png

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();

}

285903c4e9158c6a9d6509c9a90c954c.png

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();

}

ec688c7a69eb736e7a537908e77ca115.png

1.6 加入连接池的操作

使用连接池操作,可以避免频繁的创建和销毁连接对象的消耗资源

@Test

public void test3(){

//创建连接池

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();

poolConfig.setMaxTotal(100);//连接池中的最大活跃数

poolConfig.setMaxIdle(10);//最大空闲数

poolConfig.setMinIdle(5);//最小空闲数

poolConfig.setMaxWaitMillis(3000);//连接池空了,3000毫秒后没有获取Jedis对象,超时

//创建连接池

JedisPool pool = new JedisPool(poolConfig,"127.0.0.1",6379);

//通过连接池获取Jedis对象

Jedis resource = pool.getResource();

//获取上面添加的datex

String value = resource.get("datex");

log.info(value);

resource.close();

}

02fffefadcf77a5739896319eba582bd.png

1.7 管道操作

Redis的管道操作,执行一个命令需要先发送请求到Redis,需要经历网络延迟,Redis还需给客户端一个响应;如果需要一次操作多个命令,可以通过管道,将命令放到客户端的一个Pipeline中,之后一次将命令发送到服务端,服务端一次性返回到客户端;

@Test

public void test3(){

//创建连接池

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();

poolConfig.setMaxTotal(100);//连接池中的最大活跃数

poolConfig.setMaxIdle(10);//最大空闲数

poolConfig.setMinIdle(5);//最小空闲数

poolConfig.setMaxWaitMillis(3000);//连接池空了,3000毫秒后没有获取Jedis对象,超时

//创建连接池

JedisPool pool = new JedisPool(poolConfig,"127.0.0.1",6379);

//通过连接池获取Jedis对象

Jedis resource = pool.getResource();

//获取上面添加的datex

String value = resource.get("datex");

log.info(value);

resource.close();

}

267e6ad1ed66bc2a12b607f92d4726e3.png

2. 从数据中查询数据

场景:

将数据存入Redis中,避免访问量过大,造成数据库奔溃;

2.1 生成实体类

@Test

public void test3(){

//创建连接池

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();

poolConfig.setMaxTotal(100);//连接池中的最大活跃数

poolConfig.setMaxIdle(10);//最大空闲数

poolConfig.setMinIdle(5);//最小空闲数

poolConfig.setMaxWaitMillis(3000);//连接池空了,3000毫秒后没有获取Jedis对象,超时

//创建连接池

JedisPool pool = new JedisPool(poolConfig,"127.0.0.1",6379);

//通过连接池获取Jedis对象

Jedis resource = pool.getResource();

//获取上面添加的datex

String value = resource.get("datex");

log.info(value);

resource.close();

}

2.2 生成实体模板类

package com.sx.kak.po;

import java.util.ArrayList;

import java.util.List;

public class StudentExample {

protected String orderByClause;

protected boolean distinct;

protected List oredCriteria;

public StudentExample() {

oredCriteria = new ArrayList();

}

public void setOrderByClause(String orderByClause) {

this.orderByClause = orderByClause;

}

public String getOrderByClause() {

return orderByClause;

}

public void setDistinct(boolean distinct) {

this.distinct = distinct;

}

public boolean isDistinct() {

return distinct;

}

public List getOredCriteria() {

return oredCriteria;

}

public void or(Criteria criteria) {

oredCriteria.add(criteria);

}

public Criteria or() {

Criteria criteria = createCriteriaInternal();

oredCriteria.add(criteria);

return criteria;

}

public Criteria createCriteria() {

Criteria criteria = createCriteriaInternal();

if (oredCriteria.size() == 0) {

oredCriteria.add(criteria);

}

return criteria;

}

protected Criteria createCriteriaInternal() {

Criteria criteria = new Criteria();

return criteria;

}

public void clear() {

oredCriteria.clear();

orderByClause = null;

distinct = false;

}

protected abstract static class GeneratedCriteria {

protected List criteria;

protected GeneratedCriteria() {

super();

criteria = new ArrayList();

}

public boolean isValid() {

return criteria.size() > 0;

}

public List getAllCriteria() {

return criteria;

}

public List getCriteria() {

return criteria;

}

protected void addCriterion(String condition) {

if (condition == null) {

throw new RuntimeException("Value for condition cannot be null");

}

criteria.add(new Criterion(condition));

}

protected void addCriterion(String condition, Object value, String property) {

if (value == null) {

throw new RuntimeException("Value for " + property + " cannot be null");

}

criteria.add(new Criterion(condition, value));

}

protected void addCriterion(String condition, Object value1, Object value2, String property) {

if (value1 == null || value2 == null) {

throw new RuntimeException("Between values for " + property + " cannot be null");

}

criteria.add(new Criterion(condition, value1, value2));

}

public Criteria andIdIsNull() {

addCriterion("id is null");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值