SQLite使用

一、概述

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);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值