Self-learing project for Springboot for RESTful API with SQLite database
.mvn/wrapper | ||
data | ||
src | ||
.gitignore | ||
mvnw | ||
mvnw.cmd | ||
openapi.yml | ||
pom.xml | ||
README.md |
Project for learning Spring Boot, RESTful API, and SQLite
This is a self-learing project for Spring Boot for RESTful API with SQLite database
Environments
The following tools/libraries are used in this project:
- Microsoft VS Code
- OpenJDK 17
- VSCode extensions:
- Linux or Windows WSL2 with Bash or Zsh Shell
- Linux Package
sqlite3
Create the project from scratch
The following steps will create this project from scratch. Assuming the environment has been setup properly.
Create Spring Boot Project in VSCode
- Start VSCode (e.g. from CLI:
code .
) and open the project folder - Press
CTRL-SHIFT-P
and typeSpring Initializr
- Select
Spring Initializr: Create a Maven Project...
- Specify Spring Boot Version:
3.2.0
- Specify project language:
Java
- Input Group Id:
com.example
(or any other you like) - Input Artifact Id:
demo
(or any other you like, it will be the project folder too.) - Specify packaging type:
Jar
- Specify Java version:
17
- Choose dependencies:
- Spring Web
- Spring Boot DevTools
- MyBatis Framework
- Select the directory for file generation (NOTE: Project folder will be created under this folder)
- Specify Spring Boot Version:
- Now the project layout (needed files and folders) should be created.
- A dialogue will be prompted for opening the new project in VSCode. You can also Restart the VSCode under the generated project folder directly
Add Maven dependencies
- In VSCode Explorer, expand
Maven
panel and locate theDependencies
- Click the
+
button next to theDependencies
dropdown item - Type
sqlite-jdbc
, press ENTER, and findsqlite-jdbc org.xerial
, Select it - Type
gson
, press ENTER, and findgson com.google.code.gson
, Select it - New dependence will be downloaded and added
Update the Maven/Project information (Optional)
-
Open
poe.xml
-
Change the
<groupId>
,<artifactId>
,<name>
,<description>
for your desire -
Use VSCode to renew (refactor) the folder and file accordingly
- src |-main |- java/com/example/demo --> java/{other group}/{other id} |- DemoApplication.java -<RENAME>-> MainApplication.java (Note: VSCode will modify the java file accordingly)
Create/Update the folders or files
- data <NEW>
- src
|-main
|-java/com/example/demo (or other folder)
|- model <NEW>
|- ApiController.java <NEW>
|-resources
|- mapper <NEW>
|- mainapp.xml <NEW>
|- mybatis <NEW>
|- mybatis-config.xml <NEW>
Update/Create the files
Refer to the file in this project, update the following files accordingly:
- src/main/resources/mybatis/mybatis-config.xml
- src/main/resources/application.properties
- src/main/resources/mapper/mainapp.xml
- src/main/java/com/example/demo/ApiController.java
Init the database and insert sample data
Please see the README for Data folder file
Start Spring Boot Application
To start this Spring Boot Application:
- Open VSCode
- Select Spring Boot Dashboard (Left Toolbar)
- Rollover the APPS dropdown, click
|>>
button
TODO
- Resolve WARN "MyBatis: No MyBatis mapper was found in '[xx.mapper]' package. Please check your configuration."
- Why
application.properties
-spring.datasource.url
is requried even the mybatis config exist- what should be the correct value?
jdbc:sqlite:data
orjdbc:sqlite:data:abc
ok ,butjdbc:sqlite
not ok.
- what should be the correct value?
- In
src/main/java/com/example/demo/ApiController.java
- See the TODO
- Rewrite the controller code better