Spring boot 与 Redis 与 mybatis-plus测试

本文介绍了如何在Springboot项目中集成Mybatis-Plus进行数据库操作,详细步骤包括添加依赖、配置数据库连接、生成相关文件并进行测试。接着,展示了如何引入Redis并配置相关设置,以及进行简单的存取操作。最后提醒启动类需添加@MapperScan注解。
摘要由CSDN通过智能技术生成

Spring boot 与 mybatis-plus

  1. 建立Spring boot项目
    在这里插入图片描述
    在这里插入图片描述
    添加依赖
    在这里插入图片描述

  2. 连接数据库
    在这里插入图片描述
    在这里插入图片描述

  3. 生成mapper、service、xml等文件

在这里插入图片描述
应该是com.example.demo 写错了写成了/在这里插入图片描述在这里插入图片描述
文件已经生成好!
在这里插入图片描述
4. 导入plus包

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
  1. 链接数据库 配置文件application.yml

spring:
  datasource: #数据源

    url: jdbc:mysql://localhost:3306/student
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

在这里插入图片描述

  1. 测试
package com.example.demo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.model.Stu;
import com.example.demo.service.StuService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.lang.model.element.VariableElement;

@SpringBootTest
class Demo1ApplicationTests {



    @Autowired
    StuService service;

    @Test
    public void demo(){
        Stu stu = service.getOne(new QueryWrapper<Stu>().eq("id", 1));
        System.out.println(stu);

        boolean save = service.save(stu);



    }
}

成功!!!
在这里插入图片描述

Spring boot 与 Redis

  1. 导入redis依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
  1. 建立redis配置文件,防止乱码
package com.zaz.config;


import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置类
 *
 * @program: springbootdemo
 */
//@EnableCaching //开启注解
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        //key 的序列化
        template.setKeySerializer(new StringRedisSerializer());
        //value 的序列化
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //hash类型 key
        template.setHashKeySerializer(new StringRedisSerializer());
        //value
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        //注入连接工厂
        template.setConnectionFactory(factory);
        return template;
    }

}
  1. 链接Redis 配置文件application.yml
spring:
  redis:
   host: localhost
   port: 6379
#    jedis:
#      pool:
#        max-wait: 30000  #最大阻塞等待时间
#        max-active: 100  #连接池最大连接数
#        max-idle: 20 #连接池中最大空闲连接
#        min-idle: 0
#    timeout: 3000 #连接超时
  1. 测试
package com.zaz.user.service;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;

/**
 * @author 
 * @create 
 */
@Slf4j
@SpringBootTest
public class RedisTest {

    @Resource
    RedisTemplate redisTemplate;

    @Test
    public void RedisTest(){
        redisTemplate.opsForValue().set("index","就是个索引哦!!!!!");
        Object index = redisTemplate.opsForValue().get("index");

        log.info(String.valueOf(index));
    }
}

运行成功!!!
在这里插入图片描述

启动类要加上

@MapperScan("com.example.demo.mapper")

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黎明之道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值