Mybatis学习_03_配置学习

MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。

1、环境配置(environments)

MyBatis 可以配置成适应多种环境,这种机制有助于将 SQL 映射应用于多种数据库之中。不过要记住:尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境。

image-20230302162847979

 <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;usseUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
        <environment id="test">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;usseUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

2、属性(properties)

这些属性可以在外部进行配置,并可以进行动态替换。既可以在典型的 Java 属性文件中配置这些属性,也可以在 properties 元素的子元素中设置。

新建db.properties文件

image-20230302170112828

driver= com.mysql.cj.jdbc.Driver
urll = jdbc:mysql://localhost:3306/mybatis?useSSL=true&usseUnicode=true&characterEncoding=UTF-8
username=root
pwd=123456

引入到核心配置文件中:

<properties resource="db.properties"></properties>

替换:

<environment id="development">
    <transactionManager type="JDBC"/>
    <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${urll}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${pwd}"/>
    </dataSource>
</environment>

注:

    <properties resource="db.properties">
		<property name="username" value="root"/>
        <property name="password" value="123456"/>   <!--这些属性可以放入db.properties文件里-->
    </properties>

可以在properties里加入属性,如果属性property和外部文件db.properties里的冲突则外部的优先,外部文件里的优先级更高。

3、类型别名(typeAliases)

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

方式一:

<typeAliases>
    <typeAlias type="com.zhaolei.pojo.User" alias="user"></typeAlias>
</typeAliases>

未使用别名:

<select id="getUserByID" parameterType="int" resultType="com.zhaolei.pojo.User">
    select * from mybatis.user where id=#{id}
</select>

使用别名;

<select id="getUserByID" parameterType="int" resultType="user">
    select * from mybatis.user where id=#{id}
</select>

方式二:

每一个在包 com.zhaolei.pojo.User 中的 Java User,在没有注解的情况下,会使用 User的首字母小写的非限定类名来作为它的别名。 比如 com.zhaolei.pojo.User 的别名为 user;若有注解,则别名为其注解值。

<typeAliases>
    <package name="com.zhaolei.pojo"/>
</typeAliases>

未使用别名:

<select id="getUserByID" parameterType="int" resultType="com.zhaolei.pojo.User">
    select * from mybatis.user where id=#{id}
</select>

使用别名;

<select id="getUserByID" parameterType="int" resultType="user">
    select * from mybatis.user where id=#{id}
</select>

总结:

  • 当包类较少时适合采用方法一;
  • 类较多时适合采用方法二;
  • 方法一可以diy别名,方法二一般采取默认别名;

image-20230302193738148

image-20230302193752860

对于基本类型,它的别名前一般加个下划线;

对于包装类一般采用首字母小写;

4、映射器(mappers)

MapperRegistry: 注册绑定我们的Mapper文件;

方式一:(首选方案)

<!-- 使用相对于类路径的资源引用 -->
<mappers>
    <mapper resource="com/zhaolei/dao/UserMapper.xml"/>
</mappers>

方式二:

<!-- 使用映射器接口实现类的完全限定类名 -->
<mappers>
    <mapper class="com.zhaolei.dao.UserMapper"/>
</mappers>

注意点:

  1. 接口和他的类的名字必须保持一致!
  2. 接口和它的配置文件必须在同一个包下!
  3. 接口和它的配置文件名称必须一致,如(Mapper和Mapper.xml)!

方式三;

<!-- 将包内的映射器接口全部注册为映射器 -->
<mappers>
    <package name="com.zhaolei.dao"/>
</mappers>

注意点:

  1. 接口和他的类的名字必须保持一致!
  2. 接口和它的配置文件必须在同一个包下!
  3. 接口和它的配置文件名称必须一致,如(Mapper和Mapper.xml)!

5、作用域(Scope)和生命周期

生命周期,作用域是至关重要的,因为错误的使用会导致非常严重的并发问题!!!

SqlSessionFactoryBuilder:

  • 一旦创建了 SqlSessionFactory,就不再需要它了。
  • qlSessionFactoryBuilder 实例的最佳作用域是方法作用域(也就是局部方法变量)!

SqlSessionFactory:

  • SqlSessionFactory 一旦被创建就应该在应用的运行期间一直存在,没有任何理由丢弃它或重新创建另一个实例。
  • SqlSessionFactory 的最佳作用域是应用作用域。
  • 最简单的就是使用单例模式或者静态单例模式.
  • 本质上相当于数据库连接池。

SqlSession:

  • SqlSession 的实例不是线程安全的,因此是不能被共享的.
  • 它的最佳的作用域是请求或方法作用域.
  • 用完之后必须进行关闭,否则会导致资源被占用。
  • 本质上相当于连接连接池的一个请求。

image-20230302202407677

6、结果映射--->数据库字段名与实体类不一致问题

问题: 如果列名和属性名不能匹配上

image-20230302213812740

解决办法:

  1. 方法一:起别名
<select id="getUserByID" parameterType="int" resultType="user">
    select id,name,pwd as password from mybatis.user where id=#{id}
</select>

image-20230302214124306

方法二:resultMap

<resultMap id="usermap" type="user">
    <result column="pwd" property="password"/>
</resultMap>
<select id="getUserByID" parameterType="int" resultMap="usermap">
    select id,name,pwd from mybatis.user where id=#{id}
</select>

image-20230302214606422

  • resultMap 元素是 MyBatis 中最重要最强大的元素。
  • ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了.
posted @ 2023-03-02 22:06  ZLey  阅读(8)  评论(0编辑  收藏  举报