package com.example.springboot.entity;

public class Employee {
    private Integer id;
    private String name;
    private Integer age;
    private String position;

    // getters and setters
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getPosition() {
        return position;
    }
    public void setPosition(String position) {
        this.position = position;
    }
}
package com.example.springboot.mapper;

import org.apache.ibatis.annotations.Mapper;
import com.example.springboot.entity.Employee;

@Mapper
public interface EmployeeMapper {
    Employee findById(Integer id);
    void insert(Employee employee);
    void update(Employee employee);
    void delete(Integer id);
}
package com.example.springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.springboot.entity.Employee;
import com.example.springboot.mapper.EmployeeMapper;

@Service
public class EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    public Employee findById(Integer id) {
        return employeeMapper.findById(id);
    }
    public void insert(Employee employee) {
        employeeMapper.insert(employee);
    }
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }
    public void delete(Integer id) {
        employeeMapper.delete(id);
    }
}
package com.example.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootApplication {

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

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springboot.mapper.EmployeeMapper">
    <select id="findById" parameterType="int" resultType="com.example.springboot.entity.Employee">
        SELECT * FROM employee WHERE id = #{id}
    </select>
    <insert id="insert" parameterType="com.example.springboot.entity.Employee">
        INSERT INTO employee (name, age, position) VALUES (#{name}, #{age}, #{position})
    </insert>
    <update id="update" parameterType="com.example.springboot.entity.Employee">
        UPDATE employee SET name = #{name}, age = #{age}, position = #{position} WHERE id = #{id}
    </update>
    <delete id="delete" parameterType="int">
        DELETE FROM employee WHERE id = #{id}
    </delete>
</mapper>
src/main/resources/employee.xml

package com.example.springboot;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.springboot.entity.Employee;
import com.example.springboot.service.EmployeeService;

@SpringBootTest
public class EmployeeServiceTest {
    @Autowired
    private EmployeeService employeeService;

    @Test
    public void testInsert() {
        Employee employee = new Employee();
        employee.setName("张三");
        employee.setAge(30);
        employee.setPosition("员工");
        employeeService.insert(employee);
    }

    @Test
    public void testFindById() {
        Employee employee = employeeService.findById(1);
        System.out.println(employee);
    }

    @Test
    public void testUpdate() {
        Employee employee = new Employee();
        employee.setId(1);
        employee.setName("李四");
        employee.setAge(18);
        employee.setPosition("员工");
        employeeService.update(employee);
    }

    @Test
    public void testDelete() {
        employeeService.delete(1);
    }
}
<?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.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>springboot</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.4</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <version>1.18.36</version>
        </dependency>

        <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.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.36</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>

    </build>


</project>

一直显示 MyBatis 无法找到对应的 SQL 映射语句