参考:https://www.baeldung.com/spring-boot-sqlite

1.方言问题

报错:Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

See, Hibernate doesn’t ship with a Dialect for SQLite. We need to create one ourselves.

或者参考另外一篇文章:

https://stackoverflow.com/questions/46990525/problems-with-dialect-sqlite-3-with-hibernate-5

有写好的方言(我用的这种)

<!-- https://mvnrepository.com/artifact/com.zsoltfabok/sqlite-dialect -->
<dependency>
    <groupId>com.zsoltfabok</groupId>
    <artifactId>sqlite-dialect</artifactId>
    <version>1.0</version>
</dependency>

2.路径 问题:

读取对应目录文件:  jdbc:sqlite:F:/test1.db

3.加密的问题:

没解决。改为不加密了。。

4.动态修改数据源的问题。使用的是spring boot ,spring data jpa

sqlite的路径动态改变,通过接口传入,去解析不同sqlite文件里的数据

@Configuration
public class DataSourceAutoConfiguration {@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("org.sqlite.JDBC");dataSource.setUrl("jdbc:sqlite:d:/xxx.db");dataSource.setUsername("");dataSource.setPassword("");return dataSource;}
}
控制器类中:
@Autowired
private DriverManagerDataSource dataSource;
控制器方法中动态改变
dataSource.setUrl("jdbc:sqlite:d:/xxx.db");

5.sqlite插入guid

lower(hex(randomblob(16)))

6.sqlite boolean类型的问题

我存了默认值 true,修改其中两个值为1,然后又修改默认值为0  结果发现SQLite Expert Professional 和Database .NET 17.0.5798_3@87104 这两个工具查看结果不一样,

用原始的sqlite3 exe 查看发现里面有的数据是0,有的是true.

个人推测:SQLite Expert  把true当做了 1 就是真。而 database .net 把true当做字符串 "true" 作为了假
所以显示不一样。

所以直接存0,1 就可以了,两边工具查看都是一致的。不要用true这种可能被当做字符串

原文如下:

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we’ll go through steps to use an SQLite database in a JPA-enabled Spring Boot application.

Spring Boot supports a few well known in-memory databases out of the box, but SQLite requires a bit more from us.

Let’s have a look at what it takes.

2. Project Setup

For our illustration, we’ll start with a Spring Data Rest app we’ve used in past tutorials.

In the pom, we need to add the sqllite-jdbc dependency:

1

2

3

4

5

<dependency>

    <groupId>org.xerial</groupId>

    <artifactId>sqlite-jdbc</artifactId>

    <version>3.25.2</version>

</dependency>

This dependency gives us what we need to use JDBC to communicate with SQLite. But, if we are going to use an ORM, it’s not enough.

3. SQLite Dialect

See, Hibernate doesn’t ship with a Dialect for SQLite. We need to create one ourselves.

3.1. Extending Dialect

Our first step is to extend org.hibernate.dialect.Dialect class to register the data types provided by SQLite:

1

2

3

4

5

6

7

8

9

10

public class SQLiteDialect extends Dialect {

    public SQLiteDialect() {

        registerColumnType(Types.BIT, "integer");

        registerColumnType(Types.TINYINT, "tinyint");

        registerColumnType(Types.SMALLINT, "smallint");

        registerColumnType(Types.INTEGER, "integer");

        // other data types

    }

}

There are several, so definitely check out the sample code for the rest.

Next, we’ll need to override some default Dialect behaviors.

3.2. Identity Column Support

For example, we need to tell Hibernate how SQLite handles @Id columns, which we can do with a custom IdentityColumnSupport implementation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

public class SQLiteIdentityColumnSupport extends IdentityColumnSupportImpl {

    @Override

    public boolean supportsIdentityColumns() {

        return true;

    }

    @Override

    public String getIdentitySelectString(String table, String column, int type)

      throws MappingException {

        return "select last_insert_rowid()";

    }

    @Override

    public String getIdentityColumnString(int type) throws MappingException {

        return "integer";

    }

}

To keep things simple here, let’s keep the identity column type to Integer only. And to get the next available identity value, we’ll specify the appropriate mechanism.

Then, we simply override the corresponding method in our growing SQLiteDialect class:

1

2

3

4

@Override

public IdentityColumnSupport getIdentityColumnSupport() {

    return new SQLiteIdentityColumnSupport();

}

3.3. Disable Constraints Handling

And, SQLite doesn’t have support for the database constraints, so we’ll need to disable those by again overriding the appropriate methods for both primary and foreign keys:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

@Override

public boolean hasAlterTable() {

    return false;

}

@Override

public boolean dropConstraints() {

    return false;

}

@Override

public String getDropForeignKeyString() {

    return "";

}

@Override

public String getAddForeignKeyConstraintString(String cn,

  String[] fk, String t, String[] pk, boolean rpk) {

    return "";

}

@Override

public String getAddPrimaryKeyConstraintString(String constraintName) {

    return "";

}

And, in just a moment, we’ll be able to reference this new dialect in our Spring Boot configuration.

4. DataSource Configuration

Also, since Spring Boot doesn’t provide configuration support for SQLite database out of the box, we also need to expose our own DataSource bean:

1

2

3

4

5

6

7

8

9

10

11

@Autowired Environment env;

@Bean

public DataSource dataSource() {

    final DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName(env.getProperty("driverClassName"));

    dataSource.setUrl(env.getProperty("url"));

    dataSource.setUsername(env.getProperty("user"));

    dataSource.setPassword(env.getProperty("password"));

    return dataSource;

}

