komamitsu / spring-data-sqlite

Apache License 2.0
16 stars 3 forks source link

Spring Data JDBC integration for SQLite

This is a Spring Data JDBC integration for SQLite. The basic usage follows Spring Data JDBC - Reference Documentation.

Install

For Gradle

    implementation "org.komamitsu:spring-data-sqlite:1.3.0"

For Maven

<dependency>
    <groupId>org.komamitsu</groupId>
    <artifactId>spring-data-sqlite</artifactId>
    <version>1.3.0</version>
</dependency>

JVM options for Spring Boot 3

When using Spring Data JDBC for SQLite with Spring Boot 3, -parameters JVM option needs to be added. See https://stackoverflow.com/questions/78374721/spring-data-jdbc-repository-bean-creation-error/78376635#comment138178415_78376635 for detail.

Configurations

spring.datasource.url

For instance,

spring.datasource.url=jdbc:sqlite:our-app.db

Annotations

@EnableSqliteRepositories annotation is needed on the JVM application to use this integration as follows

@SpringBootApplication
@EnableSqliteRepositories
public class MyApplication {
  @Autowired GroupRepository groupRepository;
  @Autowired UserRepository userRepository;

  :

Repository class

This library provides SqliteRepository that inherits PagingAndSortingRepository and has 2 new APIs insert() and update(). Spring Data JDBC basically requires users to use auto generated ID columns. If users want to use non auto generated ID column, there are some workarounds, but they're not perfect solutions. The 2 new APIs addresses the problem.

public interface CarRepository extends SqliteRepository<Car, Integer> {}
    Car car = new Car(assignedCarId, "my new car");
    carRepository.insert(car);
       :
    carRepository.update(modifiedCar);

Example application

Example application is a JVM application that uses this integration. It only serves as a reference and does not necessarily meet production code standards.