当前位置:   article > 正文

Springboot整合mybatis_plus + redis(使用原生的方式)_springboot+mybatisplus+redis框架

springboot+mybatisplus+redis框架

首次,创建一个springboot项目,勾选相应的依赖Lombok、Web

添加依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.projectlombok</groupId>
  11. <artifactId>lombok</artifactId>
  12. <optional>true</optional>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-test</artifactId>
  17. <scope>test</scope>
  18. </dependency>
  19. <!--easy-captcha 用于生成验证码-->
  20. <dependency>
  21. <groupId>com.github.whvcse</groupId>
  22. <artifactId>easy-captcha</artifactId>
  23. <version>1.6.2</version>
  24. </dependency>
  25. <!-- 使用mybatis-plus的起步依赖 -->
  26. <dependency>
  27. <groupId>com.baomidou</groupId>
  28. <artifactId>mybatis-plus-boot-starter</artifactId>
  29. <version>3.5.1</version>
  30. </dependency>
  31. <!-- 使用mysql的数据库 -->
  32. <dependency>
  33. <groupId>mysql</groupId>
  34. <artifactId>mysql-connector-java</artifactId>
  35. </dependency>
  36. <!-- druid连接池 -->
  37. <dependency>
  38. <groupId>com.alibaba</groupId>
  39. <artifactId>druid-spring-boot-starter</artifactId>
  40. <version>1.2.6</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>junit</groupId>
  44. <artifactId>junit</artifactId>
  45. <version>4.12</version>
  46. <scope>test</scope>
  47. </dependency>

添加配置文件:

  1. spring:
  2. redis:
  3. host: localhost
  4. port: 6379
  5. password:
  6. database: 0
  7. # lettuce连接池配置
  8. lettuce:
  9. pool:
  10. max-active: 8
  11. max-wait: 10000
  12. max-idle: 20
  13. min-idle: 10
  14. datasource:
  15. type: com.alibaba.druid.pool.DruidDataSource
  16. url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8
  17. username: root
  18. password: root
  19. driver-class-name: com.mysql.cj.jdbc.Driver
  20. druid:
  21. max-active: 10
  22. mybatis-plus:
  23. mapper-locations: classpath:mapper/*.xml
  24. type-aliases-package: net.wanho.entity
  25. configuration:
  26. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  27. map-underscore-to-camel-case: true
  28. #配置服务器
  29. server:
  30. port: 8088
  31. # servlet:
  32. # context-path: "/api/v1"

添加配置类:

  1. /**
  2. * 配置类
  3. */
  4. @Configuration
  5. public class RedisConfig {
  6. @Bean
  7. public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
  8. //创建RedisTemplate对象
  9. RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
  10. //设置连接工厂
  11. redisTemplate.setConnectionFactory(factory);
  12. //设置key的序列化方式
  13. GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
  14. redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
  15. redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  16. return redisTemplate;
  17. }
  18. }

实体类:

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @TableName("t_user")
  5. public class User implements Serializable {
  6. private static final long serialVersionUID = 144456L;
  7. private Long id ;
  8. private String name ;
  9. private String password ;
  10. }

Mapper:

  1. public interface UserMapper extends BaseMapper<User> {
  2. }

Service:

  1. public interface UserService extends IService<User>{
  2. User findById(Integer id);
  3. boolean removeById(Integer id);
  4. }
  1. @Service
  2. //@RequiredArgsConstructor
  3. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  4. // private final RedisTemplate<String, Object> redisTemplate;
  5. @Autowired
  6. private RedisTemplate<String, Object> redisTemplate;
  7. @Resource
  8. private UserMapper userMapper;
  9. @Override
  10. public User findById(Integer id) {
  11. //从redis中获取用户数据
  12. User user = (User) redisTemplate.opsForValue().get("user:id:" + id);
  13. System.out.println("redis数据库中的数据:" + user);
  14. if (!ObjectUtils.isEmpty(user)) {
  15. System.out.println("已经从redis中查到信息……");
  16. return user;
  17. }
  18. //查询数据库中的User对象
  19. user = userMapper.selectById(id);
  20. System.out.println("正在从数据库中的捞取数据……");
  21. //将用户信息存入redis
  22. redisTemplate.opsForValue().set("user:id:" + id, user, 2, TimeUnit.MINUTES);
  23. return user;
  24. }
  25. @Override
  26. public boolean removeById(Integer id) {
  27. int delete = userMapper.deleteById(id);
  28. //删除成功!
  29. if(delete!=0){
  30. return true;
  31. }
  32. return false;
  33. }
  34. }

controller:

  1. @RestController
  2. @RequestMapping("user")
  3. public class UserController {
  4. @Resource
  5. private UserService userService;
  6. @GetMapping("/{id}")
  7. public ResponseEntity<User> findById(@PathVariable Integer id){
  8. User byId = userService.findById(id);
  9. return ResponseEntity.ok(byId);
  10. }
  11. }

启动类:

  1. @SpringBootApplication
  2. @MapperScan("net.wanho.mapper")
  3. public class Day14SpringbootRedisApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Day14SpringbootRedisApplication.class, args);
  6. }
  7. }

完成!!!

特别注意:存入redis的数据一定要与取出的数据格式相同,否则会造成,只能存数据,不能取数据的现象!!!

 测试:

 

再次获取数据:
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/861295
推荐阅读
相关标签
  

闽ICP备14008679号