SpringBoot項(xiàng)目整合MybatisPlus并使用SQLite作為數(shù)據(jù)庫(kù)的過(guò)程
SQLite介紹
- SQLite 是一個(gè)進(jìn)程內(nèi)庫(kù),它實(shí)現(xiàn)了獨(dú)立的、無(wú)服務(wù)器的、零配置的事務(wù)性 SQL 數(shù)據(jù)庫(kù)引擎。SQLite 沒(méi)有單獨(dú)的服務(wù)器進(jìn)程。SQLite直接讀取和寫(xiě)入普通磁盤(pán)文件,就是一個(gè)完整的 SQL 數(shù)據(jù)庫(kù),包含多個(gè)表、索引、 觸發(fā)器和視圖包含在單個(gè)磁盤(pán)文件中。 數(shù)據(jù)庫(kù)文件格式是跨平臺(tái)的
- 可以自由復(fù)制數(shù)據(jù)庫(kù)在 32 位和 64 位系統(tǒng)之間,或在 big-endian 和 little-endian 體系結(jié)構(gòu)之間。這些功能使SQLite成為流行的選擇一種應(yīng)用程序文件格式。SQLite 數(shù)據(jù)庫(kù)文件是美國(guó)國(guó)會(huì)圖書(shū)館推薦的存儲(chǔ)格式
- 免費(fèi)
- 在世界上應(yīng)用廣泛
- SQLite是一個(gè)緊湊的庫(kù),啟用所有功能后,庫(kù)大小可以小于 750KiB, 具體取決于目標(biāo)平臺(tái)和編譯器優(yōu)化設(shè)置。 內(nèi)存使用量和速度之間需要權(quán)衡。 你給它內(nèi)存越多,SQLite通常運(yùn)行得越快。盡管如此,在低內(nèi)存環(huán)境中,性能通常也相當(dāng)不錯(cuò)。根據(jù)它的使用方式,SQLite 可能比直接文件系統(tǒng) I/O 更快
搭建項(xiàng)目
創(chuàng)建項(xiàng)目


修改pom.xml
因?yàn)槭褂肧pringBoot 3.2.1 出了一些問(wèn)題,下面改成2.5.14
<?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.5.14</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.dam</groupId>
<artifactId>increment-backup-serve</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>increment-backup-serve</name>
<description>increment-backup-serve</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- web啟動(dòng)插件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- sqlite3驅(qū)動(dòng)包 -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.21.0.1</version>
</dependency>
<!--mybatis-plus插件-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>使用Macbook pro運(yùn)行項(xiàng)目,會(huì)報(bào)如下錯(cuò)誤
Caused by: java.lang.Exception: No native library is found for os.name=Mac and os.arch=aarch64. path=/org/sqlite/native/Mac/aarch64
解決方案,修改版本
<!-- sqlite3驅(qū)動(dòng)包 -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.32.3.3</version>
</dependency>SQLite
查看SQLite是否安裝
我的開(kāi)發(fā)機(jī)是mac系統(tǒng),自動(dòng)了sqlite3,如果你們沒(méi)有的話(huà),要去安裝,可以參考官方文檔的快速開(kāi)始:https://www.sqlite.org/quickstart.html
mac@MacdeMac-Pro ~ % sqlite3 SQLite version 3.39.4 2022-09-07 20:51:41 Enter ".help" for usage hints. Connected to a transient in-memory database. Use ".open FILENAME" to reopen on a persistent database.
創(chuàng)建數(shù)據(jù)庫(kù)
從一個(gè)目錄進(jìn)入終端,創(chuàng)建數(shù)據(jù)庫(kù)
mac@MacdeMac-Pro sql % sqlite3 increment-backup.db; SQLite version 3.39.4 2022-09-07 20:51:41 Enter ".help" for usage hints.
查看數(shù)據(jù)庫(kù)是否創(chuàng)建成功
sqlite> .databases main: /Volumes/MacSpan/Projects/increment-backup/sql/DatabaseName.db r/w
創(chuàng)建成功,出現(xiàn)如下文件

創(chuàng)建數(shù)據(jù)表
創(chuàng)建數(shù)據(jù)表
sqlite> create table user ...> ( ...> id INTEGER not null primary key autoincrement, ...> name varchar(20) ...> );
查看數(shù)據(jù)表
sqlite> .tables user
IDEA連接SQLite


連接成功

navicat連接SQLite數(shù)據(jù)庫(kù)


連接成功

后端增刪改查接口實(shí)現(xiàn)
MybatisX生成代碼
如果沒(méi)有安裝如下插件的話(huà),先安裝一下




生成成功

創(chuàng)建如下項(xiàng)目結(jié)構(gòu),并粘貼所生成的代碼過(guò)去

注意,mapper.xml的實(shí)體類(lèi)引用要和你項(xiàng)目的一致

