Mybatis frame 1

Learning Paths
Mybatis: 3D
Day1: the mybatis entry (complete database CRUD)
Day2: Dynamic SQL, multi-table operation
Day3: configuration, programming notes
the Spring: 4D
the Spring MVC: 3D
Maven High: 1d
freight forwarding platform: 15d

  1. Three-layer structure
    Web layer: receiving a request, responds
    Mvc (Spring MVC)
    -Service layer: and relevant code requirements (service)
    Spring
    Dao layers: the database only the CRUD
    MyBatis
    Here Insert Picture Description
    techniques 1.1 Dao layer
    Here Insert Picture Description

Thought 1.2 ORM
ORM thought: object relational mapping (object-relational mapping)
* database entity class and mapping relationship
mapping relationship * entity class attribute field in a table, and

  • Entity classes is equivalent to the operation of the database tables

Hibernate, Jpa: full realization of the ORM thought
Mybatis: partial implementation (sql statement)

  1. Mybatis overview
    Mybatis is an open source apache provided, lightweight persistence framework. Focus on the preparation of sql statement.

Here Insert Picture Description
Here Insert Picture Description
3. Mybatis Getting Case
3.1 unified development environment
Jdk: 1.8, maven repository
Here Insert Picture Description
3.2 database preparation
Here Insert Picture Description
3.3 Create Project import coordinates

<dependencies>
    <!--mybatis坐标-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.0</version>
    </dependency>
    <!--mysql驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <!--日志log4j-->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <!--junit单元测试-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

3.4 创建mybatis的核心配置文件
位置:工程的resource根部目下
命名:sqlMapConfig.xml

Here Insert Picture Description

3.5 创建sql映射文件
3.5.1 用户实体类
Here Insert Picture Description
3.5.2 Sql映射文件
位置:放到resource目录下
命名:实体类名Mapper.xml
Here Insert Picture Description
3.6 测试
Here Insert Picture Description
4. Mybatis的核心API对象
4.1 Resources
加载mybatis的核心配置文件
工具类:Resources.getResourceAsStream(“sqlMapConfig.xml”);
返回:文件输入流

4.2 SqlSessionFactoryBuilder
加载配置文件,根据配置文件创建SqlSessionFactory
方法:SqlSessionFactory.build(is)
返回:SqlSessionFactory工厂
4.3 SqlSessionFactory
工厂模式:创建SqlSession
特点:线程安全的对象,一个工程只创建一个SqlSessionFactory不存在线程安全问题
多个线程访问同一个SqlSessionFactory : 单例对象
4.4 SqlSession
作用:用于执行sql语句
方法:insert保存,update 更新,delete 删除,selectOne 查询一个对象,selectList 查询多个
getMapper 创建dao接口的动态代理对象(创建dao接口的实现类)
特点:SqlSession不是一个线程安全的对象
一个请求创建一个SqlSession对象:多例对象
5. 封装工具类
5.1 封装sqlSession工具类
Here Insert Picture Description
5.2 抽取测试基类
Here Insert Picture Description
6. Mybatis中基于dao层应用
6.1 传统的使用方式(了解)
Here Insert Picture Description
6.1.1 配置dao接口
Here Insert Picture Description
6.1.2 配置dao接口的实现类
Here Insert Picture Description
6.1.3 配置sql映射文件
Here Insert Picture Description
6.1.4 测试
Here Insert Picture Description
6.2 基于dao接口(动态代理)的使用方式(重点)
Here Insert Picture Description
Here Insert Picture Description
6.2.1 约定
Namespace = dao接口的权限定类名
Sql的Id = 执行的方法名称
6.2.2 配置dao接口
Here Insert Picture Description
6.2.3 配置sql映射文件
Here Insert Picture Description
6.2.4 测试
Session.getMapper(接口的字节码对象) : 创建dao接口的实现类(动态代理)
Here Insert Picture Description
7. Mybatis的增删改
使用基于动态代理的方式编写dao
7.1 操作步骤

1.创建工程导入坐标
2.创建mybatis的核心配置文件
3.创建sql映射文件
4.编写dao接口和接口中的方法
5.编写sql语句

7.2 保存用户
7.2.1 接口
Here Insert Picture Description
7.2.2 Sql映射文件
Here Insert Picture Description
7.2.3 测试
Here Insert Picture Description
7.3 更新用户
根据id更新用户
7.3.1 接口
Here Insert Picture Description
7.3.2 Sql映射文件
Here Insert Picture Description

7.3.3 测试
Here Insert Picture Description
7.4 删除用户

7.4.1 Interface
Here Insert Picture Description
7.4.2 Sql mapping file
Here Insert Picture Description
7.4.3 Test
Here Insert Picture Description
8. query
8.1 for all user queries
8.1.1 Interface
Here Insert Picture Description
8.1.2 Mapping File
Here Insert Picture Description
8.1.3 Test
Here Insert Picture Description
8.1.4 Description
the resultType: Return Value Type Configuration
Conditions: When a query entity class field and object attributes consistent
syntax: object permissions for a given class name
resultMap: mapping an attribute fields and
conditions of use: when the query field and object properties are inconsistent
Here Insert Picture Description
8.2 user queries based on id
8.2.1 Interface
Here Insert Picture Description
8.2.2 mapping file
Here Insert Picture Description
8.2. 3 testing
Here Insert Picture Description
8.3 Fuzzy query
8.3.1 Interface
Here Insert Picture Description
8.3.2 mapping file
Here Insert Picture Description
8.3.3 test
Here Insert Picture Description
9. interview summary

Mybatis的核心API对象:
Resource,sqlSessionFactoryBuilder,sqlSessionfactory,sqlSession
#{}和${}的区别
	#{}:占位符
		基本数据类型:#{随便写}
		preparStatment处理:预编译
		没有SQL注入风险
${}:字符串拼接
	基本数据类型:${value}
		Statement处理,没有鱼变
		有SQL注入风险

Guess you like

Origin blog.csdn.net/AdamCafe/article/details/90600179