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

测试:

再次获取数据:

相关推荐
江深竹静,一苇以航几秒前
springboot3项目整合Mybatis-plus启动项目报错:Invalid bean definition with name ‘xxxMapper‘
java·spring boot
Karoku06634 分钟前
【企业级分布式系统】Zabbix监控系统与部署安装
运维·服务器·数据库·redis·mysql·zabbix
鹿屿二向箔39 分钟前
基于SSM(Spring + Spring MVC + MyBatis)框架的汽车租赁共享平台系统
spring·mvc·mybatis
豪宇刘42 分钟前
SpringBoot+Shiro权限管理
java·spring boot·spring
gorgor在码农1 小时前
Redis 热key总结
java·redis·热key
想进大厂的小王1 小时前
项目架构介绍以及Spring cloud、redis、mq 等组件的基本认识
redis·分布式·后端·spring cloud·微服务·架构
Java 第一深情1 小时前
高性能分布式缓存Redis-数据管理与性能提升之道
redis·分布式·缓存
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS医院管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源·intellij-idea
2402_857589362 小时前
SpringBoot框架:作业管理技术新解
java·spring boot·后端