Add init db and sample data
This commit is contained in:
parent
acca73a548
commit
917941e2f2
@ -1,15 +1,66 @@
|
|||||||
package com.example.demo;
|
package com.example.demo;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.sql.Connection;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.apache.ibatis.io.Resources;
|
||||||
|
import org.apache.ibatis.jdbc.ScriptRunner;
|
||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
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.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@MapperScan("com.example.demo.repository")
|
@MapperScan("com.example.demo.repository")
|
||||||
public class MainApplication {
|
public class MainApplication {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(MainApplication.class, args);
|
SpringApplication.run(MainApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void runSqlScript() throws Exception {
|
||||||
|
// It is not working in in-memory SQLite
|
||||||
|
this.initDatabaseAndInsertSampleData();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
|
public void doSomethingAfterStartup() {
|
||||||
|
System.out.println("hello world, I have just started up");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initDatabaseAndInsertSampleData() throws Exception {
|
||||||
|
|
||||||
|
// Database initialization
|
||||||
|
Connection conn = dataSource.getConnection();
|
||||||
|
ScriptRunner runner = new ScriptRunner(conn);
|
||||||
|
runner.setStopOnError(true);
|
||||||
|
runner.setEscapeProcessing(false); // Important!
|
||||||
|
// runner.setSendFullScript(false);
|
||||||
|
runner.setAutoCommit(true);
|
||||||
|
|
||||||
|
// Init schemas
|
||||||
|
Reader schema = Resources.getResourceAsReader("sqlite/schema.sql");
|
||||||
|
runner.runScript(schema);
|
||||||
|
|
||||||
|
// Insert sample data
|
||||||
|
Reader sampledata = Resources.getResourceAsReader("sqlite/create-sample-data.sql");
|
||||||
|
runner.runScript(sampledata);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,10 @@
|
|||||||
spring.datasource.url=jdbc:sqlite:data/mainapp.sqlite3
|
spring.datasource.url=jdbc:sqlite:data/mainapp.sqlite3
|
||||||
|
|
||||||
# Datasource URL used by @Repository, the db use in-memory
|
# Datasource URL used by @Repository, the db use in-memory
|
||||||
|
# NOTE: Not working, have no idea why
|
||||||
# spring.datasource.url=jdbc:sqlite::memory:
|
# spring.datasource.url=jdbc:sqlite::memory:
|
||||||
|
# spring.datasource.hikari.maximum-pool-size=1
|
||||||
|
# spring.datasource.hikari.max-lifetime=0
|
||||||
|
|
||||||
spring.datasource.driver-class-name=org.sqlite.JDBC
|
spring.datasource.driver-class-name=org.sqlite.JDBC
|
||||||
# spring.datasource.username=
|
# spring.datasource.username=
|
||||||
@ -13,6 +16,7 @@ spring.datasource.driver-class-name=org.sqlite.JDBC
|
|||||||
## MyBatis related:
|
## MyBatis related:
|
||||||
# mybatis.check-config-location=true
|
# mybatis.check-config-location=true
|
||||||
mybatis.config-location=classpath:mybatis/mybatis-config.xml
|
mybatis.config-location=classpath:mybatis/mybatis-config.xml
|
||||||
|
mybatis.mapper-locations=classpath:com/example/demo/repository/*.xml
|
||||||
|
|
||||||
## ----- Logger configuration -----
|
## ----- Logger configuration -----
|
||||||
logging.level.org.mybatis=DEBUG
|
logging.level.org.mybatis=DEBUG
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
-- Insert the sample data for users table:
|
-- Insert the sample data for users table:
|
||||||
INSERT INTO users (name,password) VALUES ( 'admin', '1234' );
|
-- MD5 of '1234' = 81dc9bdb52d04dc20036dbd8313ed055
|
||||||
INSERT INTO users (name,password) VALUES ( 'badbuta', 'abcd' );
|
INSERT INTO users (name,password) SELECT 'admin', '81dc9bdb52d04dc20036dbd8313ed055' WHERE NOT EXISTS(SELECT 1 FROM users WHERE name = 'admin');
|
||||||
|
INSERT INTO users (name,password) SELECT 'tester', '81dc9bdb52d04dc20036dbd8313ed055' WHERE NOT EXISTS(SELECT 1 FROM users WHERE name = 'tester');
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
-- Users table
|
-- Users table
|
||||||
CREATE TABLE users(
|
CREATE TABLE IF NOT EXISTS users(
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
name VARCHAR NOT NULL,
|
name TEXT NOT NULL,
|
||||||
password VARCHAR -- Clear-text pwd
|
password TEXT -- Clear-text pwd
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user