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. ...
随机推荐
- LR 两种录制:html与url
一直在使用LR,对于Html_based script和Url-based script 两种录制方式之间,要如何选择,仍是一知半解.最近测试时遇到同样的业务功能,两种录制方式的脚本,单次执行时间差别 ...
- 【转】关于编写WPF UserControl时提示The name 'InitializeComponent' does not exist in the current contextr的解决!
1.打开.csproj(工程)文件. 2.找到<Import Project="$(MSBuildBinPath)/Microsoft.CSharp.targets" /&g ...
- (待解决)IDEA配置JDBC查询数据库PreparedStatement pstmt = dbconn.prepareStatement(sql)出现空指针错误
package com.demo; import java.io.*; import java.sql.*; import java.util.*; import javax.servlet.*; i ...
- CQOI2018 九连环 打表找规律 fft快速傅里叶变换
题面: CQOI2018九连环 分析: 个人认为这道题没有什么价值,纯粹是为了考算法而考算法. 对于小数据我们可以直接爆搜打表,打表出来我们可以观察规律. f[1~10]: 1 2 5 10 21 4 ...
- 常见的网络命令--ping.hostname
hostname命令 作用:显示以及设置主机名 一. 显示系统主机名 第一种方式:hostname 第二种方式:cat /etc/sysconfig/ntework 使用举例: 从上面可以看到我的系 ...
- Apache web服务
1.apache 1> 世界上使用率最高的网站服务器,最高时可达70%:官方网站:apache.org 2> http 超文本协议 HTML超文本标记语言 3> URL 统一资源定位 ...
- 查询SYS_ORG_TB树的层级
WITH N(SYS_ORG_ID,SYS_ORG_NAME,LEVEL) AS( AS LEVEL FROM SYS_ORG_TB WHERE SYS_ORG_UPID IS NULL UNION ...
- apidoc利用代码注释书写文档
个人博客同步文章 https://mr-houzi.com/2018/07/... apidoc是一款利用源代码中注释来创建RESTful Web API文档的工具.apidoc可用于C#,Go,Da ...
- 条款4:确定对象被使用前已被初始化(Make sure that objects are initialized before they're used)
其实 无论学何种语言 ,还是觉得要养成先声明后使用,先初始化再使用. 1.永远在使用对象之前先将其初始化. 内置类型: 必须手工完成. 内置类型以外的:使用构造函数完成.确保每一个构造函数都将对象的一 ...
- CUDA & cuDNN环境配置
环境 python3.5 tensorflow 1.3 VUDA 8.0 cuDNN V6.0 1.确保GPU驱动已经安装 lspci | grep -i nvidia 通过此命令可以查看GPU信息 ...