The use of the MyBatis framework this time is based on the SpringBoot framework. Create a SpringBBot project in IDEA and select the corresponding dependencies according to your needs.
quick start
- Requirement: Use MyBatis to query all user data
- step:
- Preparations (create Spring Boot project, database user table, entity class User)
- Entity class objects are used to encapsulate results from data
- Refer to the relevant dependencies of MyBatis, configure MyBatis (configure the database information to establish a connection: database driver, URL of the database connection (including information such as the address of the database, port number and database name), database user name and password)
- Configure the mybatis instance as follows:
- In the SpringBoot project, the configuration information is stored in the application.properties file
- Reference related dependencies:
- When you choose to use the Maven mode when creating a SpingBoot project, the dependency information is saved in the pom.xml 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- Generated by https://start.springboot.io --> <!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn --> <groupId>com.example</groupId> <artifactId>springboot-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-mybatis</name> <description>springboot-mybatis</description> <properties> <java.version>20</java.version> </properties> <dependencies> <!-- mybatis的起步依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> <!--mysql的驱动包--> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <!-- springboot项目单元测试的依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter-test</artifactId> <version>3.0.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
- Configure the mybatis instance as follows:
- Write SQL statement (annotation\XML)
- The persistence layer in MyBatis generally contains the name of Mapper, which is the same as the Dao layer in the three-tier architecture. Specific examples:
-
package com.example.mapper; import com.example.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper// 在运行时,会自动生成该接口的实现类对象(代理对象),并且SpringBoot框架会将该对象交给IOC容器管理,称为IOC容器中的bean了,可以通过依赖注入的方式进行嗲用 public interface UserMapper { @Select("select * from user") public List<User> list(); }
-
- The persistence layer in MyBatis generally contains the name of Mapper, which is the same as the Dao layer in the three-tier architecture. Specific examples:
- Preparations (create Spring Boot project, database user table, entity class User)