5.配置解析

1.核心配置文件:mybatis-config.xml

MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。 配置文档的顶层结构如下:
configuration(配置)
properties(属性)
settings(设置)
typeAliases(类型别名)
typeHandlers(类型处理器)
objectFactory(对象工厂)
plugins(插件)
environments(环境配置)
environment(环境变量)
transactionManager(事务管理器)
dataSource(数据源)
databaseIdProvider(数据库厂商标识)
mappers(映射器)
不过要记住:尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境。

配置文件的基本样例:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"></transactionManager>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://192.168.2.129:3306/mybatis"/>
                    <property name="username" value="root"/>
                    <property name="password" value="521521"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="cn\com\wmd\dao\PersonMapper.xml"/>
        </mappers>
    </configuration>

2.属性(properties)的用法

按照上述配置,数据库的连接信息是直接写死在ybatis的核心配置文件中mybatis-config.xml中的
也可以将其抽取出来放在一个properties配置文件中

例如在resource文件夹下创建一个db.properties文件专门放数据库的连接信息等
    1.db.properties文件中的配置
        driver=com.mysql.cj.jdbc.Driver
        url=jdbc:mysql://192.168.2.129:3306/mybatis
        username=root
        password=521521
    2.mybatis-config.xml(mybatis的核心配置文件)
        <configuration>
            //重点1:引入外部的配置文件,并且properties 标签只能放在最前面,放在后面会报错
            <properties resource="db.properties"/>
            <environments default="development">
                <environment id="development">
                    <transactionManager type="JDBC"></transactionManager>
                    <dataSource type="POOLED">
                        //重点2:使用${key}的形式引入外部配置文件中的配置
                        <property name="driver" value="${driver}"/>
                        <property name="url" value="${url}"/>
                        <property name="username" value="${username}"/>
                        <property name="password" value="${password}"/>
                    </dataSource>
                </environment>
            </environments>
            <mappers>
                <mapper resource="cn\com\wmd\dao\PersonMapper.xml"/>
            </mappers>
        </configuration>
        
2.第二种情况:
     1.db.properties文件中的配置
        driver=com.mysql.cj.jdbc.Driver
        url=jdbc:mysql://192.168.2.129:3306/mybatis
    2.mybatis-config.xml(mybatis的核心配置文件)
        <configuration>
            <properties resource="db.properties">
                //重点1:在引入外部配置文件的properties 标签中继续设置属性
                <property name="username" value="root"/>
                <property name="password" value="521521"/>
            </properties>
            <environments default="development">
                <environment id="development">
                    <transactionManager type="JDBC"></transactionManager>
                    <dataSource type="POOLED">
                        <property name="driver" value="${driver}"/>
                        <property name="url" value="${url}"/>
                        //重点2:这样也是可以生效的
                        <property name="username" value="${username}"/>
                        <property name="password" value="${password}"/>
                    </dataSource>
                </environment>
            </environments>
            <mappers>
                <mapper resource="cn\com\wmd\dao\PersonMapper.xml"/>
            </mappers>
        </configuration>

3.2情况引申出的问题:
    如果外部配置文件中:db.properties和properties标签下的property均设置了相同的属性username,会以哪一个优先呢
    答案是:以外部配置文件中的优先

3.类型别名(typeAliases)

类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。例如:
typeAliases的使用

问题:在mapper.xml中如果引入对象时,需要写对象的全类名如下图:
    <mapper namespace="cn.com.wmd.dao.PersonMapper">
        //重点:结果返回类型是一个实体类,写的是类的全类名(比较麻烦)
        <select id="getPersonList" resultType="cn.com.wmd.pojo.Person">
            select * from person
        </select>
    </mapper>
发现写的全类名比较麻烦,如何能简化呢?

