Springboot integrates mybatis to define entity category names and scan mapper interfaces

Insert picture description here1. Add the
following dependencies in the pom.xml file of the project :

<?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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>

    </parent>
    <groupId>cn.itcast</groupId>
    <artifactId>Spingboot_day1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.9</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--java配置配置数据库要的-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!--Spring Boot的属性注入需要的-->
        <dependency>
            <groupId> org.springframework.boot </groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <!--不传递依赖-->
            <optional>true</optional>
        </dependency>

        <!--lombok需要的-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--事务和连接池需要的-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

        <!--mybatis需要的-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
    </dependencies>
</project>
  1. Configure application.yml, the common configuration is as follows:Insert picture description here

spring:
  profiles:
    active: abc,def
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot_test
    username: root
    data-password: root

#tomcat端口
server:
  port: 81

#日志记录级别
logging:
  level:
    com.itheima: debug
    org.springframework: info

# mybatis配置
mybatis:
  type-aliases-package: com.itheima.pojo
  # 映射文件路径 (一般在启动类加注解就行)
  # mapper-locations: classpath:mappers/*.xml(一般在启动类加注解就行)
  configuration:
    # 控制台输出执行sql log-impl: 
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  

3. Create the mapper interface:

package com.itheima.mapper;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/2/21 22:39
 */
public interface UserMapper {
    
    
}

4. Configure Mapper scanning in the launcher class

package com.itheima;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/2/19 14:27
 */

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * spring boot工程都有一个启动引导类,这是工程的入口类
 * 并在引导类上添加@SpringBootApplication
 */
@SpringBootApplication
//扫描mybatis所有的业务mapper接口
@MapperScan("com.itheima.mapper")
public class Application {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Application.class,args);
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/GLOAL_COOK/article/details/113925368