sqlite part 1
This commit is contained in:
parent
7bbf5eb963
commit
26e9e2bfc0
|
@ -25,7 +25,6 @@ dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
implementation 'org.springframework.boot:spring-boot-devtools'
|
implementation 'org.springframework.boot:spring-boot-devtools'
|
||||||
implementation 'org.hsqldb:hsqldb'
|
|
||||||
implementation 'org.xerial:sqlite-jdbc:3.28.0'
|
implementation 'org.xerial:sqlite-jdbc:3.28.0'
|
||||||
testCompile("org.springframework.boot:spring-boot-starter-test")
|
testCompile("org.springframework.boot:spring-boot-starter-test")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,14 @@ package org.hso.ecommerce.app;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableJdbcRepositories
|
|
||||||
@ComponentScan(basePackages = {"org.hso.ecommerce"})
|
@ComponentScan(basePackages = {"org.hso.ecommerce"})
|
||||||
|
@EntityScan("org.hso.ecommerce")
|
||||||
|
@EnableJpaRepositories
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
|
|
|
@ -2,6 +2,7 @@ package org.hso.ecommerce.app;
|
||||||
|
|
||||||
import org.hso.ecommerce.db.CustomerRepository;
|
import org.hso.ecommerce.db.CustomerRepository;
|
||||||
import org.hso.ecommerce.entities.Customer;
|
import org.hso.ecommerce.entities.Customer;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
@ -19,6 +20,7 @@ public class RequestController {
|
||||||
|
|
||||||
private final CustomerRepository customerRepo;
|
private final CustomerRepository customerRepo;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
public RequestController(CustomerRepository customerRepo) {
|
public RequestController(CustomerRepository customerRepo) {
|
||||||
this.customerRepo = customerRepo;
|
this.customerRepo = customerRepo;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,37 +1,28 @@
|
||||||
package org.hso.ecommerce.db;
|
package org.hso.ecommerce.db;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
|
|
||||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableJdbcRepositories
|
@EnableJpaRepositories(basePackages = "org.hso.ecommerce.db")
|
||||||
public class CustomerConfig extends AbstractJdbcConfiguration {
|
public class CustomerConfig {
|
||||||
|
|
||||||
|
@Autowired Environment env;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() {
|
||||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||||
dataSource.setDriverClassName("org.sqlite.JDBC");
|
dataSource.setDriverClassName(env.getProperty("spring.datasource.driverClassName"));
|
||||||
dataSource.setUrl("jdbc:sqlite:mydb.db");
|
dataSource.setUrl(env.getProperty("spring.datasource.url"));
|
||||||
|
dataSource.setUsername(env.getProperty("spring.datasource.user"));
|
||||||
|
dataSource.setPassword(env.getProperty("spring.datasource.password"));
|
||||||
return dataSource;
|
return dataSource;
|
||||||
|
|
||||||
/* return new EmbeddedDatabaseBuilder()
|
|
||||||
.generateUniqueName(true)
|
|
||||||
.setType(EmbeddedDatabaseType.HSQL)
|
|
||||||
.addScript("classpath:db/customers.sql")
|
|
||||||
.build();*/
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
|
|
||||||
return new NamedParameterJdbcTemplate(dataSource);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,19 @@
|
||||||
package org.hso.ecommerce.db;
|
package org.hso.ecommerce.db;
|
||||||
|
|
||||||
import org.hso.ecommerce.entities.Customer;
|
import org.hso.ecommerce.entities.Customer;
|
||||||
import org.springframework.data.jdbc.repository.query.Query;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface CustomerRepository extends CrudRepository<Customer, Long> {
|
@Repository
|
||||||
|
public interface CustomerRepository extends JpaRepository<Customer, Long> {
|
||||||
|
|
||||||
@Query("SELECT * FROM customer WHERE lastName = :lastName")
|
@Query("SELECT c FROM Customer c WHERE c.lastname = :lastname")
|
||||||
List<Customer> findByLastname(String lastName);
|
List<Customer> findByLastname(String lastname);
|
||||||
|
|
||||||
@Query("SELECT * FROM customer WHERE username = :username")
|
@Query("SELECT c FROM Customer c WHERE c.username = :username")
|
||||||
List<Customer> findByUsername(String username);
|
List<Customer> findByUsername(String username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
package org.hso.ecommerce.db;
|
package org.hso.ecommerce.db;
|
||||||
|
|
||||||
import org.hibernate.dialect.Dialect;
|
|
||||||
|
|
||||||
import java.sql.Types;
|
import java.sql.Types;
|
||||||
|
|
||||||
|
import org.hibernate.dialect.Dialect;
|
||||||
|
import org.hibernate.dialect.function.SQLFunctionTemplate;
|
||||||
|
import org.hibernate.dialect.function.StandardSQLFunction;
|
||||||
|
import org.hibernate.dialect.function.VarArgsSQLFunction;
|
||||||
|
import org.hibernate.type.StringType;
|
||||||
|
|
||||||
public class SQLiteDialect extends Dialect {
|
public class SQLiteDialect extends Dialect {
|
||||||
|
|
||||||
public SQLiteDialect() {
|
public SQLiteDialect() {
|
||||||
|
@ -11,7 +15,140 @@ public class SQLiteDialect extends Dialect {
|
||||||
registerColumnType(Types.TINYINT, "tinyint");
|
registerColumnType(Types.TINYINT, "tinyint");
|
||||||
registerColumnType(Types.SMALLINT, "smallint");
|
registerColumnType(Types.SMALLINT, "smallint");
|
||||||
registerColumnType(Types.INTEGER, "integer");
|
registerColumnType(Types.INTEGER, "integer");
|
||||||
// other data types
|
registerColumnType(Types.BIGINT, "bigint");
|
||||||
|
registerColumnType(Types.FLOAT, "float");
|
||||||
|
registerColumnType(Types.REAL, "real");
|
||||||
|
registerColumnType(Types.DOUBLE, "double");
|
||||||
|
registerColumnType(Types.NUMERIC, "numeric");
|
||||||
|
registerColumnType(Types.DECIMAL, "decimal");
|
||||||
|
registerColumnType(Types.CHAR, "char");
|
||||||
|
registerColumnType(Types.VARCHAR, "varchar");
|
||||||
|
registerColumnType(Types.LONGVARCHAR, "longvarchar");
|
||||||
|
registerColumnType(Types.DATE, "date");
|
||||||
|
registerColumnType(Types.TIME, "time");
|
||||||
|
registerColumnType(Types.TIMESTAMP, "timestamp");
|
||||||
|
registerColumnType(Types.BINARY, "blob");
|
||||||
|
registerColumnType(Types.VARBINARY, "blob");
|
||||||
|
registerColumnType(Types.LONGVARBINARY, "blob");
|
||||||
|
// registerColumnType(Types.NULL, "null");
|
||||||
|
registerColumnType(Types.BLOB, "blob");
|
||||||
|
registerColumnType(Types.CLOB, "clob");
|
||||||
|
registerColumnType(Types.BOOLEAN, "integer");
|
||||||
|
|
||||||
|
registerFunction("concat", new VarArgsSQLFunction(StringType.INSTANCE, "", "||", ""));
|
||||||
|
registerFunction("mod", new SQLFunctionTemplate(StringType.INSTANCE, "?1 % ?2"));
|
||||||
|
registerFunction("substr", new StandardSQLFunction("substr", StringType.INSTANCE));
|
||||||
|
registerFunction("substring", new StandardSQLFunction("substr", StringType.INSTANCE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean supportsIdentityColumns() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
public boolean supportsInsertSelectIdentity() {
|
||||||
|
return true; // As specify in NHibernate dialect
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
public boolean hasDataTypeInIdentityColumn() {
|
||||||
|
return false; // As specify in NHibernate dialect
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
public String appendIdentitySelectToInsert(String insertString) {
|
||||||
|
return new StringBuffer(insertString.length()+30). // As specify in NHibernate dialect
|
||||||
|
append(insertString).
|
||||||
|
append("; ").append(getIdentitySelectString()).
|
||||||
|
toString();
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
public String getIdentityColumnString() {
|
||||||
|
// return "integer primary key autoincrement";
|
||||||
|
return "integer";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIdentitySelectString() {
|
||||||
|
return "select last_insert_rowid()";
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean supportsLimit() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getLimitString(String query, boolean hasOffset) {
|
||||||
|
return new StringBuffer(query.length() + 20).
|
||||||
|
append(query).
|
||||||
|
append(hasOffset ? " limit ? offset ?" : " limit ?").
|
||||||
|
toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean supportsTemporaryTables() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCreateTemporaryTableString() {
|
||||||
|
return "create temporary table if not exists";
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean dropTemporaryTableAfterUse() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean supportsCurrentTimestampSelection() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCurrentTimestampSelectStringCallable() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCurrentTimestampSelectString() {
|
||||||
|
return "select current_timestamp";
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean supportsUnionAll() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasAlterTable() {
|
||||||
|
return false; // As specify in NHibernate dialect
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean dropConstraints() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddColumnString() {
|
||||||
|
return "add column";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getForUpdateString() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean supportsOuterJoinForUpdate() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDropForeignKeyString() {
|
||||||
|
throw new UnsupportedOperationException("No drop foreign key syntax supported by SQLiteDialect");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddForeignKeyConstraintString(String constraintName,
|
||||||
|
String[] foreignKey, String referencedTable, String[] primaryKey,
|
||||||
|
boolean referencesPrimaryKey) {
|
||||||
|
throw new UnsupportedOperationException("No add foreign key syntax supported by SQLiteDialect");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddPrimaryKeyConstraintString(String constraintName) {
|
||||||
|
throw new UnsupportedOperationException("No add primary key syntax supported by SQLiteDialect");
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean supportsIfExistsBeforeTableName() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean supportsCascadeDelete() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
package org.hso.ecommerce.entities;
|
package org.hso.ecommerce.entities;
|
||||||
|
|
||||||
import org.springframework.data.annotation.Id;
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "customers")
|
||||||
public class Customer {
|
public class Customer {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
public Long id;
|
public Long id;
|
||||||
public String lastname;
|
public String lastname;
|
||||||
public String firstname;
|
public String firstname;
|
||||||
|
|
|
@ -6,10 +6,9 @@
|
||||||
logging.level.org.springframework.web=INFO
|
logging.level.org.springframework.web=INFO
|
||||||
|
|
||||||
# DATABASE
|
# DATABASE
|
||||||
spring.datasource.url=jdbc:HSQL
|
spring.datasource.url = jdbc:sqlite:src/main/resources/test.db
|
||||||
spring.datasource.username=
|
spring.datasource.driverClassName = org.sqlite.JDBC
|
||||||
spring.datasource.password=
|
spring.jpa.properties.hibernate.dialect = org.hso.ecommerce.db.SQLiteDialect
|
||||||
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
|
|
||||||
|
|
||||||
# ----------------------------------------
|
# ----------------------------------------
|
||||||
# WEB PROPERTIES
|
# WEB PROPERTIES
|
||||||
|
|
|
@ -1 +1,7 @@
|
||||||
CREATE TABLE IF NOT EXISTS customer (id INTEGER IDENTITY PRIMARY KEY, lastname VARCHAR(100), firstname VARCHAR(100), username VARCHAR(100), password VARCHAR(100));
|
CREATE TABLE "customers" (
|
||||||
|
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
"lastname" TEXT,
|
||||||
|
"firstname" TEXT,
|
||||||
|
"username" TEXT,
|
||||||
|
"password" TEXT
|
||||||
|
);
|
||||||
|
|
Binary file not shown.
Reference in New Issue