Springboot整合mybatis_plus + redis(使用原生的方式)

首次,创建一个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的数据一定要与取出的数据格式相同,否则会造成,只能存数据,不能取数据的现象!!!

测试:

再次获取数据:

相关推荐
lxyzcm17 分钟前
C++23新特性解析:[[assume]]属性
java·c++·spring boot·c++23
C++忠实粉丝1 小时前
Redis 介绍和安装
数据库·redis·缓存
迷糊的『迷』2 小时前
vue-axios+springboot实现文件流下载
vue.js·spring boot
ClouGence2 小时前
Redis 到 Redis 数据迁移同步
数据库·redis·缓存
苏三说技术2 小时前
Redis 性能优化的18招
数据库·redis·性能优化
Tttian6222 小时前
基于Pycharm与数据库的新闻管理系统(2)Redis
数据库·redis·pycharm
小池先生3 小时前
springboot启动不了 因一个spring-boot-starter-web底下的tomcat-embed-core依赖丢失
java·spring boot·后端
言之。3 小时前
redis延迟队列
redis
hanbarger3 小时前
mybatis框架——缓存,分页
java·spring·mybatis
苹果醋34 小时前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx