Bootstrap

Spring,Spring Boot中使用MyBatis简单教程

Spring,Spring Boot中使用MyBatis简单教程

1. 概述

MyBatis是Java应用程序中实现SQL数据库访问最常用的开源框架之一。

在这个快速教程中,我们将介绍如何将MyBatis与Spring和Spring Boot集成

2. 定义数据库模型

首先,让我们从定义简单的POJO开始,我们将在下文使用它:

public class Article {
    private Long id;
    private String title;
    private String author;
 
    // constructor, standard getters and setters
}

对应的SQL schema 文件:

CREATE TABLE IF NOT EXISTS `ARTICLES`(
    `id`          INTEGER PRIMARY KEY,
    `title`       VARCHAR(100) NOT NULL,
    `author`      VARCHAR(100) NOT NULL
);

接下来,让我们创建一个data.sql文件,它简单地将一条记录插入到我们的articles表中:

INSERT INTO ARTICLES
VALUES (1, 'Working with MyBatis in Spring', 'Baeldung');

注意上面的两个SQL文件必须包含在classpath中

;