1

Mybatis核心配置文件中的标签介绍 - 就是搞笑啊

 1 year ago
source link: https://www.cnblogs.com/tianaqiang/p/16600508.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Mybatis核心配置文件中的标签介绍

0. 标签顺序

Mybatis核心配置文件中有很多标签,它们谁谁写在前写在后其实是💥有顺序💥要求的:

从前到后:

properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?

像properties要在typeAliases之前,typeAliases要在mappers之前

1. typeAliases和typeAlias

<typeAliases>
	<typeAlias type="com.atguigu.mybatis.pojo.User" alias="abc"></typeAlias>
</typeAliases>

image-20220818215516622

typeAlias:设置某个类型的别名

  • type:设置需要的实体类型的别名

  • alias:设置某个类型的别名,若不设置该属性,💥默认别名为其类名且不区分大小写

    <typeAlias type="com.atguigu.mybatis.pojo.User></typeAlias>
    

    例如这个不写alias,默认别名为User,不区分大小写

2. package

当实体类过多是,一般不采用一个个写typeAlias别名的做法,而是通过包设置别名。

<typeAliases>
	<package name="com.atguigu.mybatis.pojo"/>
</typeAliases>

package:以包为单位,将包下💦所有的类型设置默认的类型别名,即类名且不区分大小写

3. mappers和mapper

<mappers>
	<mapper resource="mappers/UserMapper.xml"/>
</mappers>

mapper:引入mybatis的映射文件

  • ❄️🌀ke问题:当映射文件过多时,会出现与typeAlias一样的问题。

  • ☀️解决方法:使用package标签以包为单位引入映射文件,和typeAlias类似 。

    <mappers>
    	<package name="com.atguigu.mybatis.mapper"/>
    </mappers>
    
  • 🐔🐬package标签的使用有两个要求:

    • mapper接口所在的包要和映射文件所在的包一致

    • mapper接口要和映射文件的名字一致

    image-20220818222650873
  • 注意上面映射文件这里com.atguigu.mybatis.mapper是一个包,打开本地目录后是这样的,而不是一个文件夹名

    image-20220818223246598

    怎么创建这样一个包呢??

    1. image-20220818223620325
    2. image-20220818223817574
  • 为什么mapper接口文件和映射文件要设置为😠同一包目录??

    image-20220818224840984

    因为这样最终加载完接口文件和映射文件是在同一目录下放在一起的。

部分内容来自🌹🌹 B站尚硅谷课程 🌹。老师讲得很好,有兴趣的可以听听。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK