Mybatis入门学习03--配置⽂件

目录

Mybatis映射配置文件

Mybatis核心配置文件

Mybatis 核心配置文件内容

Mybatis 常用配置解析

properties

environments

typeAliases

mappers


Mybatis映射配置文件

1). namespce 属性表示命名空间 , 起到业务隔离作⽤ , SQL 语句的 ID 组合起来成为 SQL 的唯⼀标识 ,也称为 statementid
2). <select> 标签⽤于声明 select 查询语句 ; 当然也有对应的 <insert> , <delete> , <update> 标签分别⽤来表示增删改的操作 。
3). resultType 属性表示结果集映射中的单条记录所需要映射的类型 。
4). parameterType 属性指 SQL语句需要传⼊的参数类型, 可以是简单数据类型(基本数据类型,基本数据类型包装类, String ),也可以是 POJO ,或者是 Map。

Mybatis核心配置文件

Mybatis 核心配置文件内容

configuration 
     |- properties(属性)
     |- settings(全局配置参数)
     |- typeAliases(类型别名)
     |- typeHandlers(类型处理器)
     |- objectFactory(对象⼯⼚)
     |- plugins(插件)
     |- environments(环境集合属性对象)
         |- environment(环境⼦属性对象)
             |- transactionManager(事务管理)
             |- dataSource(数据源)
     |- mappers(映射器)

Mybatis 常用配置解析

properties

classpath 下定义 db.properties ⽂件,
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wensong?useUnicode=true&characterEncoding=UTF-8&useSSL=false&use&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root

SqlMapConfig.xml引⽤如下:

<configuration>
     <!--加载jdbc.properties 配置⽂件-->
     <properties resource="jdbc.properties"/>

     <!--配置数据库运⾏环境-->
     <environments default="development">
         <!--⽣产环境-->
         <environment id="development">
             <transactionManager type="JDBC"/>
             <dataSource type="POOLED">
                 <property name="driver" value="${jdbc.driver}"/>
                 <property name="url" value="${jdbc.url}"/>
                 <property name="username" value="${jdbc.username}"/>
                 <property name="password" value="${jdbc.password}"/>
             </dataSource>
         </environment>
     </environments>
     <!--配置关联的SQL语句的映射⽂件-->
     <mappers>
         <mapper resource="UserMapper.xml"/>
     </mappers>
</configuration>

environments

数据库环境的配置,⽀持多环境配置

typeAliases

该标签⽤于设置别名 , 设置了别名之后 , 就可以简化映射配置⽂件中 resultType , parameterType的类型配置 ;
1 mybatis 默认别名
下⾯都是 MyBatis 默认⽀持的别名。我们也可以从源码⻆度来看它们分别都是如何定义出来的。
可以参考 TypeAliasRegistery.class 的源码。
序号Alias
Mapped Type
1
_byte
byte
2
_long
long
3
_short
short
4
_int
int
5
_integer
integer
6
_double
double
7
_float
float
8
_boolean
boolean
9
string
String
10
byte
Byte
11
long
Long
12
short
Short
13
int
Integer
14
integer
Integer
15
double
Double
16
float
Float
17
boolean
Boolean
18
date
Date
19
decimal
BigDecimal
20
bigdecimal
BigDecimal
21
object
Object
22
map
Map
23
hashmap
HashMap
24
list
List
25
arraylist
ArrayList
26
collection
Collection
27
iterator
Iterator
2 ⾃定义别名
SqlMapConfig.xml 中配置:
<!--类型别名-->
<typeAliases>
     <!--为单⼀的Pojo设置别名-->
     <!--<typeAlias type="com.wensong.pojo.User" alias="user"></typeAlias>-->
     <!-- 批量别名定义,扫描整个包下的类,别名为类名(⾸字⺟⼤写或⼩写都可以) -->
     <package name="com.wensong.pojo"/>
</typeAliases>
3 简化映射配置⽂件配置
<!--根据⽤户id, 查询⽤户-->
<select id="findById" parameterType="integer" resultType="user">
     select * from user where id=#{id}
</select>
<!--查询所有⽤户-->
<!--
     id: 当前statment的唯⼀标示
     resultType: 把sql语句执⾏结果的每条数据, 封装成指定的java类型
-->
<select id="findAll" resultType="User">
     select * from user
</select>
<!--根据⽤户id 更新⽤户-->
<update id="update" parameterType="user">
     update user
     set username=#{username}, sex=#{sex}, address=#{address}
     where id=#{id}
</update>

mappers

作⽤ : 加载映射配置⽂件
Mapper 配置的⼏种⽅法:
A. <mapper resource="" />
     使⽤相对于类路径的资源
     如:<mapper resource="UserMapper.xml" />
B. <mapper class=" " />
     使⽤mapper接⼝类路径
     如:<mapper class="com.wensong.mapper.UserMapper"/>
 
     注意:此种⽅法要求mapper接⼝名称和mapper映射⽂件名称相同,且放在同⼀个⽬录中。
C. <package name=""/>
     注册指定包下的所有mapper接⼝
     如:<package name="com.wensong.mapper"/>
 
     注意:此种⽅法要求mapper接⼝名称和mapper映射⽂件名称相同,且放在同⼀个⽬录中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值