And finally, we’ll configure the following properties in our persistence.properties file:

1

2

3

4

5

6

7

driverClassName=org.sqlite.JDBC

url=jdbc:sqlite:memory:myDb?cache=shared

username=sa

password=sa

hibernate.dialect=com.baeldung.dialect.SQLiteDialect

hibernate.hbm2ddl.auto=create-drop

hibernate.show_sql=true

Note that we need to keep the cache as shared in order to keep the database updates visible across multiple database connections.

So, with the above configurations, the app will start and will launch an in-memory database called myDb, which the remaining Spring Data Rest configuration can take up.

5. Conclusion

In this article, we took a sample Spring Data Rest application and pointed it at an SQLite database. However, to do so, we had to create a custom Hibernate dialect.

Make sure to check out the application over on Github. Just run with mvn -Dspring.profiles.active=sqlite spring-boot:run and browse to http://localhost:8080.

springboot 连接sqlite相关推荐

  1. SpringBoot 连接mysql踩到的坑

    首先对于用SpringBoot连接mysql我先说明一下pom文件中需要引入那些jar: <dependency><groupId>mysql</groupId>& ...

  2. 【Laravel】连接sqlite,Database [] not configured,sqlite example

    .env.local 文件 SQLITE_DB_CONNECTION=sqlite VIRSH_DB_DATABASE=virsh.sqlite 在 database 文件夹下 创建 touch vi ...

  3. C#连接Sqlite 出现:混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。的解决方案...

    C#连接Sqlite 出现: 混合模式程序集是针对"v2.0.50727"版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集.的解决方案 C#连接s ...

  4. SpringBoot连接多RabbitMQ源

    转自: SpringBoot连接多RabbitMQ源 - 掘金在实际开发中,很多场景需要异步处理,这时就需要用到RabbitMQ,而且随着场景的增多程序可能需要连接多个RabbitMQ.SpringB ...

  5. python连接sqlite数据库的代码_Python3实现连接SQLite数据库的方法

    本文实例讲述了Python3实现连接SQLite数据库的方法,对于Python的学习有不错的参考借鉴价值.分享给大家供大家参考之用.具体方法如下: 实例代码如下: import sqlite3 db ...

  6. power bi连接mysql_一起学微软Power BI系列-使用技巧(6) 连接Sqlite数据库

    阅读目录1.ODBC驱动 2.Power BI Desktop连接Sqlite 3.资源 好久没有研究Power BI了,看到高飞大神弄的东西,太惭愧了.今天有个小东西,数据在Sqlite里面,想倒腾 ...

  7. FIREDAC连接SQLITE乱码的解决

    在好多群里面都碰到问"FIREDAC连接SQLITE乱码的"的问题的同仁,遂将解决方法贴出来: 如上图所示设置 stringFormat为unicode即可 转载于:https:/ ...

  8. Spring Boot学习总结(28)—— springboot连接postgresql 指定模式Schema

    springboot 连接 postgresql 指定模式Schema 一般的连接方式,我们创建数据库之后,在public 的Schema(模式)下建表,这时使用连接方式 jdbc:postgresq ...

  9. SpringBoot连接Redis服务出现DENIED Redis is running in protected mode because protected mode is enabled

    问题描述:SpringBoot连接Redis服务出现DENIED Redis is running in protected mode because protected mode is enable ...

最新文章

  1. 广东海洋大学数学与计算机学院校友会,数学与计算机学院召开2020级研究生入学教育会...
  2. 笔记本电脑处理器_英特尔发布第九代酷睿移动处理器:笔记本电脑进入8核5.0GHz时代!...
  3. 阿里巴巴:全链路压测体系建设方案的思考与实践
  4. linux简单几个小命令
  5. 操作系统之内存管理:4、基本地址变换机构(段氏、页式、段页式)
  6. Discuz素材资源下载官网门户+自带论坛 整站源码+带后台+带数据库
  7. python global nonlocal
  8. mysql_query 资源标识符_PHP mysql_query() 函数解析
  9. 文件服务器报告,文件服务器报告
  10. 第一章 数学建模与误差分析
  11. oracle所有自带系统表,oracle常用系统表
  12. java 播放h264_一个可以解码并实时播放H264的播放器
  13. python获取outlook邮件内容_Python3读取Outlook邮件并写入MySQL
  14. 全国计算机等级考试怎么分级,【海贝推荐】全国计算机等级考试分级介绍
  15. (转)无法启动服务,原因可能是已被禁用或与其相关联的设备没有启动
  16. Linux系统中的内存划分-- VM split
  17. EEPROM 24C02 24C64误换Debug
  18. R语言中的多项式回归、局部回归、核平滑和平滑样条回归模型
  19. 5000比特量子计算机,量子计算机平台正式发布:拥有5000量子比特
  20. Testin云测试:联想K900热卖 完美兼容10000款主流App

热门文章

  1. Python全栈工程师特训班-第一期直播回放-韦玮-专题视频课程
  2. 华为云桌面简介和使用流程
  3. FTP-服务器搭建及使用
  4. Windows下的Java开发环境搭建+常用软件(含下载传送门)
  5. Dropper v 2.0 by Gergely Kutenich
  6. 微前端wujie的使用与nginx部署整理
  7. 知识点扫盲区:JDK是什么意思?什么是JRE?
  8. fatal: unable to access: OpenSSL SSL_read: Connection was rese, errno 10054
  9. java获取系统时间的几种方法_获取当前时间的几种方法整理(Java)
  10. Oracle中小数点前0不显示