- MyBatis-study notes 01 [01. Mybatis course introduction and environment construction] [day01]
- MyBatis-study notes 02 [02. Mybatis entry case]
- MyBatis-study notes 03 [03. Customize Mybatis framework]
- MyBatis-study notes 04 [04. Custom Mybatis framework based on annotation development]
- MyBatis-study notes 05 [05. Use Mybatis to complete CRUD]
- MyBatis-study notes 06 [06. Use Mybatis to complete the development of the DAO layer]
- MyBatis-study notes 07 [07.Mybatis connection pool and transaction]
- MyBatis-study notes 08 [08. Dynamic SQL]
- MyBatis-study notes 09【09.Multi-table operation of Mybatis】
- MyBatis-study notes 10 [10. JNDI extended knowledge]
- MyBatis-study notes 11 [11. Mybatis cache]
- MyBatis-study notes 12 [12. Mybatis annotation development]
table of Contents
01.mybatis course introduction
02. Correspondence between the three-tier architecture and the ssm framework
03.Analysis of the problem of jdbc operating database
05.mybatis environment construction-preliminary preparation
06.Mybatis environment construction
Write the persistence layer interface IUserDao
Create a new SqlMapConfig.xml file in the resources folder
Summary of steps to build mybatis environment
07. Precautions for environmental construction
01.mybatis course introduction
Mybatis framework
A total of four days. The
first day: Mybatis introduction
Mybatis overview
Mybatis environment to build
mybatis entry case
Custom mybatis framework (the main purpose is to let everyone understand the execution details in
mybatis ) Second day: Mybatis basically uses
the single table of mybatis crud operating
parameters and return values mybatis
dao mybatis written
details mybatis configured
to use several tags
Day: mybatis in-depth and multi-table
mybatis connection pooling
transaction control method and design of mybatis of
multi-table queries mybatis
one pair Many (many-to-one)
many-to-many
Day 4: Mybatis cache and annotation development
Mybatis loading timing (query timing)
Mybatis first level cache and second level cache
Mybatis annotation development
single-table CRUD
multi-table query
02. Correspondence between the three-tier architecture and the ssm framework
1. What is a framework?
It is a set of solutions in our software development, and different frameworks solve different problems.
The benefits of using the framework: the
framework encapsulates a lot of details, so that developers can use a minimalist way to achieve functions. Greatly improve development efficiency.
2. Three-tier architecture
presentation layer:
the
business layer for displaying data : the persistence layer
for processing business requirements
: it
interacts with the database
03.Analysis of the problem of jdbc operating database
3. Persistence layer technology solution
JDBC technology:
Connection
PreparedStatement
ResultSet
Spring's JdbcTemplate:
Simple encapsulation of jdbc in Spring
Apache's DBUtils:
It is very similar to Spring's JdbcTemplate, and it is also a simple encapsulation of JdbcThese are not frameworks.
JDBC is a standard
Spring’s JdbcTemplate and Apache’s DBUtils are just tools.
04.mybatis overview
4. Overview of
mybatis Mybatis is a persistence layer framework, written in java.
It encapsulates many details of jdbc operation, so that developers only need to pay attention to the sql statement itself, without having to pay attention to the complicated process of registering drivers, creating connections and so on.
It uses ORM ideas to achieve the encapsulation of result sets.ORM:
Object Relational Mappging
Simply put: it
is to associate the database table with the entity class and the attribute of the entity class
so that we can operate the database table by operating the entity class.user User
id userId
user_name userName
Today we need to
keep the attributes in the entity class consistent with the field names of the database table.
user data table User entity class
id id
user_name user_name
05.mybatis environment construction-preliminary preparation
Create Maven project
If checked, select ordinary java project
Create database table
Database: eesy_mybatis
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL COMMENT '用户名称',
`birthday` datetime default NULL COMMENT '生日',
`sex` char(1) default NULL COMMENT '性别',
`address` varchar(256) default NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;insert into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (41,'Old King','2018-02-27 17:47:08','Male ','Beijing'),(42,'Little Second King','2018-03-02 15:09:37','Female','Beijing Golden Yanlong'),(43,'Little Second King','2018 -03-04 11:34:34','female','Beijing Jinyanlong'),(45,'Chuanzhi Podcast','2018-03-04 12:04:06','male','Beijing Jinyanlong '),(46,'Lao Wang','2018-03-07 17:37:26','Male','Beijing'),(48,'Little Pony','2018-03-08 11:44 :00','Female','Beijing Correction');
mybatis coordinates
mybatis official website: https://mybatis.org/mybatis-3/zh/index.html
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>day01_easy_01mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging><!--打包方式:jar包-->
<!--导入mybatis坐标-->
<dependencies><!--依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
06.Mybatis environment construction
Write User entity class
package com.itheima.domain;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
Write the persistence layer interface IUserDao
package com.itheima.dao;
import com.itheima.domain.User;
import java.util.List;
/**
* 用户的持久层接口
*/
public interface IUserDao {
/**
* 查询所有用户
*
* @return
*/
List<User> findAll();
}
Create a new SqlMapConfig.xml file in the resources folder
SqlMapConfig.xml: The file name is generally written as SqlMapConfig.xml.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- mybatis的主配置文件 -->
<configuration>
<!-- 配置环境 -->
<environments default="mysql">
<!-- 配置mysql的环境-->
<environment id="mysql">
<!-- 配置事务的类型-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源(连接池) -->
<dataSource type="POOLED">
<!-- 配置连接数据库的4个基本信息 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件 -->
<mappers>
<mapper resource="com/itheima/dao/IUserDao.xml"/>
</mappers>
</configuration>
Write IUserDao.xml
<?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">
<mapper namespace="com.itheima.dao.IUserDao">
<!--配置查询所有-->
<select id="findAll" resultType="com.itheima.domain.User">
select * from user
</select>
</mapper>
Summary of steps to build mybatis environment
07. Precautions for environmental construction
Precautions for setting up the environment: The
first one: when creating IUserDao.xml and IUserDao.java, the names are to be consistent with our previous knowledge.
In Mybatis, it also calls the operation interface name and mapping file of the persistence layer: Mapper,
so: IUserDao and IUserMapper are the same. (Mapper==Dao)
Second: When creating a directory in idea, it is different from a
package. When the package is created: com.itheima.dao, it is a three-level structure
directory. When created: com.itheima.dao It is the
third level of the first-level directory : the location of the mapping configuration file of mybatis must be the same as the package structure of the dao interface. The
fourth: the value of the namespace attribute of the mapper tag of the mapping configuration file must be the fully qualified class name of the dao interface. The
fifth: The operation configuration of the mapping configuration file (select), the value of the id attribute must be the method name of the dao interfaceWhen we comply with the third, fourth, and fifth points, we no longer need to write dao implementation classes in development.