1.第一种方式别名(<typeAlias type="" alias=""/>一个具体的类一个别名)
    1.1在mybatis的核心配置文件中:mybatis-config.xml中配置别名:
        <configuration>
            <properties resource="db.properties">
                <property name="username" value="root"/>
                <property name="password" value="521521"/>
            </properties>
            //重点1:typeAliases有位置要求,放在第二位置上,放其他位置会报错
            <typeAliases>
                //第一种方式的别名:是以一个具体的类起一个别名
                <typeAlias type="cn.com.wmd.pojo.Person" alias="Person"/>
            </typeAliases>
            ...
        </configuration>
     1.2在mapper.xml中的用法:
         <mapper namespace="cn.com.wmd.dao.PersonMapper">
             //重点1:返回值类型使用的是别名:Person
            <select id="getPersonList" resultType="Person">
                select * from person
            </select>
        </mapper>

2.指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean
    (<typeAliases>
      <package name="包名"/>
    </typeAliases>)
    1.mybatis的核心配置文件中(mybatis-config.xml)
        <configuration>
            <properties resource="db.properties">
                <property name="username" value="root"/>
                <property name="password" value="521521"/>
            </properties>
            //重点1:使用包的形式(在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名)
            <typeAliases>
                <package name="cn.com.wmd.pojo"/>
            </typeAliases>
                ...
          </configuration>  
     2.mapper.xml中的用法
         <mapper namespace="cn.com.wmd.dao.PersonMapper">
             //重点1:使用的别名是实体类名小写
            <select id="getPersonList" resultType="person">
                select * from person
            </select>
        </mapper> 
        
3.实体类上使用标签自定义别名@Alias(必须在指定包的形式下才生效)
    1.mybatis核心配置文件
        <configuration>
            <properties resource="db.properties">
                <property name="username" value="root"/>
                <property name="password" value="521521"/>
            </properties>
            //重点1:配置包的形式,没有标签,别名就是类名小写
            <typeAliases>
                <package name="cn.com.wmd.pojo"/>
            </typeAliases>
                ...
          </configuration> 
    2.实体类写法
        //重点2:在此模式下使用@Alias标签自定义别名
        @Alias("wmd")
        public class Person {
        }
    3.mapper.xml中的写法
        <mapper namespace="cn.com.wmd.dao.PersonMapper">
            //重点1:使用的是自定义标签@Alias("wmd")的名称
            <select id="getPersonList" resultType="wmd">
                select * from person
            </select>
        </mapper>
别名
映射的类型
_byte
byte
_long
long
_short
short
_int
int
_integer
int
_double
double
_float
float
_boolean
boolean
string
String
byte
Byte
long
Long
short
Short
int
Integer
integer
Integer
double
Double
float
Float
boolean
Boolean
date
Date
decimal
BigDecimal
bigdecimal
BigDecimal
object
Object
map
Map
hashmap
HashMap
list
List
arraylist
ArrayList
collection
Collection
iterator
Iterator
结论发现:
  1.想使用基本类型时:别名为:_类型

4.映射器(mappers)

在mybatis的核心配置文件中引入mapper文件有以下三种方式
1.使用resource直接引入
    <mappers>
        //引入的是绑定接口的mapper.xml文件
        <mapper resource="cn\com\wmd\dao\PersonMapper.xml"/>
    </mappers>
    
2.使用class文件绑定注册
    <mappers>
        //重点1:使用class属性绑定对应的接口类
        <mapper class="cn.com.wmd.dao.PersonMapper"/>
    </mappers>
    但是这种方式有以下注意:
        1.接口名称必须和mapper配置文件同名
        2.接口和他的mapper配置文件必须在同一个包下
        
3.使用扫描包的形式注入绑定
        <mappers>
            重点1:使用package标签
            <package name="cn.com.wmd.dao"/>
        </mappers>
        但是这种方式有以下注意(和class文件绑定注册注意点一致):
            1.接口名称必须和mapper配置文件同名
            2.接口和他的mapper配置文件必须在同一个包下

posted @ 2022-05-13 19:43  努力的达子  阅读(41)  评论(0编辑  收藏  举报