当前位置:网站首页>XML配置
XML配置
2022-08-01 18:18:00 【酷小亚】
文章目录
XML配置解析
1、核心配置文件
MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息 。
configuration(配置)
properties(属性)
settings(设置)
typeAliases(类型别名)
typeHandlers(类型处理器)
objectFactory(对象工厂)
plugins(插件)
environments(环境配置)
environment(环境变量)
transactionManager(事务管理器)
dataSource(数据源)
databaseIdProvider(数据库厂商标识)
mappers(映射器)
2、环境配置(environments)
MyBatis可以配置成适应多种环境
不过要记住:尽管可以配置多个环境,但每个SqlSessionFactory实例只能选择一种情况。
(学会使用配置多套运行环境)
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>
也可以指定一个包名,Mybatis会在包下面搜索需要的Java Bean,比如:
扫描实体类的包,他的默认别名就为这个类的 类名,首小写!
<!--可以给实体类起别名-->
<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配置文件必须在同一个包下!
边栏推荐
- Solve the problem that MySQL cannot insert Chinese data
- 解决MySQL插入不了中文数据问题
- Clip-on multimeter use method, how to measure the voltage, current, resistance?
- 公用函数----mfc
- EpiSci | Deep Reinforcement Learning for SoCs: Myth and Reality
- AntDB database appeared in the 24th high-speed exhibition, helping smart high-speed innovative applications
- 三维空间中点的插值
- MySQL 45 Talk | 09 How to choose common index and unique index?
- SQL函数 TO_DATE(二)
- QT_事件类
猜你喜欢
电商库存系统的防超卖和高并发扣减方案
XAML WPF item groupBox control
将ENS域名转化为音乐需要几步?
explain 各字段介绍
Topology零部件拆解3D可视化解决方案
QT基础功能,信号、槽
B005 - STC8 based single chip microcomputer intelligent street light control system
Solve the problem that MySQL cannot insert Chinese data
【Day_09 0427】走方格的方案数
亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
随机推荐
University of California | Inverse Reinforcement Learning from Different Third-Person Videos via Graph Abstraction
Live chat system technology (8) : vivo live IM message module architecture practice in the system
ACID Characteristics and Implementation Methods of MySQL Relational Database Transactions
How to make the fixed-point monitoring equipment display the geographic location on the EasyCVR platform GIS electronic map?
【无标题】setInterval和setTimeout详解
QT常用全局宏定义
EpiSci|片上系统的深度强化学习:神话与现实
Golang协程调度器scheduler怎么使用
2022年SQL大厂高频实战面试题(详细解析)
SQL的索引详细介绍
QT_事件类
QT基础功能,信号、槽
The function realization of the national standard GB28181 protocol EasyGBS platform compatible with the old version of the inflow port
Leetcode72. Edit Distance
成都理工大学&电子科技大学|用于强化学习的域自适应状态表示对齐
Leetcode74. Search 2D Matrix
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
How to solve the dynamic binding of el-form-item prop attribute does not take effect
tooltip control
md5sum源码 可多平台编译