mybatis搭建项目框架

现在给大家搭建一个mybatis的简单框架。

复制代码
package org.example.mybatis;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("org.example.mybatis")
@SpringBootApplication
public class MybatisApplication {

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

}

package org.example.mybatis;



public class User {

    private Long id;
    private String name;
    private Integer age;

    // getter & setter
    public Long getId() {
        return id;
    }

    public void setId(Long 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;
    }
}

package org.example.mybatis;

import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    private final UserMapper userMapper;

    public UserController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @GetMapping("/list")
    public List<User> list() {
        return userMapper.findAll();
    }

    @GetMapping("/{id}")
    public User get(@PathVariable Long id) {
        return userMapper.findById(id);
    }

    @PostMapping("/add")
    public String add(@RequestBody User user) {
        userMapper.insert(user);
        return "success";
    }
}

package org.example.mybatis;


import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {

    List<User> findAll();

    User findById(Long id);

    int insert(User user);
}

<?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="org.example.mybatis.UserMapper">

    <select id="findAll" resultType="org.example.mybatis.User">
        select * from user
    </select>

    <select id="findById" resultType="org.example.mybatis.User">
        select * from user where id = #{id}
    </select>

    <insert id="insert">
        insert into user(name, age)
        values(#{name}, #{age})
    </insert>

</mapper>

<?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.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis</name>
    <description>mybatis</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>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- MyBatis Spring Boot Starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </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>
                        </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>

server:
  port: 8084

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/testmybatis?useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password:
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:*.xml
  type-aliases-package: org.example.mybatis

代码目录结构如下:

相关推荐
w_t_y_y6 小时前
工具Cursor(三)MCP(3)常用的三方MCP Tools
java
what丶k6 小时前
你应该更新的 Java 知识:Record 特性深度解析
java·开发语言
毕设源码-朱学姐6 小时前
【开题答辩全过程】以 剧本杀服务管理系统的设计与实现为例,包含答辩的问题和答案
java
乐观勇敢坚强的老彭6 小时前
c++信奥寒假营集训01
android·java·c++
☀Mark_LY6 小时前
MyBatis-Flex入门以及多数据源配置
java·mybatis
郑州光合科技余经理6 小时前
同城配送调度系统实战:JAVA微服务
java·开发语言·前端·后端·微服务·中间件·php
独自破碎E6 小时前
【模拟】顺时针旋转矩阵
java·线性代数·矩阵
哪里不会点哪里.7 小时前
什么是 Spring Cloud?
后端·spring·spring cloud
山上三树7 小时前
详细介绍读写锁
开发语言·c++·spring
TsengOnce7 小时前
Docker 安装达梦8数据库-5步成功
java·数据库