SpringBoot項目整合MybatisPlus并使用SQLite作為數(shù)據(jù)庫的過程
SQLite介紹
- SQLite 是一個進程內(nèi)庫,它實現(xiàn)了獨立的、無服務器的、零配置的事務性 SQL 數(shù)據(jù)庫引擎。SQLite 沒有單獨的服務器進程。SQLite直接讀取和寫入普通磁盤文件,就是一個完整的 SQL 數(shù)據(jù)庫,包含多個表、索引、 觸發(fā)器和視圖包含在單個磁盤文件中。 數(shù)據(jù)庫文件格式是跨平臺的
- 可以自由復制數(shù)據(jù)庫在 32 位和 64 位系統(tǒng)之間,或在 big-endian 和 little-endian 體系結(jié)構(gòu)之間。這些功能使SQLite成為流行的選擇一種應用程序文件格式。SQLite 數(shù)據(jù)庫文件是美國國會圖書館推薦的存儲格式
- 免費
- 在世界上應用廣泛
- SQLite是一個緊湊的庫,啟用所有功能后,庫大小可以小于 750KiB, 具體取決于目標平臺和編譯器優(yōu)化設置。 內(nèi)存使用量和速度之間需要權(quán)衡。 你給它內(nèi)存越多,SQLite通常運行得越快。盡管如此,在低內(nèi)存環(huán)境中,性能通常也相當不錯。根據(jù)它的使用方式,SQLite 可能比直接文件系統(tǒng) I/O 更快
搭建項目
創(chuàng)建項目
修改pom.xml
因為使用SpringBoot 3.2.1 出了一些問題,下面改成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啟動插件 --> <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ū)動包 --> <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運行項目,會報如下錯誤
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ū)動包 --> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.32.3.3</version> </dependency>
SQLite
查看SQLite是否安裝
我的開發(fā)機是mac系統(tǒng),自動了sqlite3,如果你們沒有的話,要去安裝,可以參考官方文檔的快速開始: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ù)庫
從一個目錄進入終端,創(chuàng)建數(shù)據(jù)庫
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ù)庫是否創(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ù)庫
連接成功
后端增刪改查接口實現(xiàn)
MybatisX生成代碼
如果沒有安裝如下插件的話,先安裝一下
生成成功
創(chuàng)建如下項目結(jié)構(gòu),并粘貼所生成的代碼過去
注意,mapper.xml的實體類引用要和你項目的一致
不會生成看這個
因為只是非常簡單的案例,這里先不使用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 針對表【user】的數(shù)據(jù)庫操作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); } /** * 查詢數(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要對應sqlite數(shù)據(jù)庫
# Tomcat server: port: 8899 #spring spring: datasource: #引用項目中的數(shù)據(jù)庫文件 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
啟動類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); } }
測試
我這邊使用接口測試工具Apifox來進行測試,使用Postman等其他工具也是可以的,不過我強烈推薦Apifox,感覺非常好用
插入用戶
查詢所有用戶
修改用戶名稱
再查一次,修改成功
刪除用戶
再查一遍,刪除成功
到此這篇關(guān)于SpringBoot項目整合MybatisPlus并使用SQLite作為數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)SpringBoot整合MybatisPlus內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用Statement接口執(zhí)行SQL語句操作實例分析
這篇文章主要介紹了Java使用Statement接口執(zhí)行SQL語句操作,結(jié)合實例形式詳細分析了Java使用Statement接口針對mysql數(shù)據(jù)庫進行連接與執(zhí)行SQL語句增刪改查等相關(guān)操作技巧與注意事項,需要的朋友可以參考下2018-07-07SpringBoot優(yōu)先加載指定Bean的實現(xiàn)
SpringBoot框架在啟動時可以自動將托管的Bean實例化,一般情況下它的依賴注入特性可以正確處理Bean之間的依賴關(guān)系,無需手動指定某個 Bean優(yōu)先創(chuàng)建實例,文中有詳細的代碼示例,需要的朋友可以參考下2023-05-05Java ConcurrentModificationException異常解決案例詳解
這篇文章主要介紹了Java ConcurrentModificationException異常解決案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09自從在 IDEA 中用了熱部署神器 JRebel 之后,開發(fā)效率提升了 10(真棒)
在javaweb開發(fā)過程中,使用熱部署神器 JRebel可以使class類還是更新spring配置文件都能立馬見到效率,本文給大家介紹JRebel的兩種安裝方法,小編建議使用第二種方法,具體安裝步驟跟隨小編一起看看吧2021-06-06Java程序流程控制:判斷結(jié)構(gòu)、選擇結(jié)構(gòu)、循環(huán)結(jié)構(gòu)原理與用法實例分析
這篇文章主要介紹了Java程序流程控制:判斷結(jié)構(gòu)、選擇結(jié)構(gòu)、循環(huán)結(jié)構(gòu)原理與用法,結(jié)合實例形式分析了Java流程控制中判斷結(jié)構(gòu)、選擇結(jié)構(gòu)、循環(huán)結(jié)構(gòu)相關(guān)原理、用法及操作注意事項,需要的朋友可以參考下2020-04-04Spring Data Jpa實現(xiàn)分頁和排序代碼實例
本篇文章主要介紹了Spring Data Jpa實現(xiàn)分頁和排序代碼實例,具有一定的參考價值,有興趣的可以了解一下。2017-03-03Java參數(shù)校驗@Validated、@Valid介紹及使用詳解
Javax.validation是?spring?集成自帶的一個參數(shù)校驗接口,可通過添加注解來設置校驗條件,這篇文章主要介紹了Java參數(shù)校驗@Validated、@Valid介紹及使用詳解,需要的朋友可以參考下2024-08-08