一、概述
SQLite 只需要使用单个db文件即可,不用建立数据库服务。非常简单易用,适用于小型单体项目。
菜鸟教程-SQLite 教程
https://www.runoob.com/sqlite/sqlite-tutorial.html
二、使用
使用Navicat
可以直接新建SQLite
数据库。
时间
要获取当前日期时间,您可以使用:
SELECT date(‘now’);
注意:这不是服务器日期,如果您直接从应用程序查询日期和时间,这是因为SQLITE在进程中运行.
如果您的语言日期处理非常差,那么将当前时间放入表格或进行一些简单的计算非常有用.
要进行计算,请参阅SQLITE文档
例如,请参阅文档以了解格式:
SELECT strftime(‘%Y-%m-%d %H:%M:%S’, date(‘now’))
select datetime(CURRENT_TIMESTAMP,‘localtime’);
获取当前时间(数据库所在时区)
三、集成 SpringBoot
1、项目案例
(1)application.yml
spring:
datasource:
driver-class-name: org.sqlite.JDBC
# 路径:db/mydb.bd
#url: jdbc:sqlite:db/mydb.db
# 路径:src/main/resources/db/mydb.db
#url: jdbc:sqlite::resource:db/mydb.db
url: jdbc:sqlite:D:/Program Files/sqlite3/mydb.db
username:
password:
mybatis:
mapper-locations:
- classpath:mapper/*.xml
(2)pom.xml
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.36.0.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
(3)student.xml
src/main/resources/mapper/student.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="student">
<resultMap id="BaseResultMap" type="java.util.HashMap"></resultMap>
<parameterMap id="BaseParameterMap" type="java.util.HashMap"></parameterMap>
<select id="findStudent" resultMap="BaseResultMap" parameterMap="BaseParameterMap">
select
"id",
"name"
from
student
where
id = #{id}
</select>
</mapper>
(4)测试代码
src/test/java/com/scy/sqlite/SqliteApplicationTests.java
package com.scy.sqlite;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
class SqliteApplicationTests {
@Resource
SqlSession sqlSession;
@Test
void contextLoads() {
Map map = new HashMap();
map.put("id", "2");
List<Object> studentList = sqlSession.selectList("student.findStudent", map);
System.out.println(studentList);
}
}