SpringBoot与MyBatis简单整合


步骤一、配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>

    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.5
         
    
    com.shen
    springboot-mybatis
    0.0.1-SNAPSHOT
    springboot-mybatis
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



步骤二、实体类

package com.shen.springbootmybatis.model;

public class User {

    private int id;
    private String username;
    private String password;

    public User() {
    }

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "user{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

步骤三、XxxMapper接口类

package com.shen.springbootmybatis.mapper;

import com.shen.springbootmybatis.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserXmlMapper {

    public List findAll();
}

步骤四、XxxMapper接口映射的XML

<?xml version="1.0" encoding="UTF-8" ?>


    
        
        
        
    

    

步骤五、application.yml

spring:
#  数据源信息配置
  datasource:
    url: jdbc:mysql://192.168.20.1:3306/dbtmp?serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

# mybatis
mybatis:
#  mapper 映射文件路径
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.shen.springbootmybatis.model
#  config-location: --指定mybatis核心配置文件

步骤六、单元测试

package com.shen.springbootmybatis;

import com.shen.springbootmybatis.mapper.UserMapper;
import com.shen.springbootmybatis.mapper.UserXmlMapper;
import com.shen.springbootmybatis.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class SpringbootMybatisApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserXmlMapper userXmlMapper;

    @Test
    public void testFindAll() {
        List users = userMapper.findAll();
        System.out.println(users);
    }

    @Test
    public void testFindAllXml() {
        List users = userXmlMapper.findAll();
        System.out.println(users);
    }

}