本文目录
- 搭建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
修改配置文件中的数据库链接信息
mapper文件的路径
第三步:创建pojo实体类
第四步:创建映射文件 ***Mapper.xml
namespces mapper 的命名空间,必须保证全局唯一
- select 标签中存在id,保证在本mapper中唯一
- parameterType 输入参数的类型
- resultType 返回值的结果类型
第五步:测试
加载框架的配置文件 io流的模式
- 初始化 sql 工厂
- 使用sql 工厂创建sqlSession
- 使用sqlSession 操作数据库