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. ...
随机推荐
- MyEclipse 2015 安装到配置一站式备忘
目录 h1 2121 h1 2121
- os x 中出现message sent to deallocated instance 的错误总结
一般是程序中的某一个对象被release 了两次 一般情况下是与你定义的类型有关 这里面我的错误是吧 NSString 类型的变量的属性 设置为了 assign 了 目测与这个有关 补充object- ...
- COM技术开发(一)
COM :基本的接口(IX,IY), 组件的实现(CA),以及对组件的调用 #include "pch.h" #include <iostream> #include ...
- CPP-基础:快速排序
快速排序(Quicksort)是对冒泡排序的一种改进. 它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分 ...
- Python虚拟环境 之 virtualenv 与 virtualenvwrapper
在开发Python应用程序的时候,比如系统安装的Python3只有一个版本:3.6.所有第三方的包都会被 pip 安装到Python3的 site-packages 目录下. 如果我们要 ...
- [BZOJ3207]:花神的嘲讽(分块解法)
题目传送门 题目描述:背景花神是神,一大癖好就是嘲讽大J,举例如下:“哎你傻不傻的![hqz:大笨J]”“这道题又被J屎过了!!”“J这程序怎么跑这么快!J要逆袭了!”…… 描述这一天DJ在给吾等众蒟 ...
- Shell读取一个表达式并计算其结果
#!/bin/bash # 读取一个算数表达式并计算出结果 # 如果输入 # 5+50*3/20 + (19*2)/7 # 则结果为 # 17.929 read x printf "%.3f ...
- 2018美赛准备之路——Matlab基础——基本运算符号表示
π pi ln(x) log(x) lg(x) log10(x) log2(x) log2(x) 根号 sqrt(x) x的y次方 x^y e的y次方 exp(y)
- linux shell 自动判断操作系统release 然后连接FTP yum源的脚本
如何搭建本地yum源见附录① 如何搭建FTP yum源见附录② 脚本正文: #!/bin/sh# CenterOS config yumOSV=`rpm -q --qf %{version} cent ...
- vs2015 添加行件
VS自定义项目模板:[2]创建VSIX项目模板扩展 http://jingyan.baidu.com/article/bad08e1e9b08ee09c851210d.html