A main frame: Mybatis frame (1) generally, the example environment structures

One: the three-tier framework and summarizes

1. What is the framework?

Let me talk about the framework, the framework is to our tedious code Briefly achieve encapsulation , so minimalist implementation, greatly improving development efficiency. Different frameworks to solve different problems.

2. The three-tier MVC architecture

In javaweb we said in the three-tier architecture, including the presentation layer (display data), business layer (business processing, transaction processing), persistence layer (interact with the database)
Here Insert Picture Description
three-tier architecture used in the framework
3. persistence layer (dao) technology solutions

(1) JDBC technology:

	Connection

	PreparedStatement

	ResultSet

(2) Others, such as the Spring JdbcTemplate:

Spring中对jdbc的简单封装

(3) Apache's DBUtils:

它和Spring的JdbcTemplate很像,也是对Jdbc的简单封装

These specifications are not the frame is the Spring JdbcTemplate JDBC and Apache DBUtils tools are just
Here Insert Picture Description
persistence (DAO layer) Total FIG.

Two: Mybatis Overview

Mybatis a framework persistence layer , written in java. Sealed it installed a lot of details jdbc operations, is that developers can focus sql statement itself, without the need to focus on those registration drive, create complex connections in the process, he uses an ORM idea to achieve the result set of packages.

ORM (Objece Relational Mapping) thought: object-relational mapping name suggests.

Short answer, it is the database and the entity class attributes corresponding to them so that we can work with entity classes to achieve operation of the database.

数据库中是user id password 则实体类中就必须是user id password,我们只需要做到实体类中的属性和数据库表的字段名称保持一致。

三:Mybatis环境搭建

1.准备:

创建Maven工程导入jar坐标和Mybatis的坐标
Here Insert Picture Description
在main中创建实体类和dao的接口(例如User和com.itheima.dao.IUserDao)
Here Insert Picture Description
实体类和dao的接口

在resource资源目录下创建Mybatis整体的主配置文件SqlMapConifg.xml以及映射的配置文件

mybatis的主配置文件
Here Insert Picture Description
创建映射配置文件IUserDao.xml

映射配置文件指的是每个dao独立的配置文件
Here Insert Picture Description

环境搭建的注意事项:

1.创建IUserDao.xml和IUserDao.java时名称是为了和我们之前学习javaweb的知识相同一致。

在Mybatis中它把持久层的操作接口名称和映射文件也叫作Mapper(即mapper=dao)所以:IUserDao 和 IUserMapper是一样的

2.在IDEA创建目录的时候,它和包不一样,包在创建时:com.itheima.dao它是三级结构

目录在创建时:com.itheima.dao是一级目录

3.Mybatis的映射配置文件位置必须和 dao接口的包结构相同,也要是com.itheima.dao.

4.映射配置文件(IUserDao.xml)mapper标签和namespace属性的取值必须是dao接口的全限定类名

5.映射配置文件(IUserDao.xml)对数据库的操作配置(select),id 属性必须是dao接口的方法名(findAll)

因此,我们遵从了以上点时,我们就无需再在开发中书写dao的实现类了(只需要写接口)。
Here Insert Picture Description
2.代码案例分析

一切环境配置准备就绪后,我们开始编写测试工具类,来实现功能。

编写测试类:com.itheima.test.MybatisTest
Here Insert Picture Description
(1)读取主的配置文件(主配置里面包含映射配置)

InputStream in = Resources.getResourceAsStream(“SqlMapConfig.xml”);

2)创建SqlSessionFactory工厂(构建者设计模式):项目给设计者builder,builder替我们项目生成一个工厂。

SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);

(3)创建SqlSession对象(工厂模式):使用工厂来创建SqlSession对象。

SqlSession session = factory.openSession(); //工厂factory来生产session对象

(4)创建Dao接口的代理对象(代理模式):使用session来创建代理对象

IUserDao userDao = session.getMapper(IUserDao.class);

(5)使用代理对象来执行dao的方法

List users = userDao.findAll(); 最后打印users即可

(6) the release of resources: session.close (); in.close () ;
Here Insert Picture Description
Note: Do not forget to inform mybatis to be encapsulated in the map configuration mode to which the entity class configuration: the fully qualified class name of the designated entity class com.itheima .domain.User

Four: Mybatis annotation-based programming

(1) can be IUserDao.xml mapping configuration removed

(2) the use @select comment on the method dao interface , and know that SQL statement,

Here Insert Picture Description
(3) simultaneously at the required time mapper SqlMapConfig.xml master profile configuration, use the class attribute specifies the fully qualified class name dao interface. (Com.itheima.dao.IUserDao)
Here Insert Picture Description
and our actual development, all the more simple the better, so do not write are based on the implementation of dao class . Regardless of annotations or XML configuration. But Mybatis it is written dao support implementation class.

Published 47 original articles · won praise 18 · views 4881

Guess you like

Origin blog.csdn.net/qq_43605085/article/details/96478686