当前位置:网站首页>The XML configuration
The XML configuration
2022-08-01 18:20:00 【Cool Xiaoya】
文章目录
XML配置解析
1、核心配置文件
MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息 .
configuration(配置)
properties(属性)
settings(设置)
typeAliases(类型别名)
typeHandlers(类型处理器)
objectFactory(对象工厂)
plugins(插件)
environments(环境配置)
environment(环境变量)
transactionManager(事务管理器)
dataSource(数据源)
databaseIdProvider(数据库厂商标识)
mappers(映射器)
2、环境配置(environments)
MyBatis可以配置成适应多种环境
不过要记住:尽管可以配置多个环境,但每个SqlSessionFactoryInstance can only choose one situation.
(学会使用配置多套运行环境)
Mybatis默认的
- 事务管理:JDBC ,
- 连接池 :POOLED
3、属性(properties)
我们可以通过properties属性来实现引用配置文件
这些属性都是可外部配置且可动态替换的,既可以在典型的Java属性文件中配置,也可以通过properties元素的子元素来传递.
编写一个配置文件
db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis01?useSSL=false&useUnicode=true&\
characterEncoding=UTF-8&serverTimezone=UTC
username=root
password=123456
在核心配置文件中映入
<!--引入外部配置文件-->
<properties resource="db.properties">
<property name="username" value="root"/>
<property name="password" value="123456"/>
</properties>
- 可以直接引入外部文件
- 可以在其中增加一些属性配置
- 如果两个文件有同一个字段,优先使用外部配置文件!
4、类型别名(typeAliases)
- 类型别名是为Java类型设置一个短的名字.
- 存在的意义仅在于用来减少类完全限定名的冗余.
<!--可以给实体类起别名-->
<typeAliases>
<typeAlias type="com.jin.pojo.User" alias="User"/>
</typeAliases>
<update id="UserUpdate" parameterType="User">
...
</update>
也可以指定一个包名,MybatisWill be in the package search need belowJava Bean,比如:
扫描实体类的包,He default alias for this class 类名,首小写!
<!--可以给实体类起别名-->
<typeAliases>
<package name="com.jin.pojo"/>
</typeAliases>
<update id="UserUpdate" parameterType="user">
...
</update>
在实体类比较少的时候,使用第一种方式!
在实体类比较多的时候,使用第二种方式.
第一种可以DIY别名,第二种则不行.如果非要改,需要在实体类上增加注解!
5、设置(settings)
6、其他配置
- typeHandlers(类型处理器)
- objectFactory(对象工厂)
- plugins插件
- mybatis-generator-core
- mybatis-plus
- 通用mapper
7、映射器(mappers)
典型报错:MapperRegistry
注册绑定我们的Mapper文件
方式一:【推荐】
<!-- 使用相对于类路径的资源引用 -->
<mappers>
<mapper resource="com/jin/mapper/UserMapper.xml"/>
</mappers>
方式二:
<!-- 使用映射器接口实现类的完全限定类名 -->
<mappers>
<mapper class="com.jin.mapper.UserMapper.xml"/>
</mappers>
注意点:
- 接口和他的Mapper配置文件必须同名!
- 接口和他的Mapper配置文件必须在同一个包下!
方式三:
<!--使用扫描包进行注入绑定-->
<mappers>
<package name="com.jin.mapper"/>
</mappers>
注意点:
- 接口和他的Mapper配置文件必须同名!
- 接口和他的Mapper配置文件必须在同一个包下!
边栏推荐
- Leetcode74. 搜索二维矩阵
- QT basic functions, signals, slots
- 用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
- B002 - Embedded Elderly Positioning Tracking Monitor
- QT基础功能,信号、槽
- Go GORM transaction instance analysis
- 无需破解,官网安装Visual Studio 2013社区版
- How to solve the dynamic binding of el-form-item prop attribute does not take effect
- OpenCV installation, QT, VS configuration project settings
- LeetCode 0152. 乘积最大子数组:dp + 原地滚动
猜你喜欢
Leetcode75. 颜色分类
Leetcode75. Color Classification
将ENS域名转化为音乐需要几步?
【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)
Solve the problem that MySQL cannot insert Chinese data
opencv语法Mat类型总结
B002 - Embedded Elderly Positioning Tracking Monitor
B001 - 基于STM32的智能生态鱼缸
XAML WPF item groupBox control
一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
随机推荐
C语言理论--笔试面试基础稳固
塔防海岸线用户协议
【Day_09 0427】走方格的方案数
QT commonly used global macro definitions
Leetcode73. Matrix Zeroing
亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
OpenCV installation, QT, VS configuration project settings
钳形万用表使用方法,如何测量电压、电流、电阻?
Golang协程调度器scheduler怎么使用
8月微软技术课程,欢迎参与
Detailed explanation of DBPack SQL Tracing function and data encryption function
QT_QDialog 对话框
暑假第一周总结博客
WinRAR | 将多个安装程序生成一个安装程序
C language theory--a solid foundation for the written test and interview
opencv基本的图像处理
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) Solution
B002 - Embedded Elderly Positioning Tracking Monitor
想随时、随地、随心使用数据库的朋友们,全体注意!
opencv如何实现图像倾斜校正