Add init db and sample data

This commit is contained in:
BadBUTA 2023-12-21 23:51:44 +08:00
parent acca73a548
commit 917941e2f2
4 changed files with 61 additions and 5 deletions

View File

@ -1,15 +1,66 @@
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import jakarta.annotation.PostConstruct;
@SpringBootApplication
@MapperScan("com.example.demo.repository")
public class MainApplication {
@Autowired
private DataSource dataSource;
public static void main(String[] 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);
}
}

View File

@ -4,7 +4,10 @@
spring.datasource.url=jdbc:sqlite:data/mainapp.sqlite3
# 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.hikari.maximum-pool-size=1
# spring.datasource.hikari.max-lifetime=0
spring.datasource.driver-class-name=org.sqlite.JDBC
# spring.datasource.username=
@ -13,6 +16,7 @@ spring.datasource.driver-class-name=org.sqlite.JDBC
## MyBatis related:
# mybatis.check-config-location=true
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:com/example/demo/repository/*.xml
## ----- Logger configuration -----
logging.level.org.mybatis=DEBUG

View File

@ -1,3 +1,4 @@
-- Insert the sample data for users table:
INSERT INTO users (name,password) VALUES ( 'admin', '1234' );
INSERT INTO users (name,password) VALUES ( 'badbuta', 'abcd' );
-- MD5 of '1234' = 81dc9bdb52d04dc20036dbd8313ed055
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');

View File

@ -1,6 +1,6 @@
-- Users table
CREATE TABLE users(
CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL,
password VARCHAR -- Clear-text pwd
name TEXT NOT NULL,
password TEXT -- Clear-text pwd
);