SpringBoot配置Mybatis的两种方式(通过XML配置和通过YML配置文件配置) | 您所在的位置:网站首页 › configLocation配置路径覆盖原来文件 › SpringBoot配置Mybatis的两种方式(通过XML配置和通过YML配置文件配置) |
转自:https://blog.csdn.net/weixin_43966635/article/details/112342116
一、 通过XML的方式配置Mybatis在 /src/main/resource下创建Mybatis配置文件 mybatis-config.xml 和 映射文件目录mapper在application.yml指定Mybatis配置文件、映射文件的位置可选:- mybatis.config-location:配置 mybatis-config.xml 路径mybatis-config.xml 中配置 MyBatis 基础属性- mybatis.mapper-locations:配置 Mapper 对应的 XML 文件路径- mybatis.type-aliases-package:配置项目中实体类包路径
application.yml
mybatis: #标注mybatis配置文件的位置 config-location: classpath:mybatis-config.xml #标注待解析的mapper的xml文件位置 mapper-locations: classpath:mapper/*.xml #标注实体类位置 type-aliases-package: com.gzu.photo.entity
mybatis-config.xml
二、在appllication.yml中直接配置Mybatis可配置的属性也可以通过查看源码org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration了解所有可以配置的属性(可参考我的另一篇博客SpringBoot自动配置原理)源码中部分配置属性截图
mybatis: configuration: #开启mybatis驼峰式命名规则自动转换 map-underscore-to-camel-case: true ....
注意两种配置方式只能二选一,不能同时使用application.yml中的configuration和mybatis-config.xml文件配置mybabis 即: application.yml中configuration 和 configLocation两个属性不能同时存在,否则会报错:
|
CopyRight 2018-2019 实验室设备网 版权所有 |