SpringBoot集成Mybatis注解版

单淳
2023-12-01

目录

1、导入依赖

2、实体类

3、Mapper类

4、controller

5、启动类

6、启动成功、访问路径


简单记录一下spingboot集成Mybatis注解版过程。

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>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.michael</groupId>
    <artifactId>my-blog</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-blog</name>
    <description>Demo project for Vue</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <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>2.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、实体类

package com.michael.myblog.entity;

/**
 * @ClassName: Customer
 * @Package com.michael.myblog.entity
 * @Description: TODO
 * @Author Mr.Z
 * @Date 2020/7/26 19:56
 * @Version 1.0
 */
public class Customer {
    private String name;
    private String phone;
    private String email;

    //getter、setter方法....
}

3、Mapper类

package com.michael.myblog.dao;

import com.michael.myblog.entity.Customer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName: CustomerMapper
 * @Package com.michael.myblog.dao
 * @Description: TODO
 * @Author Mr.Z
 * @Date 2020/7/26 19:59
 * @Version 1.0
 */
@Mapper
@Repository
public interface CustomerMapper {
    @Select("select * from customer")
    List<Customer> getCustomers();
}

4、controller

@RestController
@RequestMapping("/customer")
public class UserController {

    @Autowired
    private CustomerMapper customerMapper;

    @GetMapping
    public List<Customer> getCustomers() {
        return customerMapper.getCustomers();
    }
}

5、启动类

@SpringBootApplication
@MapperScan("com.michael.myblog.dao") //扫描Mapper类
public class MyBlogApplication {

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

}

6、启动成功、访问路径

 

 

 

 类似资料: