MyBatis-入门-快速入门程序

本次使用MyBatis框架是基于SpringBoot框架进行的,在IDEA中创建一个SpringBBot工程,根据自己的需求选择对应的依赖即可

快速入门

  • 需求:使用MyBatis查询所有用户数据
  • 步骤:
    • 准备工作(创建Spring Boot工程、数据库user表、实体类User)
      • 实体类对象用于封装从数据结果
    • 引用MyBatis的相关依赖,配置MyBatis(配置要建立连接的数据库信息:数据库驱动程序,数据库连接的URL(包含了数据库的地址、端口号和数据库名称等信息),数据库用户名和密码)
      • 配置mybatis实例如下:
      • 在SpringBoot工程中,配置信息保存在application.properties文件中
      • 引用相关依赖:
      • 在创建SpingBoot项目时选择使用的时Maven模式,依赖信息保存在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 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>
          
    • 编写SQL语句(注解\XML)
      • MyBatis中持久层一般是含有Mapper的名称,与三层架构中Dao层一样。具体实例:
        • 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();
          }
          

猜你喜欢

转载自blog.csdn.net/weixin_64939936/article/details/131905644