Mybatis(mybatis-config.xml解析)+流程分析

1.环境配置

Mybatis 可以配置成适应多种环境。
不过要记住:尽管可以配置多个环境,但每个SqlSessionFactory实例只能选择一种环境。
学会使用配置多套运行环境。
Mybatis默认的事务管理器是JDBC,连接池:POOLED。

2. 属性(properties)

可以通过properties属性实现引用配置文件
这些属性都是可外部配置且可动态替换的,既可以在典型的Java属性文件中配置,亦可通过properties元素的子元素来传递。[db.properties]
db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&userUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
username=root

mybatis.xml中文件映入

<!--引入外部配置 注意标签优先级,配置文件和标签都能写-->
    <properties resource="db.properties">
        <property name="password" value="123456"/>
    </properties>

可以直接引入外部文件
可以在其中增加一些属性配置
如果两个文件有同一个字段,优先使用外部配置文件的

3.类型别名(typeAliases)

类型别名是为Java类型设置一个短的名字。
存在的意义仅在于用来减少类完全限定名的冗余

  <!--完全限定类更改别名-->
    <typeAliases>
        <typeAlias type="com.my.pojo.User" alias="User"/>
    </typeAliases>

也可以指定一个包名,Mybatis会在包名下面搜索需要的Java Bean,比如:
扫描实体类的包,它的默认别名就为这个类的类名,首字母小写,大写也可以!

    <typeAliases>
        <package name="com.my.pojo"/>
    </typeAliases>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.my.dao.UserDao">
    <!--id对应Dao/Mapper接口方法名字-->
    <!--resultType 返回结果要写全限定类名-->
<select id="getUserList" resultType="user">
    select * from mybatis.user;
</select>
    <!--parameterType参数类型-->
    <select id="getUserById" resultType="user" parameterType="int">
       select * from mybatis.user where id=#{id};
    </select>
    <insert id="insertUser" parameterType="map">
        insert into  mybatis.user (pwd, name, id)values(#{pwd},#{name},#{id});
    </insert>
    <delete id="deleteUserById" parameterType="int">
        delete from mybatis.user where id=#{id};
    </delete>
    <update id="updateUser" parameterType="user">
    update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
    </update>
    <!--模糊查询-->
    <select id="getLikeUser" resultType="user">
        select * from mybatis.`user` where name like #{value};
    </select>
    <select id="getLikeUser1" resultType="user">
        select *from mybatis.`user` where  name like "%" #{value} "%";
    </select>
</mapper>

在实体类比较少的时候用第一种方式。可以diy取别名
如果实体类十分多,使用第二种方式。不可以,如果非要改,在实体类上添加注解@Alias(" ")

4.设置(settings)

Mybatis中的调整设置
logImpl

5.映射器(mappers)

MapperRegistry:注册绑定我们的Mapper文件:
方式一:

 <mappers>
        <!--所有的路径以/结尾-->
        <mapper resource="com/my/dao/UserMapper.xml"/>
    </mappers>

方式二:

 <mappers>
       <mapper class="com.my.dao.UserMapper"/>
    </mappers>

注意点:
接口和mapper配置文件必须同名
接口和他的mapper配置文件必须在同一个包下

方式三:

    <mappers>
     <package name="com.my.dao"/>
    </mappers>

注意点:
接口和mapper配置文件必须同名
接口和他的mapper配置文件必须在同一个包下
斜体样式

6.生命周期和作用域

在这里插入图片描述

生命周期类是至关重要的,因为错误的使用会出现导致非常严重的并发问题
SqlSessionFactoryBuilder:
 一旦创建了SqlSessionFactory,就不再需要它了
 局部变量
SqlSessionFactory
 可以想象为数据库连接池
 SqlSessionFactory一旦被创建就应该在应用的运行期间一直存在,没有任何理由丢弃它或重新创建另一个实例。
 因此SqlSessionFactory的最佳作用于是应用作用域
 最简单的就是使用单例模式或者静态单例模式
SqlSession
 连接到连接池的一个请求。
 SqlSession的实例不是线程安全的,因此是不能被共享的,所以它的最佳作用域是请求或方法作用域。
 用完之后要赶紧关闭,否则资源被占用
在这里插入图片描述

这里的每一个Mapper就代表一个业务。
在这里插入图片描述
在这里插入图片描述![在这里插入图片描述](https://img-blog.csdnimg.cn/b4481babf99b41968d5215926f6c2a6e.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5LiJ5p626ams6L2m,size_14,color_FFFFFF,t_70,g_se,x_16
Mybatis流程步骤:
1.Resources获取加载全局配置文件
2.实例化SqlSessionFactoryBuilder构造器
3.解析配置文件流XMLConfigBuilder
4.Configuration所有配置信息
5.SqlSessionFactory实例化
6.transactionanl事务管理
7.创建executor执行器
8.创建SqlSession
9.实现CRUD
x1.查看事务是否成功
x2.提交事务
x3.关闭

ps狂神

猜你喜欢

转载自blog.csdn.net/fhuqw/article/details/121212083
今日推荐