MyBatis uses -1- to build the running environment of the Maven project and the test cases run through

table of Contents

0, write in front

1. The detailed steps of the new Maven project [myBatis-study]

2. Add MyBatis component dependency in the pom.xml configuration file: pom.xml

3. Create a table in the database and insert test data: RUNOOB.TBL_Employee

4. Write the entity class corresponding to the table: EmployeeEntity.java

5. Write the interface corresponding to the table: EmployeeDao.java

6. Write the mapper configuration file corresponding to the table (various SQL for table operations): mapper.xml

7. Write the global configuration file of MyBatis (register the mapper.xml file in the global configuration file): mybatis-config.xml

8. The final Maven project structure diagram

9. Interface test: EmployeeTest.java

10. Summary of MyBatis steps

11. Refer to MyBatis Video

12. MyBatis video PPT screenshot

0, write in front

(1) The goals of this series of blogs are: MyBatis configuration file writing, MyBatis dynamic SQL, MyBatis caching mechanism, MyBatis-Spring integration, MyBatis reverse engineering, MyBatis advanced content (MyBatis source code analysis, MyBatis single/multiple plug-in operation mechanism, MyBatis four The working principle of large objects, custom TypeHandler, MyBatis stored procedure & cursor processing, etc.). 

(2) MyBatis official website: https://mybatis.org/mybatis-3/zh/index.html

(3) Download the latest MyBatis components and source code: https://github.com/mybatis/mybatis-3/releases

1. The detailed steps of the new Maven project [myBatis-study]

(1)file -》 new -》 project

(2) Select the maven project, select the JDK used by the project

(3) Fill in the groupID and artifactID of the Maven project

(4) Fill in the project name of the Maven project, select the address of the Maven project, and finally click Finish

(5) After creation, the initial Maven project structure diagram

2. Add MyBatis component dependency in the pom.xml configuration file: 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.wind</groupId>
    <artifactId>mybatis-study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--导入使用MyBatis时相关的依赖=start-->
    <dependencies>
        <!--MyBatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>

        <!--MySQL连接依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>

        <!--第三方缓存依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.6.11</version>
        </dependency>

        <!--lombok插件依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
    </dependencies>
    <!--导入使用MyBatis时相关的依赖=end-->


    <!--编译之后的文件中少了mapper.xml,这个和maven有关,maven编译src/java代码的时候,默认只会对java文件进行编译然后放在target/classes目录,需要在pom.xml中加入下面配置-->
    <!--如果不添加此节点,mapper.xml文件、config.properties文件、config.spring文件都不会被加载到target的classes中去,也就不能被使用,也就会报错-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

3. Create a table in the database and insert test data: RUNOOB.TBL_Employee

CREATE TABLE `TBL_Employee` (
  `Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户Id',
  `Name` varchar(255) NOT NULL DEFAULT '' COMMENT '姓名',
  `Gender` char(1) NOT NULL DEFAULT '' COMMENT '性别',
  `Email` varchar(255) NOT NULL DEFAULT '' COMMENT '邮箱',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='员工表';

insert into TBL_Employee(name,gender,email) values("张三","男","[email protected]"),("李四","女","[email protected]"),("王二","女","[email protected]");
select * from TBL_Employee;

4. Write the entity class corresponding to the table: EmployeeEntity.java

package com.wind.entity;

import java.io.Serializable;

public class EmployeeEntity implements Serializable {

    private static final long serialVersionUID = -5639704456233181138L;

    private Integer id;
    private String name;
    private String gender;
    private String email;

// 省略各种setter、getter、toString方法
}

5. Write the interface corresponding to the table: EmployeeDao.java

package com.wind.dao;

import com.wind.entity.EmployeeEntity;
import org.apache.ibatis.annotations.Param;

/**
 * @Description: 这是与MyBatis中的mapper.xml文件相互绑定的接口。注意:该接口是用来操作DB的。
 */
public interface EmployeeDao {

    EmployeeEntity queryEmployeeById(@Param("id") int id);

}

6. Write the mapper configuration file corresponding to the table (various SQL for table operations): mapper.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.xml文件都有一个命名空间,该命名空间表示:该mapper.xml文件和哪个接口绑定起来。
注意:该接口是用来操作DB的。-->
<mapper namespace="com.wind.dao.EmployeeDao">

    <select id="queryEmployeeById" resultType="EmployeeEntity">
        select * from TBL_Employee where id = #{id}
    </select>

</mapper>

7. Write the global configuration file of MyBatis (register the mapper.xml file in the global configuration file): mybatis-config.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">

<configuration>

    <!--MyBatis的设置-->
    <settings>
        <!--开启在控制台打印SQL日志-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!--MyBatis中entity别名的设置-->
    <typeAliases>
        <package name="com.wind.entity"/>
    </typeAliases>

    <!--配置MyBatis运行环境的全局配置文件-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <!--配置POOLED类型的JDBC数据源连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url"
                          value="jdbc:mysql://localhost:3306/RUNOOB?useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <!--把一个个的mapper配置文件注册到全局配置文件中-->
    <mappers>
        <mapper resource="mapper/EmployeeMapper.xml"/>
    </mappers>

</configuration>

8. The final Maven project structure diagram

9. Interface test: EmployeeTest.java

import com.wind.dao.EmployeeDao;
import com.wind.entity.EmployeeEntity;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class EmployeeTest {

    public static void main(String[] args) {
        //加载MyBatis配置文件
        InputStream inputStream = EmployeeTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //获取实现接口的代理对象
        EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
        EmployeeEntity employeeEntity = employeeDao.queryEmployeeById(1);
        System.out.println(employeeEntity);

        //关闭资源
        sqlSession.close();
    }
}

10. Summary of MyBatis steps

11. Refer to MyBatis Video

Site : https://www.bilibili.com/video/BV1mW411M737?p=1

12. MyBatis video PPT screenshot

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/cmm0401/article/details/111720470