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

本文介绍了如何在SpringBoot项目中添加Lombok、Web依赖,配置Redis、MySQL和Druid连接池,以及如何使用MyBatisPlus进行数据库操作。还展示了如何在实体类、Mapper、Service和Controller中集成这些组件,以及启动类的编写和基本的测试过程。
摘要由CSDN通过智能技术生成

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

添加依赖:

 <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>

添加配置文件:

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"

添加配置类:

/**
 * 配置类
 */
@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;
    }
}

实体类:

@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:

public interface UserMapper  extends BaseMapper<User> {
}

Service:

public interface UserService extends IService<User>{
    User findById(Integer id);

    boolean removeById(Integer id);

}
@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:

@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);
    }
}

启动类:

@SpringBootApplication
@MapperScan("net.wanho.mapper")
public class Day14SpringbootRedisApplication {

    public static void main(String[] args) {
        SpringApplication.run(Day14SpringbootRedisApplication.class, args);
    }

}

完成!!!

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

 测试:

 

再次获取数据:
 

项目描述 在上家公司自己集成的一套系统,用了两个多月的时间完成的:Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级开发系统 Springboot作为容器,使用mybatis作为持久层框架 使用官方推荐的thymeleaf做为模板引擎,shiro作为安全框架,主流技术 几乎零XML,极简配置 两套UI实现(bootstrap+layer ui),可以自由切换 报表后端采用技术: SpringBoot整合SSM(Spring+Mybatis-plus+ SpringMvc),spring security 全注解式的权限管理和JWT方式禁用Session,采用redis存储token及权限信息 报表前端采用Bootstrap框架,结合Jquery Ajax,整合前端Layer.js(提供弹窗)+Bootstrap-table(数据列表展示)+ Bootstrap-Export(各种报表导出SQL,Excel,pdf等)框架整合Echars,各类图表的展示(折线图,饼图,直方图等),使用了layui的弹出层、菜单、文件上传、富文本编辑、日历、选项卡、数据表格等 Oracle关系型数据库以及非关系型数据库(Redis),Oracle 性能调优(PL/SQL语言,SQL查询优化,存储过程等),用Redis做中间缓存,缓存数据 实现异步处理,定时任务,整合Quartz Job以及Spring Task 邮件管理功能, 整合spring-boot-starter-mail发送邮件等, 数据源:druid 用户管理,菜单管理,角色管理,代码生成 运行环境 jdk8+oracle+redis+IntelliJ IDEA+maven 项目技术(必填) Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis 数据库文件 压缩包内 jar包文件 maven搭建 Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统 http://localhost:/8080/login admin admin Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值