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

相关推荐
开发小能手-roy7 分钟前
Java集合框架选型指南:从ArrayList到ConcurrentSkipListMap
java·开发语言
凡人叶枫22 分钟前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫27 分钟前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
chushiyunen43 分钟前
java中的路径处理、左右斜杠
java·开发语言·python
星落zx1 小时前
Spring Boot 多模型集成:优雅调用全球主流大模型
人工智能·spring boot·chatgpt
yyxx4121231 小时前
上海企业如何选择专业的钉钉服务商
java·大数据·人工智能·钉钉
一杯奶茶¥1 小时前
水果销售网站 CRM客户信息管理系统 超市管理系 酒店管理系统 健身房管理系统 在线音乐网站 校园招聘系统
java·vue.js·spring boot·mysql·spring·java项目
重生之后端学习1 小时前
Java入门
java·开发语言·职场和发展
碧海蓝天20221 小时前
C++法则24:在标准 C++ 中,没有任何可移植的方式判断指针 T* pt 指向的内存位置是否已经 构造了对象,程序员必须手动跟踪哪些元素已构造。
java·开发语言·c++
один but you2 小时前
const和constexpr常量表达式
java·前端·javascript