MyBatis Framework 01

(1) Overview
of MyBatis I learned about the MyBatis framework in the first week of school, and probably understood what MyBatis is. MyBatis is an excellent persistence layer framework that supports ordinary SQL queries, stored procedures and advanced mapping. MyBatis eliminates almost all manual setting of JDBC code and parameters and retrieval of result sets. MyBatis uses simple XML or annotations for configuration and primitive mapping, mapping interfaces and Java POJOs (Plan Old Java Objects, ordinary Java objects) into records in the database.
Insert picture description here
MyBatis is an excellent persistence layer framework that supports ordinary SQL queries, stored procedures and advanced mapping. MyBatis eliminates almost all manual setting of JDBC code and parameters and retrieval of result sets. MyBatis uses simple XML or annotations for configuration and primitive mapping, mapping interfaces and Java POJOs (Plan Old Java Objects, ordinary Java objects) into records in the database.
Find MyBatis in the Maven repository-https://mvnrepository.com/artifact/org.mybatis/mybatis
Insert picture description here
2. Create database and table
1. Create MySQL database testdb in Navicat
Insert picture description here

2、创建用户表 - t_user
CREATE TABLE t_user (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) DEFAULT NULL,
age int(11) DEFAULT NULL,
address varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
Insert picture description here

3. Insert 3 records in the user table
INSERT INTO t_userVALUES ('1','Li Honggang', '20','Fourth Floor, Building 3, Jiayu Garden, Jiangyang District 15#');
INSERT INTO t_userVALUES ('2', ' Wang Yunhua', '30','Three teams of Hongyu Village, Dadu Town, Naxi District');
INSERT INTO t_userVALUES ('3','Zheng Xiaocui', '21','No. 15, 5th Floor, Building 2, Laojiao Garden, Jiangyang District ');

View user table records

Insert picture description here
3. Case demonstration MyBatis basic use
(1) Create a Maven project-MyBatisDemo
Insert picture description here
(2) Add the corresponding dependency in the pom file

<?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>net.hw.mybatis</groupId>
    <artifactId>MyBatisDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

</project>

(2) Add the corresponding dependency in the pom file

<?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>net.hw.mybatis</groupId>
    <artifactId>MyBatisDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

</project>

(3) Create a user entity class corresponding to the user table-User
Insert picture description here
(4) Create a user entity relationship mapping configuration file
Insert picture description here
(5) Create a log attribute file
Insert picture description here
(6) Create a test class to test user operations
1. Create a test class-TestUserOperation
Insert picture description here

2. Run test method-testFindById()
Insert picture description here
3. Run test method-testFindAll()
Insert picture description here
(2) Create test class Test user mapper interface
1. Create test class-TestUserMapper
Insert picture description here

2. Run the test method-testFindById()
Insert picture description here
3. Run the test method-testFindAll() In
Insert picture description here
summary, when learning the MyBatis framework, when the package name was created, the word contained capital letters, which caused the program to report an error.

Guess you like

Origin blog.csdn.net/triet/article/details/114325987