不會(huì)生成看這個(gè)
因?yàn)橹皇欠浅:?jiǎn)單的案例,這里先不使用service包下的代碼
User
package org.dam.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
/**
*
* @TableName user
*/
@TableName(value ="user")
@Data
public class User implements Serializable {
/**
*
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
*
*/
private String name;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
User other = (User) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", name=").append(name);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}UserMapper
package org.dam.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.dam.entity.User;
/**
* @author mac
* @description 針對(duì)表【user】的數(shù)據(jù)庫(kù)操作Mapper
* @createDate 2024-01-18 21:12:12
* @Entity generator.entity.User
*/
public interface UserMapper extends BaseMapper<User> {
}UserMapper.xml
<?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.dam.mapper.UserMapper">
<resultMap id="BaseResultMap" type="org.dam.entity.User">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id,name
</sql>
</mapper>controller
package org.dam.controller;
import org.dam.entity.User;
import org.dam.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @Author dam
* @create 2024/1/18 20:37
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserMapper userMapper;
/**
* 增添數(shù)據(jù)
*/
@GetMapping("/insert")
public Object insert(String name) {
User user = new User();
user.setName(name);
return userMapper.insert(user);
}
/**
* 查詢(xún)數(shù)據(jù)
*/
@GetMapping("/show")
public Object show() {
return userMapper.selectList(null);
}
/**
* 刪除數(shù)據(jù)
*/
@DeleteMapping("/delete")
public Object delete(Integer id) {
return userMapper.deleteById(id);
}
/**
* 修改數(shù)據(jù)
*/
@GetMapping("/update")
public Object update(Integer id, String name) {
User user = new User();
user.setId(id);
user.setName(name);
return userMapper.updateById(user);
}
}創(chuàng)建配置文件application.yaml
注意url要對(duì)應(yīng)sqlite數(shù)據(jù)庫(kù)

# Tomcat
server:
port: 8899
#spring
spring:
datasource:
#引用項(xiàng)目中的數(shù)據(jù)庫(kù)文件
driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite::resource:static/sqlite/increment-backup.db
username:
password:
# 指定靜態(tài)資源的路徑
web:
resources:
static-locations: classpath:/static/
#mybatis:
# mapper-locations: classpath*:mapper/**/*.xml啟動(dòng)類(lèi)IncrementBackupServeApplication
package org.dam;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("org.dam.mapper")
public class IncrementBackupServeApplication {
public static void main(String[] args) {
SpringApplication.run(IncrementBackupServeApplication.class, args);
}
}測(cè)試
我這邊使用接口測(cè)試工具Apifox來(lái)進(jìn)行測(cè)試,使用Postman等其他工具也是可以的,不過(guò)我強(qiáng)烈推薦Apifox,感覺(jué)非常好用
插入用戶(hù)

查詢(xún)所有用戶(hù)

修改用戶(hù)名稱(chēng)

再查一次,修改成功

刪除用戶(hù)

再查一遍,刪除成功

到此這篇關(guān)于SpringBoot項(xiàng)目整合MybatisPlus并使用SQLite作為數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)SpringBoot整合MybatisPlus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java多線(xiàn)程編程之為什么要進(jìn)行數(shù)據(jù)同步
數(shù)據(jù)同步就是指在同一時(shí)間,只能由一個(gè)線(xiàn)程來(lái)訪(fǎng)問(wèn)被同步的類(lèi)變量,當(dāng)前線(xiàn)程訪(fǎng)問(wèn)完這些變量后,其他線(xiàn)程才能繼續(xù)訪(fǎng)問(wèn),下面看一下為什么要進(jìn)行數(shù)據(jù)同步2014-01-01
java通過(guò)HTTP接收json詳細(xì)實(shí)例代碼
Java作為一門(mén)廣泛使用的編程語(yǔ)言,很多開(kāi)發(fā)人員會(huì)用它來(lái)進(jìn)行http請(qǐng)求,獲取json數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于java通過(guò)HTTP接收json的相關(guān)資料,需要的朋友可以參考下2023-11-11
Java枚舉的七種常見(jiàn)用法總結(jié)(必看)
下面小編就為大家?guī)?lái)一篇Java枚舉的七種常見(jiàn)用法總結(jié)(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
Java static 與 final關(guān)鍵字實(shí)例詳解
本文詳細(xì)介紹了Java中的static和final關(guān)鍵字,包括它們的本質(zhì)、內(nèi)存分配、線(xiàn)程安全問(wèn)題以及在類(lèi)加載過(guò)程中的內(nèi)存變化,通過(guò)舉例和解釋,感興趣的朋友跟隨小編一起看看吧2026-01-01
mybatis-plus中BaseMapper入門(mén)使用
本文主要介紹了mybatis-plus中BaseMapper入門(mén)使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
deepseek本地部署及java、python調(diào)用步驟詳解
這篇文章主要介紹了如何下載和使用Ollama模型,包括安裝JDK?17及以上版本和Spring?Boot?3.3.6,配置pom文件和application.yml,創(chuàng)建Controller,以及使用Python調(diào)用模型,需要的朋友可以參考下2025-02-02
一文搞懂Java創(chuàng)建線(xiàn)程的五種方法
本文主要為大家詳細(xì)介紹一下Java實(shí)現(xiàn)線(xiàn)程創(chuàng)建的五種常見(jiàn)方式,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的幫助,感興趣的可以跟隨小編學(xué)習(xí)一下2022-06-06

