首次,创建一个springboot项目,勾选相应的依赖Lombok、Web
添加依赖:
XML
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--easy-captcha 用于生成验证码-->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<!-- 使用mybatis-plus的起步依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 使用mysql的数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
添加配置文件:
java
spring:
redis:
host: localhost
port: 6379
password:
database: 0
# lettuce连接池配置
lettuce:
pool:
max-active: 8
max-wait: 10000
max-idle: 20
min-idle: 10
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
max-active: 10
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: net.wanho.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
#配置服务器
server:
port: 8088
# servlet:
# context-path: "/api/v1"
添加配置类:
java
/**
* 配置类
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
//创建RedisTemplate对象
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
//设置连接工厂
redisTemplate.setConnectionFactory(factory);
//设置key的序列化方式
GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}
实体类:
java
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_user")
public class User implements Serializable {
private static final long serialVersionUID = 144456L;
private Long id ;
private String name ;
private String password ;
}
Mapper:
java
public interface UserMapper extends BaseMapper<User> {
}
Service:
java
public interface UserService extends IService<User>{
User findById(Integer id);
boolean removeById(Integer id);
}
java
@Service
//@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
// private final RedisTemplate<String, Object> redisTemplate;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Resource
private UserMapper userMapper;
@Override
public User findById(Integer id) {
//从redis中获取用户数据
User user = (User) redisTemplate.opsForValue().get("user:id:" + id);
System.out.println("redis数据库中的数据:" + user);
if (!ObjectUtils.isEmpty(user)) {
System.out.println("已经从redis中查到信息......");
return user;
}
//查询数据库中的User对象
user = userMapper.selectById(id);
System.out.println("正在从数据库中的捞取数据......");
//将用户信息存入redis
redisTemplate.opsForValue().set("user:id:" + id, user, 2, TimeUnit.MINUTES);
return user;
}
@Override
public boolean removeById(Integer id) {
int delete = userMapper.deleteById(id);
//删除成功!
if(delete!=0){
return true;
}
return false;
}
}
controller:
java
@RestController
@RequestMapping("user")
public class UserController {
@Resource
private UserService userService;
@GetMapping("/{id}")
public ResponseEntity<User> findById(@PathVariable Integer id){
User byId = userService.findById(id);
return ResponseEntity.ok(byId);
}
}
启动类:
java
@SpringBootApplication
@MapperScan("net.wanho.mapper")
public class Day14SpringbootRedisApplication {
public static void main(String[] args) {
SpringApplication.run(Day14SpringbootRedisApplication.class, args);
}
}
完成!!!
特别注意:存入redis的数据一定要与取出的数据格式相同,否则会造成,只能存数据,不能取数据的现象!!!
测试:
再次获取数据: