redis连接数据库进行操作
该项目需要的类目录

1.首先我们需要创建我们的实体类

2.放置我们的dao层,在里面写入方法

3.配置类Appconfig需要加入我们的JdbcTemplate方法,因为我们用的是spring,所以需要手动添加@Bean
package com.lichuang.redislearn.config; import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource; @Configuration
@ComponentScan
@EnableCaching
public class AppConfig { @Bean
public RedisStandaloneConfiguration redisStandaloneConfiguration(){
RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration("101.132.107.145",);
return standaloneConfiguration;
}
@Bean
public RedisConnectionFactory redisConnectionFactory(){
LettuceConnectionFactory lettuceConnectionFactory= new LettuceConnectionFactory(redisStandaloneConfiguration());
return lettuceConnectionFactory;
}
@Bean
public StringRedisTemplate stringRedisTemplate(){
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory());
return stringRedisTemplate;
} @Bean
public CacheManager cacheManager(){
RedisCacheConfiguration redisCacheConfiguration=
RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
RedisCacheWriter redisCacheWriter=RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory());
RedisCacheManager redisCacheManager=new RedisCacheManager(redisCacheWriter,redisCacheConfiguration);
return redisCacheManager;
} @Bean
public JdbcTemplate jdbcTemplate(){
DriverManagerDataSource dataSource=new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUsername("root");
dataSource.setPassword("");
dataSource.setUrl("jdbc:mysql://localhost:3306/docker-test?serverTimezone=UTC");
return new JdbcTemplate(dataSource);
}
}
4.CacheService里面注入JdbcTemplate,写查询方法
@Service
public class CacheService {
@Autowired
private JdbcTemplate jdbcTemplate; @Cacheable(cacheNames = "person")
public Person selectById(int id){
System.out.println("缓存中如果没有,则执行此方法添加缓存");
return jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{id}, new RowMapper<Person>() {
@Override
public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
Person person=new Person();
person.setName(rs.getString("name"));
person.setAge(rs.getInt("age"));
person.setId(rs.getInt("id"));
return person;
}
});
}
5.操作main方法
public class RedisServer {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(AppConfig.class);
StringRedisTemplate redisTemplate=applicationContext.getBean(StringRedisTemplate.class);
// redisTemplate.opsForValue().set("name","test");
CacheService cacheService=applicationContext.getBean(CacheService.class);
for (int i=;i<;i++){
System.out.println(cacheService.selectById(i).getName());
}
}
}
6.application.yml里面需要加入我们连接数据库的属性
spring:
application:
name: redis-server
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password:
url: jdbc:mysql://localhost:3306/docker-test?serverTimezone=UTC
redis:
host: 101.132.107.145
port:
7.所需的依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter'
// implementation 'org.springframework.cloud:spring-cloud-starter-config'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
compileOnly group: 'org.projectlombok', name: 'lombok'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.58'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'mysql:mysql-connector-java'
compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '2.0.1'
}
连接服务器进行操作redis
首先需要打开我们的redis容器客户端
docker exec -it 738d6038d618 redis-cli
进入redis内部进行操作数据库

完成!
redis连接数据库进行操作的更多相关文章
- redis的一些操作
public class WnsRedisFactory { private static Cache pool = null; private static JedisConnectionFacto ...
- python之redis和memcache操作
Redis 教程 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据 ...
- redis的hash操作在集中式session中的应用
在集群部署时,为了高可用性的目的,往往把session进行共享,共享分为两种:session复制和集中式管理. redis在session集中式管理中可以起到比较大的作用. 制约session集中式共 ...
- Redis客户端API操作 Jedis详解
redis是一个著名的key-value存储系统,也是nosql中的最常见的一种.其实,个人认为,redis最强大的地方不在于其存储,而在于其强大的缓存作用. 我们可以把它想象成一个巨大的(多借点集群 ...
- Spring Boot 整合 Redis 实现缓存操作
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』 本文提纲 ...
- 缓存数据库-redis数据类型和操作(list)
转: 狼来的日子里! 奋发博取 缓存数据库-redis数据类型和操作(list) 一:Redis 列表(List) Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列表的头部( ...
- redis键值操作
1.1. redis键值操作 1.1.1. keys patten 查询相应的key 可以精确的查,也可以模糊的查 1.1.1.1. 通配符:* ? [] 在redis里,模糊查询key的时候有3个通 ...
- redis 批量删除操作
redis 批量删除操作 需要在redis里面清空一批数据,redis没有支持通配符删除, 只有del key1 key2 ... 但是可以通配符获取 KEYS PATTERN 然后利用linux管道 ...
- thinkphp5.0上对redis的具体操作
一.环境搭建 首先先安装composer.thinkphp5.0版本.和redis的windows版本的redis程序或者linux版本的redis程序,linux安装教程: https://www. ...
随机推荐
- loadrunner11报错:Error -27780
LR回放https协议脚本失败:[GENERAL_MSG_CAT_SSL_ERROR]connect to host "XXX" failed:[10054] Connection ...
- 机器学习在SAP Cloud for Customer中的应用
关于机器学习这个话题,我相信我这个公众号1500多位关注者里,一定有很多朋友的水平比Jerry高得多.如果您看过我以前两篇文章,您就会发现,我对机器学习仅仅停留在会使用API的层面上. 使用Java程 ...
- uva1439 Exclusive Access 2
感觉这道题读题有点难..似乎和现实联系的比较密切1.每个process的两个资源可以顺序反一下2.p->q,q->s不可以同时进行 p->q,p->s可以 输出最长等待链输出每 ...
- python之路——内置函数和匿名函数
阅读目录 楔子 内置函数 匿名函数 本章小结 楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们 ...
- QT +菜单栏和工具栏
#include "mainwindow.h" #include <QMenuBar>//菜单栏需要的头文件 #include <QMenu>//菜单 #i ...
- Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1146
问题介绍: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1146 MySql语法错误, 但是错 ...
- saltstack入门个人笔记
offical website reference1 reference2 install apt-get install python-software-properties apt install ...
- PowerShell(PHPStorm terminal with PowerShell)运行git log中文乱码
解决方案: 1)以管理员身份运行PowerShell 2)新建一个针对PowerShell的Pofile文件 New-Item -Path $Profile -ItemType file -Force ...
- IP封包协议头/TCP协议头/TCP3次握手/TCP4次挥手/UDP协议头/ICMP协议头/HTTP协议(请求报文和响应报文)/IP地址/子网掩码(划分子网)/路由概念/MAC封包格式
IP协议头IP包头格式: 1.版本号:4个bit,用来标识IP版本号.这个4位字段的值设置为二进制的0100表示IPv4,设置为0110表示IPv6.目前使用的IP协议版本号是4. 2.首部长度:4个 ...
- 一篇文章掌握nightwatch自动化测试
nightwatch.js是一个web-ui自动化测试框架,被vue-cli深度整合进来.如果一个项目是基于vue-cli搭建的,基本可以做到开箱即用. 但是我们不可能一直都使用vue-cli.因为它 ...