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使用Statement接口執(zhí)行SQL語(yǔ)句操作實(shí)例分析
這篇文章主要介紹了Java使用Statement接口執(zhí)行SQL語(yǔ)句操作,結(jié)合實(shí)例形式詳細(xì)分析了Java使用Statement接口針對(duì)mysql數(shù)據(jù)庫(kù)進(jìn)行連接與執(zhí)行SQL語(yǔ)句增刪改查等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-07-07SpringBoot優(yōu)先加載指定Bean的實(shí)現(xiàn)
SpringBoot框架在啟動(dòng)時(shí)可以自動(dòng)將托管的Bean實(shí)例化,一般情況下它的依賴(lài)注入特性可以正確處理Bean之間的依賴(lài)關(guān)系,無(wú)需手動(dòng)指定某個(gè) Bean優(yōu)先創(chuàng)建實(shí)例,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-05-05Java ConcurrentModificationException異常解決案例詳解
這篇文章主要介紹了Java ConcurrentModificationException異常解決案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09MyBatis實(shí)現(xiàn)三級(jí)樹(shù)查詢(xún)的示例代碼
在實(shí)際項(xiàng)目開(kāi)發(fā)中,樹(shù)形結(jié)構(gòu)的數(shù)據(jù)查詢(xún)是一個(gè)非常常見(jiàn)的需求,比如組織架構(gòu)、菜單管理、地區(qū)選擇等場(chǎng)景都需要處理樹(shù)形數(shù)據(jù),本文將詳細(xì)講解如何使用MyBatis實(shí)現(xiàn)三級(jí)樹(shù)形數(shù)據(jù)的查詢(xún),需要的朋友可以參考下2024-12-12自從在 IDEA 中用了熱部署神器 JRebel 之后,開(kāi)發(fā)效率提升了 10(真棒)
在javaweb開(kāi)發(fā)過(guò)程中,使用熱部署神器 JRebel可以使class類(lèi)還是更新spring配置文件都能立馬見(jiàn)到效率,本文給大家介紹JRebel的兩種安裝方法,小編建議使用第二種方法,具體安裝步驟跟隨小編一起看看吧2021-06-06Java程序流程控制:判斷結(jié)構(gòu)、選擇結(jié)構(gòu)、循環(huán)結(jié)構(gòu)原理與用法實(shí)例分析
這篇文章主要介紹了Java程序流程控制:判斷結(jié)構(gòu)、選擇結(jié)構(gòu)、循環(huán)結(jié)構(gòu)原理與用法,結(jié)合實(shí)例形式分析了Java流程控制中判斷結(jié)構(gòu)、選擇結(jié)構(gòu)、循環(huán)結(jié)構(gòu)相關(guān)原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04Spring Data Jpa實(shí)現(xiàn)分頁(yè)和排序代碼實(shí)例
本篇文章主要介紹了Spring Data Jpa實(shí)現(xiàn)分頁(yè)和排序代碼實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03Java參數(shù)校驗(yàn)@Validated、@Valid介紹及使用詳解
Javax.validation是?spring?集成自帶的一個(gè)參數(shù)校驗(yàn)接口,可通過(guò)添加注解來(lái)設(shè)置校驗(yàn)條件,這篇文章主要介紹了Java參數(shù)校驗(yàn)@Validated、@Valid介紹及使用詳解,需要的朋友可以參考下2024-08-08