springboot整合mybatis详细教程

1,打开idea创建springboot项目,点击Spring Initializr

springboot整合mybatis详细教程_第1张图片

2,随便填写点击next,java version是jdk版本

springboot整合mybatis详细教程_第2张图片

3,就选一个lombok,其他jar包在pom手动导入

springboot整合mybatis详细教程_第3张图片

4,直接finish

springboot整合mybatis详细教程_第4张图片

5,在pom.xml文件中导入三个整合mybatis所需的依赖jar包


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.1version>
        <relativePath/> 
    parent>
    <groupId>com.phgroupId>
    <artifactId>springboot-mybatisartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>springboot-mybatisname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.2version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.47version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>

project>

6,在application.yml中配置druid连接池(只是简单配置)

spring:
  #druid连接池配置
  datasource:
    druid:
      username: root
      password: 123456
      url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
      driver-class-name: com.mysql.jdbc.Driver
      db-type: com.alibaba.druid.pool.DruidDataSource

7,创建实体类

package com.ph.springbootmybatis.entity;

import lombok.Data;

@Data
public class Student {
     

    private long id;
    private String name;
}

8,创建mapper接口

package com.ph.springbootmybatis.mapper;

import com.ph.springbootmybatis.entity.Student;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface StudentMapper {
     
    List<Student> findAll();
}


9,在启动类上添加注解扫描(用来扫描mapper类)

package com.ph.springbootmybatis;

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

@SpringBootApplication
@MapperScan("com.ph.mapper")//扫描mapper包下的所有类
public class SpringbootMybatisApplication {
     

    public static void main(String[] args) {
     
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

10,创建mapper.xml文件



<mapper namespace="com.ph.springbootmybatis.mapper.StudentMapper">
    <select id="findAll" resultType="com.ph.springbootmybatis.entity.Student">
        select * from student
    select>
mapper>

11,在application.yml中配置扫描映射文件

#配置mybatis
mybatis:
  #扫描xml文件
  mapper-locations: classpath:mapper/*.xml
  configuration:
    #开启驼峰命名法
    map-underscore-to-camel-case: true

12,测试

package com.ph.springbootmybatis.mapper;

import com.ph.springbootmybatis.entity.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class StudentMapperTest {
     
    @Autowired
    private StudentMapper studentMapper;

    @Test
    void findAll() {
     
        List<Student> students = studentMapper.findAll();
        for (Student student : students) {
     
            System.out.println(student);
        }
    }
}

测试结果:
springboot整合mybatis详细教程_第5张图片

总结:

根据以上操作就能把springboot与mybatis整合,注意点就是mybatis的jar包是整合包mybatis-spring-boot-starter,然后就是在启动类上需要加上注解@MapperScan(“mapper包的路径”)或者在需要交给spring容器管理的mapper接口上加上@Mapper注解(以上操作中两种方式都实现了,一种即可生效),还有application配置文件中需要配置需要扫描的mapper映射文件的位置,mybatis的其他配置看项目需求。

你可能感兴趣的:(java,springboot,mybatis,mybatis,spring,spring,boot,maven,java)