SpringBoot-数据访问之MyBatis与Redis

MyBatis导入

XML 复制代码
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.1.4</version>
</dependency>

MyBatis的自动配置

  1. 绑定配置文件 prefix = "mybatis"
  1. SqlSessionFactory
  1. SqlSession
  • 自动配置了 SqlSessionTemplate 组合了SqlSession
  1. AutoConfiguredMapperScannerRegistrar
  • @Import(AutoConfiguredMapperScannerRegistrar.class)
  • Mapper: 只要操作MyBatis的接口使用了 @Mapper 就会被自动扫描进来

MyBatis配置文件

  • config-location的所有全局配置文件 和 configuration配置 只可选其一
bash 复制代码
# 配置mybatis规则
mybatis:

# 配置mybatis的config.xml路径
#  config-location: classpath:mybatis/mybatis-config.xml

# 配置mybatis的mapper.xml路径
  mapper-locations: classpath:mybatis/mapper/*.xml
 
# 使用配置文件代替config.xml
  configuration:
    map-underscore-to-camel-case: true    #开启驼峰命名法

注解模式

  1. 只需要写mapper接口,不要写mapper.xml,使用注解写SQL语句
  • 省略@Mapper注解,启动类上标注@MapperScan("mapper包路径")
java 复制代码
@Mapper
public interface CityMapper {

    @Select("select * from city where id=#{id}")
    public City getById(Long id);

    public void insert(City city);

}
  1. 自增主键例子
  • xml写法
XML 复制代码
<!-- 
	useGeneratedKeys: 是否使用自增主键, 
	keyProperty:插入后自增主键返回时绑定的实体类属性名
-->
<insert id="addUser" useGeneratedKeys="true" keyProperty="id">
	insert into `user`(...)values(...)
</insert>
  • 注解写法
java 复制代码
@Insert(" insert ...")
@Options(useGeneratedKeys = true, keyProperty = "id")
int addUser();

MyBatis-Plus自动配置

  1. 导入依赖
XML 复制代码
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.4.1</version>
</dependency>
  1. MybatisPlusAutoConfiguration 自动配置类
  • 配置文件绑定:mybatis-plus: xxx
  1. SqlSessionFactory 自动配置好。底层是容器中默认的数据源
  1. mapperLocations 自动配置好的
  • 默认值:classpath*:/mapper/**/*.xml
  • 类路径的mapper文件夹下,任意路径的所有xml都是sql映射文件
  • 建议以后sql映射文件,放在此路径下
  1. SqlSessionTemplate 自动配置好了
  1. @Mapper 标注的接口也会被自动扫描
  • 建议直接 @MapperScan("mapper包路径") 批量扫描就行

Redis自动配置

  1. 导入依赖
XML 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. RedisAutoConfiguration 自动配置类
  • 配置文件配置 spring.redis.xxx
  • 默认连接工厂是LettuceConnectionConfiguration,也可以切换JedisConnectionConfiguration
  1. template操作redis
  • 自动注入了RedisTemplate<Object, Object>:k:v都是Object
  • 自动注入了StringRedisTemplate:k:v都是String
  1. 测试连接
  • 配置文件

    spring:
    redis:
    host: 主机名
    port: 6379
    password: user:password

  • 测试代码

java 复制代码
@Test
void testRedis(){
	ValueOperations<String, String> operations = redisTemplate.opsForValue();
	operations.set("hello","world");
	String hello = operations.get("hello");
	System.out.println(hello);
}
  1. 切换至jedis连接
  • 导入jedis
XML 复制代码
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>
  • 配置文件 client-type

    spring:
    redis:
    host:
    port: 6379
    password:
    client-type: jedis

相关推荐
ZGG00327 分钟前
MySQL 索引详解:B+ 树、聚簇索引与最左前缀
java·数据库·mysql
萧瑟余晖37 分钟前
Java深入解析篇五十四之对象模型详解
java·开发语言
张文是假的啊1 小时前
Java | record | Controller逻辑
java·开发语言
勿忘,瞬间1 小时前
Mybatis高阶
java·数据库·mybatis
嘻哈baby2 小时前
Go 函数中的参数为什么不支持默认值?
java·开发语言·jvm
慧都小项3 小时前
MyEclipse 2026:当 Agent、MCP 与 Java 26 进入 Eclipse 工作流
java·eclipse·springboot·copilot·myeclipse
摇滚侠3 小时前
《SpringBoot 3:入门与应用实战》第 14 章 打包与部署 Spring Boot 应用打包 阅读笔记 41
spring boot·笔记·后端
大梦想家a3 小时前
新能源汽车共享充电桩收费管理系统的设计与实现(SpringBoot+MyBatis-Plus+Vue,前后台分离完整源码)
spring boot·汽车·mybatis
CoderYanger3 小时前
前端基础——JavaScript(基础语法)(下篇)
java·开发语言·前端·javascript·程序人生·面试·职场和发展
京师20万禁军教头3 小时前
39面向对象(高级)-设计模式
java·开发语言·设计模式