SpringBoot快速整合SqLite
SpringBoot 快速整合SqLite
1)SqLite简介
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月。 至2021年已经接近有21个年头,SQLite也迎来了一个版本 SQLite 3已经发布。
2)依赖导入
<!-- 添加数据库依赖-->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
<!-- 从 Hibernate 6 开始,支持 SQLite 方言。-->
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-community-dialects -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-community-dialects</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
3)Yaml配置
--- #配置SQLLite数据库信息
sqlLite:
dbFilePath: ${APP_DB:edge.db}
spring:
datasource:
url: jdbc:sqlite:${sqlLite.dbFilePath}
driver-class-name: org.sqlite.JDBC
jpa:
database-platform: org.hibernate.community.dialect.SQLiteDialect
hibernate:
ddl-auto: update #当表不存在的时候进行创建
naming: #开启驼峰命令
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
show-sql: true
4)Dao层实现
注意:实体类可以通过Idea的持久化映射实现
a)创建Entity
/**
* @author zxl
*/
@Entity
@Table(name = "edge_camera", schema = "main")
@Data
public class CameraEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id", nullable = false)
private Long id;
/**
* 方向
*/
private Direction direction;
/**
* 租户id
*/
private Long tenantId;
/**
* 道路id
*/
private Long roadId;
/**
* @description Java Attribute variables
*/
@Basic
@Column(name = "name", nullable = true, length = -1)
private String name;
/**
* @description Java Attribute variables
*/
@Basic
@Column(name = "sn", nullable = true, length = -1)
private String sn;
/**
* @description Java Attribute variables
*/
@Basic
@Column(name = "username", nullable = true, length = -1)
private String username;
/**
* @description Java Attribute variables
*/
@Basic
@Column(name = "password", nullable = true, length = -1)
private String password;
/**
* @description Java Attribute variables
*/
@Basic
@Column(name = "ip", nullable = true, length = -1)
private String ip;
/**
* @description Java Attribute variables
*/
@Basic
@Column(name = "port", nullable = true)
private Integer port;
/**
* @description 摄像头类型
*/
@Basic
@Column(name = "vendor", nullable = true)
private Vendor vendor;
/**
* @description Java Attribute variables
*/
@Basic
@Column(name = "run_status", nullable = true)
private Integer runStatus;
}
b)实现Jpa接口
/**
* @author zxl
* @date 2024/12/20
*/
public interface CameraRepository extends JpaRepository<CameraEntity, Long> {
/**
* @param sn
* @return CameraEntity
*/
CameraEntity findBySn(String sn);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了