redis mysql mybatis_SpringBoot + MySQL + MyBatis 整合 Redis 实现缓存操作

该博客介绍了如何在SpringBoot项目中整合Redis、MySQL和MyBatis,实现缓存操作。通过配置Redis连接,利用注解和XML映射文件进行数据操作,并展示了在Service层如何进行数据的缓存读取和更新操作。
摘要由CSDN通过智能技术生成

本地安装 Redis

项目结构:

SpringBootRedis 工程项目结构如下:

controller - Controller 层

dao - 数据操作层

model - 实体层

service - 业务逻辑层

Application - 启动类

resources 资源文件夹

application.properties - 应用配置文件,应用启动会自动读取配置

generatorConfig.xml - mybatis 逆向生成配置(这里不是本文只要重点,所以不进行介绍)

mapper 文件夹

StudentMapper.xml - mybatis 关系映射 xml 文件

项目工程代码详情

pom.xml 配置

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.7.RELEASE

com.example

demo

0.0.1-SNAPSHOT

war

demo

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-tomcat

provided

org.springframework.boot

spring-boot-starter-test

test

mysql

mysql-connector-java

5.1.46

org.springframework.boot

spring-boot-starter-data-redis

2.1.6.RELEASE

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

com.google.code.gson

gson

2.8.5

org.springframework.boot

spring-boot-maven-plugin

org.mybatis.generator

mybatis-generator-maven-plugin

1.3.7

${basedir}/src/main/resources/generatorConfig.xml

true

mysql

mysql-connector-java

8.0.15

application.properties 配置

spring:

datasource:

url: jdbc:mysql://localhost:3306/mydb?useSSL=true

username: oukele

password: oukele

driver-class-name: com.mysql.jdbc.Driver

# 配置 redis

redis:

# redis 数据库索引(默认为0)

database: 0

# redis 服务地址

host: 127.0.0.1

# redis 连接端口

port: 6379

# redis 服务器链接密码 (默认为空)

password:

# 连接超时时间 (毫秒)

timeout: 5000

# 配置 redis 连接池

jedis:

pool:

# 连接池最大连接数 (使用负值表示没有限制)

max-active: 8

# 连接池最大阻塞等待时间(使用负值表示没有限制)

max-wait: -1

# 连接池的最大空闲连接

max-idle: 8

# 连接池中 最小空闲连接

min-idle: 0

# 配置 mybatis

mybatis:

# 设置 实体类所在的包名

typeAliasesPackage: com.example.demo.model

# mybatis xml 映射关系

mapper-locations: classpath:mapper/*.xml

项目结构图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值