当前位置: 首页 > news >正文

SQLite使用入门

SQLite数据库概述

在某些场景下使用SQLite文件数据库替代MySQL数据库是非常值得的,比如存储的数据量可控,业务查询简单。
相比起MySQL,PostgreSQL这样的大型数据库,SQLite文件数据库仅支持有限的数据类型;SQLite支持的SQL语法与MySQL/PostreSQL也有不尽相同,关于SQLite的介绍参考SQLite 教程。

在Spring Boot框架中使用SQLite数据库

基础配置

组件依赖:

<!-- 使用JDBC访问 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId>
</dependency><!-- SQLite JDBC驱动 -->
<dependency><groupId>org.xerial</groupId><artifactId>sqlite-jdbc</artifactId><version>3.44.1.0</version>
</dependency><!-- 集成MyBatis -->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${version.mybatis.starter}</version>
</dependency>

数据源配置:

@Configuration
public class DataSourceConfig {@Beanpublic DataSource dataSource() {SQLiteDataSource dataSource = new SQLiteDataSource();// 指定SQLite数据库文件地址dataSource.setUrl("jdbc:sqlite:C:\\Users\\test\\sample.db");return dataSource;}
}

MyBatis配置:

@Configuration
@MapperScan("com.sample.mapper")
public class MyBatisConfig {@Beanpublic SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(dataSource);// 设置MyBatis配置org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();configuration.setMapUnderscoreToCamelCase(true);configuration.getTypeAliasRegistry().registerAliases("com.sample.po");configuration.getTypeHandlerRegistry().register("com.sample.handler");// 设置方言configuration.setDatabaseId("sqlite");sessionFactory.setConfiguration(configuration);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));return sessionFactory.getObject();}
}

SQLite本身不支持Boolean类型,如果数据实体中存在Boolean类型属性,需要进行类型转换,可以配置全局类型处理器:

package com.sample.handler;import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;@MappedTypes(Boolean.class)
@MappedJdbcTypes(JdbcType.INTEGER)
public class BooleanTypeHandler extends BaseTypeHandler<Boolean> {@Overridepublic void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType) throws SQLException {ps.setInt(i, parameter ? 1 : 0);}@Overridepublic Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {int value = rs.getInt(columnName);return rs.wasNull() ? null : value == 1;}@Overridepublic Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {int value = rs.getInt(columnIndex);return rs.wasNull() ? null : value == 1;}@Overridepublic Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {int value = cs.getInt(columnIndex);return cs.wasNull() ? null : value == 1;}
}

Spring Boot应用配置配置:

## 应用启动时初始化数据库表
spring.datasource.platform=sqlite
spring.datasource.initialization-mode=always
spring.datasource.schema=classpath:sql/schema.sql## 集成MyBatis
## 检查MyBatis的配置文件
mybatis.check-config-location=true
## 指定MyBatis配置文件路径
mybatis.config-location=classpath:/mybatis-config.xml
## 注册XML映射器
mybatis.mapper-locations=classpath:/mapper/**/*.xml
## 指定POJO别名设置所在包
mybatis.type-aliases-package=com.sample.po
## 指定Java类型处理器所在包
mybatis.type-handlers-package=com.sample.handler
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.executor-type=simple

数据表DDL

将如下创建数据库表的DDL语句保存到classpath:sql/schema.sql文件中。

CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT, -- 主键acct TEXT NOT NULL,                   -- 账号名称age INTEGER NOT NULL,                 -- 年龄addr TEXT NOT NULL,                   -- 地址ctime INTEGER NOT NULL,               -- 创建时间戳mtime INTEGER NOT NULL                -- 修改时间戳
);

完成上述准备工作以后,就可以像使用MySQL那样通过MyBatis操作数据库表了。

SQLite访问工具

推荐使用开源,跨平台的SQLite数据库管理工具:SQLiteStudio。

【参考】
SQLite 教程
关于SQLite的注释

http://www.hskmm.com/?act=detail&tid=33724

相关文章:

  • 数论-supergcd
  • Layui框架使用入门
  • The 2024 ICPC Asia Hangzhou Regional Contest
  • 手机也能用的在线p图网站,大图轻松处理
  • Spring Boot框架常见问题
  • C# - Socket 基础指南
  • XSS检测绕过(UTF-7编码绕过)
  • Java平台的SQL监控组件
  • 2025 年东莞网络公司推荐,东莞市正度网络科技有限公司提供企业网络营销全流程适宜落地方案
  • 2025 年无锡短视频拍摄公司推荐:宜兴企拓网络,提供新媒体营销与短视频全流程解决方案
  • 2025 年中心供氧系统厂家推荐:山东恒大医用设备工程有限公司,提供医疗工程一体化解决方案
  • CF2135 C. By the Assignment
  • 2025 年防爆冰箱厂家推荐:浙江其春电气技术解析,防爆冰箱 / 冷柜 / 空调专业解决方案与应用实践
  • 2025 年互联网推广公司推荐:北京蓝海引擎科技,为中小企业提供智能化数字营销解决方案
  • Android 网络请求:多功能网络请求库
  • 触想参与国家标准起草,助力行业规范化发展
  • 349. 两个数组的交集
  • F5 BIG-IP 16.1.6.1 - 多云安全和应用交付
  • 2025 年最新推荐!污水处理设备优质厂家排行榜,帮企业避开劣质产品选到高效靠谱设备
  • Burp Suite Professional 2025.10 发布 - Web 应用安全、测试和扫描
  • 2025 年最新推荐真空炉制造厂家榜单:覆盖高温烧结 / 真空退火 / 智能铍铜炉,助力企业精准选型
  • 数论——CF757B Bashs Big Day
  • F5 安全事件:BIG-IP 源代码被窃取
  • F5 BIG-IP 15.1.10.8 - 领先的应用交付与安全服务
  • 2025 测量仪器厂家最新推荐榜单:国产新锐与领军品牌深度解析,精准匹配工业科研需求
  • Ant Design:企业级 UI 设计语言与 React 组件库
  • 2025 年最新推荐钢套钢保温钢管源头厂家榜:聚焦品质与实力,精选优质厂家助力采购决策
  • 2025年10月市场地位认证机构推荐榜:尚普与华信人深度对比评测
  • 2025年10月智能体公司推荐榜单:五强对比与中立评测助您精准选型
  • XPath索引定位深度解析://X[n]与(//X)[n]的本质区别