创建的User实体类在非关系数据库(redis)中进行crud操作,用这个数据库语句use mybatis; drop table if exists `tb_user`; -- 创建测试表 CREATE TABLE `tb_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `user_name` varchar(20) NOT NULL COMMENT '用户名', `password` varchar(20) NOT NULL COMMENT '密码', `name` varchar(30) DEFAULT NULL COMMENT '姓名', `age` int(11) DEFAULT NULL COMMENT '年龄', `email` varchar(50) DEFAULT NULL COMMENT '邮箱', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- 插入测试数据 INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('1', 'zhangsan', '123456', '张三', '18', 'test1@qq.com'); INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('2', 'lisi', '123456', '李四', '20', 'test2@qq.com'); INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('3', 'wangwu', '123456', '王五', '28', 'test3@qq.com');
时间: 2023-06-11 16:04:13 浏览: 184
非关系数据库Redis通常用于缓存、存储session等用途,不适合用于保存大量的数据。如果您仍想在Redis中进行crud操作,您可以使用RedisTemplate来实现。具体操作方法可以参考Spring Data Redis文档。注意,在Redis中无法像关系型数据库一样使用SQL语句进行增删改查操作。
相关问题
搭建一个springboot 2.7.0版本的项目 使用德鲁伊连接池连接Orcale数据库mybatis-plus操作数据库创建redis连接池使用StringRedisTemplate对redis进行操作
1. 创建Spring Boot 2.7.0项目
可以通过Spring Initializr或者使用IDEA的Spring Initializr插件来创建Spring Boot项目。
2. 配置德鲁伊连接池
在pom.xml中添加以下依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
```
在application.properties中添加以下配置:
```properties
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:ORCL
spring.datasource.username=用户名
spring.datasource.password=密码
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
# 配置德鲁伊连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.testOnBorrow=true
spring.datasource.testOnReturn=false
spring.datasource.testWhileIdle=true
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
```
3. 配置mybatis-plus
在pom.xml中添加以下依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.3</version>
</dependency>
```
在application.properties中添加以下配置:
```properties
# mybatis-plus配置
mybatis-plus.mapper-locations=classpath*:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
mybatis-plus.global-config.id-type=auto
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
```
4. 配置Redis连接池
在pom.xml中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.7.0</version>
</dependency>
```
在application.properties中添加以下配置:
```properties
# Redis连接池配置
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0
spring.redis.timeout=3000
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.pool.max-wait=-1ms
```
5. 创建StringRedisTemplate对Redis进行操作
在Spring Boot中可以直接使用StringRedisTemplate对Redis进行操作,无需额外配置,只需要注入StringRedisTemplate即可。
```java
@Autowired
private StringRedisTemplate redisTemplate;
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
public String get(String key) {
return redisTemplate.opsForValue().get(key);
}
public void delete(String key) {
redisTemplate.delete(key);
}
```
以上就是使用Spring Boot 2.7.0版本搭建项目,使用德鲁伊连接池连接Orcale数据库,使用mybatis-plus操作数据库,创建Redis连接池,使用StringRedisTemplate对Redis进行操作的步骤。
非关系型数据库redis考点
非关系型数据库Redis是一个基于键值对的存储系统,它常被用作缓存、消息队列、会话存储等。在面试中,常见的Redis考点包括以下几个方面:
1. 数据结构:Redis支持多种数据结构,如字符串、哈希表、列表、集合和有序集合等。你需要了解每种数据结构的特点、用途以及相应的操作命令。
2. 缓存:Redis常被用作缓存,因为它具有高速读写能力和丰富的数据结构。你需要了解如何使用Redis作为缓存,如何设置过期时间、缓存击穿和雪崩的处理等。
3. 持久化:Redis提供了两种持久化方式,即RDB和AOF。你需要了解它们的原理、优缺点以及如何配置和使用。
4. 高可用性:在分布式系统中,Redis可以通过主从复制和哨兵机制来实现高可用性。你需要了解主从复制的原理、配置和故障转移的过程。
5. 分布式锁:Redis可以利用其原子性操作和分布式特性实现分布式锁。你需要了解如何使用Redis实现分布式锁以及可能遇到的问题和解决方案。
以上是非关系型数据库Redis的一些常见考点,希望对你有帮助!如需了解更多,请继续提问。