当前位置:网站首页>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配置文件必须在同一个包下!
边栏推荐
- 【Day_12 0507】二进制插入
- Three solutions: npm WARN config global --global, --local are deprecated. Use --location=global instead.
- opencv syntax Mat type summary
- 【Day_08 0426】两种排序方法
- 计算IoU(D2L)
- B005 – 基于STC8的单片机智能路灯控制系统
- 无需破解,官网安装Visual Studio 2013社区版
- 【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)
- QT_Event class
- Detailed explanation of DBPack SQL Tracing function and data encryption function
猜你喜欢
How to use the Golang coroutine scheduler scheduler
打开微信客服
Zabbix6.0 DingTalk robot alarm
Topology Parts Disassembly 3D Visualization Solution
MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
OpenCV installation, QT, VS configuration project settings
QT basic functions, signals, slots
QLineEdit learning and use
Leetcode71. Simplified Paths
483-82(23、239、450、113)
随机推荐
How to solve the dynamic binding of el-form-item prop attribute does not take effect
QT_事件类
SQL函数 TO_DATE(一)
SQL function TO_DATE (1)
Zabbix6.0钉钉机器人告警
行业沙龙第二期丨如何通过供应链数字化业务协同,赋能化工企业降本增效?
Review实战经典:2 种封装风格,你偏爱哪种?
MySQL Lock wait timeout exceeded; try restarting transaction 锁等待
LeetCode 0152. 乘积最大子数组:dp + 原地滚动
【Day_10 0428】密码强度等级
How many steps does it take to convert an ENS domain name into music?
C#/VB.NET:从 PDF 文档中提取所有表格
突破性能天花板!亚信数据库支撑 10 多亿用户,峰值每秒百万交易
阿里云的域名和ip绑定
B002 - Embedded Elderly Positioning Tracking Monitor
QLineEdit learning and use
C#/VB.NET: extracted from the PDF document all form
EpiSci|片上系统的深度强化学习:神话与现实
483-82(23、239、450、113)
C#/VB.NET:从 PDF 文档中提取所有表格