搭建Mybatis框架

本文目录

  • 搭建mybatis框架
  • Mybatis 的执行流程

搭建mybatis框架

第一步:引入框架相关jar
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<!--mybatis 核心包-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.3.0</version>
  <scope>test</scope>
</dependency>
<!-- 数据库驱动 -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.18</version>
</dependency>
<!-- log4j日志打印 -->
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.16</version>
</dependency>
第二步:添加框架的配置文件 mybatis-config.xml
  1. 修改配置文件中的数据库链接信息

  2. mapper文件的路径

第三步:创建pojo实体类
第四步:创建映射文件 ***Mapper.xml
  1. namespces mapper 的命名空间,必须保证全局唯一

    1. select 标签中存在id,保证在本mapper中唯一
    2. parameterType 输入参数的类型
    3. resultType 返回值的结果类型
第五步:测试
  1. 加载框架的配置文件 io流的模式

    1. 初始化 sql 工厂
    2. 使用sql 工厂创建sqlSession
    3. 使用sqlSession 操作数据库

Mybatis的执行流程

mybatis的执行流程图


   转载规则


《搭建Mybatis框架》 TyZhai 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录