整合mybatis

springboot 整合 mybatis

Mybatis github地址 htttps://github.com/mybatis

springboot 官方starter:mybatis-spring-boot-starter

<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>

1.配置模式

  • 全局配置文件

  • SqlSessionFactory:自动配置好了

  • SqlSession:自动配置了 SqlSessionTemplate 组合了 SqlSession

  • @Import(AutoConfiguredMapperScannerRegistrar.class)

  • Mapper:只要我们写的操作 Mybatis 的接口标注了 @Mapper 注解,就会被自动扫描进来。

    @EnableConfigurationProperties(MybatisProperties.class)
    @AutoConfigureAfter({DataSourceAutoConfiguration.class,MybatisLanguageDriverAutoConfiguration.class})
    public class MybatisAutoConfiguration{}
    
    @ConfigurationProperties(prefix = "mybatis")
    public class MybatisProperties{}
    

yaml 配置相关

# 配置 mybatis 的规则
mybatis:
	# config-location: classpath:mybatis/mybatis-config.xml		# 全局配置文件的位置
	mapper-locations: classpath:mybatis/mapper/*.xml	# mapper接口映射文件的位置
	configuration: 	# 他和全局配置文件只能存在一个
		map-underscore-to-camel-case: true
		
可以不写全局配置文件,所有权据配置文件的配置项都放在 configuration配置项中即可
<!-- 全局配置 -->
<configuration>
    <settings>
        <!-- 开启驼峰命名规则 -->
    	<setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

配置 private Configuration configuration; mybatis.configuration 下的所有,就是相当于全局配置文件的值。

步骤

  1. 导入mybatis官方starter
  2. 编写mapper接口,标注@Mapper接口
  3. 编写sql映射文件并绑定mapper接口
  4. 在 application.yaml 中编写 Mapper 配置文件的位置,以及指定全局配置文
  5. 件的信息(建议配置在 mybatis.configuration)

2. 注解模式

了解

用注解的方式编写sql查询

3.混合模式

注解+映射文件

可以在启动类上标注 @MapperScan(“com.test.mapper”) 来扫描包下的所有mapper,不建议。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值