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

代码目录结构如下:

相关推荐
初子无爱12 小时前
Java接入支付宝沙箱支付教程
java·开发语言
BD_Marathon12 小时前
AOP入门案例
spring
程序猿零零漆12 小时前
Spring之旅 - 记录学习 Spring 框架的过程和经验(十)基于注解配置的AOP使用
java·学习·spring
努力的小郑13 小时前
SQL 性能避坑:为什么阿里强制禁用 ORDER BY RAND()?
java·mysql·性能优化
悟能不能悟13 小时前
前端调用a服务,a服务将请求用controller+openfeign调用b服务,接口参数中有header参数和body,a服务应该怎么设置,才简单
java·开发语言·前端
2501_9418859613 小时前
从接口演化到系统自治的互联网工程语法重构与多语言实践思路拆解分享文
java·开发语言
2501_9418053113 小时前
在阿姆斯特丹智能港口场景中构建集装箱实时调度与高并发物流数据分析平台的工程设计实践经验分享
java·大数据·算法
小许学java13 小时前
网络原理-HTTP/HTTPS
java·网络·http·https
大爱编程♡13 小时前
JAVAEE-Spring Web MVC
前端·spring·java-ee
panamera1213 小时前
C++ 中 static 关键字
java·开发语言·c++