您好,登錄后才能下訂單哦!
這篇“SpringBoot怎么整合SQLite數(shù)據(jù)庫”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“SpringBoot怎么整合SQLite數(shù)據(jù)庫”文章吧。
SQLite是一個進程內(nèi)的庫,實現(xiàn)了自給自足的、無服務(wù)器的、零配置的、事務(wù)性的 SQL 數(shù)據(jù)庫引擎。它是一個零配置的數(shù)據(jù)庫,這意味著與其他數(shù)據(jù)庫不一樣,您不需要在系統(tǒng)中配置。
就像其他數(shù)據(jù)庫,SQLite 引擎不是一個獨立的進程,可以按應(yīng)用程序需求進行靜態(tài)或動態(tài)連接。SQLite 直接訪問其存儲文件。
ACID事務(wù)
零配置 – 無需安裝和管理配置
儲存在單一磁盤文件中的一個完整的數(shù)據(jù)庫
數(shù)據(jù)庫文件可以在不同字節(jié)順序的機器間自由的共享
支持數(shù)據(jù)庫大小至2TB
足夠小, 大致13萬行C代碼, 4.43M
比一些流行的數(shù)據(jù)庫在大部分普通數(shù)據(jù)庫操作要快
簡單, 輕松的API
包含TCL綁定, 同時通過Wrapper支持其他語言的綁定
良好注釋的源代碼, 并且有著90%以上的測試覆蓋率
獨立: 沒有額外依賴
源碼完全的開源, 你可以用于任何用途, 包括出售它
支持多種開發(fā)語言,C, C++, PHP, Perl, Java, C#,Python, Ruby等
<dependencies> <!--web應(yīng)用基本環(huán)境配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--sqlite--> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> </dependency> <!-- jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies>
SQLite只需要關(guān)聯(lián)一個.db文件,就能實現(xiàn)數(shù)據(jù)庫的連接操作。
spring.datasource.driver-class-name=org.sqlite.JDBC #絕對位置配置方式 #spring.datasource.url=jdbc:sqlite:E:/db/test.db #相對位置配置方式 spring.datasource.url=jdbc:sqlite::resource:db/test.db
在如下位置,手動創(chuàng)建一個 test.db 空文件
@Autowired private JdbcTemplate jdbcTemplate;
// 1、建表 DDL String createUser = "create table user(" + "id integer primary key autoincrement," + "name varchar(20)," + "age integer" + ")"; jdbcTemplate.update(createUser); // 2、插入數(shù)據(jù) String insertUserData = "insert into user(name,age) values ('張三',18),('李四',20)"; jdbcTemplate.update(insertUserData); // 3、查詢語句 String selectUserData = "select * from user"; List<Map<String, Object>> list = jdbcTemplate.queryForList(selectUserData); for (Map<String, Object> map : list) { for (Map.Entry<String, Object> entry : map.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); } } // 5、刪除整張表 String dropTable = "drop table user"; jdbcTemplate.update(dropTable);
完整測試代碼
package com.study.myweb; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Controller; import java.util.List; import java.util.Map; @SpringBootApplication public class MyWebApplication implements CommandLineRunner { @Autowired private JdbcTemplate jdbcTemplate; public static void main(String[] args) { SpringApplication.run(MyWebApplication.class, args); } @Override public void run(String... args) throws Exception { // 1、建表 DDL String createUser = "create table user(" + "id integer primary key autoincrement," + "name varchar(20)," + "age integer" + ")"; jdbcTemplate.update(createUser); // 2、插入數(shù)據(jù) String insertUserData = "insert into user(name,age) values ('張三',18),('李四',20)"; jdbcTemplate.update(insertUserData); // 3、查詢語句 String selectUserData = "select * from user"; List<Map<String, Object>> list = jdbcTemplate.queryForList(selectUserData); for (Map<String, Object> map : list) { for (Map.Entry<String, Object> entry : map.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); } } // 4、刪除整張表 String dropTable = "drop table user"; jdbcTemplate.update(dropTable); } }
以上就是關(guān)于“SpringBoot怎么整合SQLite數(shù)據(jù)庫”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。