当前位置: 首页 > news >正文

SpringBoot-数据访问之MyBatis与Redis

MyBatis导入

  • GitHub:https://github.com/mybatis
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version>
</dependency>

MyBatis的自动配置

1. 绑定配置文件 prefix = "mybatis"

2. SqlSessionFactory

3. SqlSession

  • 自动配置了 SqlSessionTemplate 组合了SqlSession

4. AutoConfiguredMapperScannerRegistrar

  • @Import(AutoConfiguredMapperScannerRegistrar.class)
  • Mapper: 只要操作MyBatis的接口使用了 @Mapper 就会被自动扫描进来

MyBatis配置文件

  • config-location的所有全局配置文件 和 configuration配置 只可选其一
# 配置mybatis规则
mybatis:# 配置mybatis的config.xml路径
#  config-location: classpath:mybatis/mybatis-config.xml# 配置mybatis的mapper.xml路径mapper-locations: classpath:mybatis/mapper/*.xml# 使用配置文件代替config.xmlconfiguration:map-underscore-to-camel-case: true    #开启驼峰命名法

注解模式

1. 只需要写mapper接口,不要写mapper.xml,使用注解写SQL语句

  • 省略@Mapper注解,启动类上标注@MapperScan("mapper包路径") 
@Mapper
public interface CityMapper {@Select("select * from city where id=#{id}")public City getById(Long id);public void insert(City city);}

2. 自增主键例子

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

MyBatis-Plus自动配置

1. 导入依赖

  • 官网地址:https://baomidou.com
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version>
</dependency>

2. MybatisPlusAutoConfiguration 自动配置类

  • 配置文件绑定:mybatis-plus: xxx

3. SqlSessionFactory 自动配置好。底层是容器中默认的数据源

4. mapperLocations 自动配置好的

  • 默认值:classpath*:/mapper/**/*.xml
  • 类路径的mapper文件夹下,任意路径的所有xml都是sql映射文件
  • 建议以后sql映射文件,放在此路径下

5. SqlSessionTemplate 自动配置好了

6. @Mapper 标注的接口也会被自动扫描

  • 建议直接 @MapperScan("mapper包路径") 批量扫描就行

Redis自动配置

1. 导入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2. RedisAutoConfiguration 自动配置类

  • 配置文件配置 spring.redis.xxx
  • 默认连接工厂是LettuceConnectionConfiguration,也可以切换JedisConnectionConfiguration

3. template操作redis

  • 自动注入了RedisTemplate<Object, Object>:k:v都是Object
  • 自动注入了StringRedisTemplate:k:v都是String

4. 测试连接

  • 配置文件
spring:redis:host: 主机名port: 6379password: user:password
  • 测试代码
@Test
void testRedis(){ValueOperations<String, String> operations = redisTemplate.opsForValue();operations.set("hello","world");String hello = operations.get("hello");System.out.println(hello);
}

5. 切换至jedis连接

  • 导入jedis
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>
</dependency>
  • 配置文件 client-type
spring:redis:host: port: 6379password: client-type: jedis
http://www.dtcms.com/a/519346.html

相关文章:

  • iOS 26 App 运行状况全面解析 多工具协同监控与调试实战指南
  • uts ios插件开发tips
  • 单页营销型网站全国城建中心官方网站
  • 了解sip和rtp是什么
  • MySQL-3-函数应用及多表查询
  • 自然语言处理分享系列-词语和短语的分布式表示及其组合性(二)
  • 网站建设珠海 新盈科技泉州建站模板
  • ISO 8601日期时间标准及其在JavaScript、SQLite与MySQL中的应用解析
  • 利用博客做网站排名南京外贸网站建设案例
  • 使用 VS Code 的 Dev Containers 插件,通过跳板机间接连接docker
  • 衡水城乡建设局网站首页北京网站建设华大
  • 湛江网站建设哪家优惠多常见的网站结构有哪些
  • php网站源码架构seo标题生成器
  • ui设计师与网站编辑有什么关系重庆森林为什么不能看
  • 站长工具成品源码广西贵港建设集团有限公司网站
  • 电子商务网站建设技术解决方案wordpress ios
  • 企业是如何做电子商务网站软件开发培训机构去学
  • 摄影网站策划书wordpress 赞 分享
  • 深圳我的网站深圳市工程交易服务网宝安
  • php制作wap网站开发我国酒店网站建设存在的问题
  • 金坛网站建设企业手机网站建设渠道
  • 做网站要不要钱东莞网站快速优化排名
  • 做微信营销网站建设dw个人简历网页制作
  • 建个免费的销售网站好asp net4.0网站开发
  • 给个能看的网站中国500强企业名称
  • 网站一般用什么免费字体云主机wordpress
  • 旅游网站流程图电子商务网站开发的视频
  • 网站集约化建设流程专业网站建设价位
  • 食品网站建设客户需求调查表网站硬件费用
  • 深圳做网站600百度官网首页网址