learn-springboot-restapi-sq.../README.md
2023-12-21 19:05:45 +08:00

116 lines
5.2 KiB
Markdown

# 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:
1. Microsoft VS Code
1. OpenJDK 17
1. VSCode extensions:
1. [Debugger for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-debug)
1. [Extension Pack for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack)
1. [Java Code Generators](https://marketplace.visualstudio.com/items?itemName=sohibe.java-generate-setters-getters)
1. [Language Support for Java(TM) by Red Hat](https://marketplace.visualstudio.com/items?itemName=redhat.java)
1. [Maven for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven)
1. [Project Manager for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-dependency)
1. [Spring Boot Dashboard](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-spring-boot-dashboard)
1. [Spring Boot Extension Pack](https://marketplace.visualstudio.com/items?itemName=vmware.vscode-boot-dev-pack)
1. [Spring Boot Tools](https://marketplace.visualstudio.com/items?itemName=vmware.vscode-spring-boot)
1. [Spring Initializr Java Support](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-spring-initializr)
1. [SQLite](https://marketplace.visualstudio.com/items?itemName=alexcvzz.vscode-sqlite) (Optional)
1. [Test Runner for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-test) (Optional)
1. Linux or Windows WSL2 with Bash or Zsh Shell
1. Linux Package
1. `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
1. Start VSCode (e.g. from CLI: `code .`) and open the project folder
1. Press `CTRL-SHIFT-P` and type `Spring Initializr`
1. Select `Spring Initializr: Create a Maven Project...`
1. Specify Spring Boot Version: `3.2.0`
1. Specify project language: `Java`
1. Input Group Id: `com.example` (or any other you like)
1. Input Artifact Id: `demo` (or any other you like, it will be the project folder too.)
1. Specify packaging type: `Jar`
1. Specify Java version: `17`
1. Choose dependencies:
1. Spring Web
1. Spring Boot DevTools
1. MyBatis Framework
1. Select the directory for file generation (__NOTE__: Project folder will be created under this folder)
1. Now the project layout (needed files and folders) should be created.
1. 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
1. In VSCode Explorer, expand `Maven` panel and locate the `Dependencies`
1. Click the `+` button next to the `Dependencies` dropdown item
1. Type `sqlite-jdbc`, press ENTER, and find `sqlite-jdbc org.xerial`, Select it
1. Type `gson`, press ENTER, and find `gson com.google.code.gson`, Select it
1. New dependence will be downloaded and added
## Update the Maven/Project information (Optional)
1. Open `poe.xml`
1. Change the `<groupId>`,`<artifactId>`,`<name>`,`<description>` for your desire
1. 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/mybatis/mybatis-config.xml)
- [src/main/resources/application.properties](src/main/resources/application.properties)
- [src/main/resources/mapper/mainapp.xml](src/main/resources/mapper/mainapp.xml)
- [src/main/java/com/example/demo/ApiController.java](src/main/java/com/example/demo/ApiController.java)
# Init the database and insert sample data
Please see the [README for Data folder](data/README.md) file
# Start Spring Boot Application
To start this Spring Boot Application:
1. Open VSCode
1. Select Spring Boot Dashboard (Left Toolbar)
1. Rollover the APPS dropdown, click `|>>` button
# TODO
1. Resolve WARN "MyBatis: No MyBatis mapper was found in '[xx.mapper]' package. Please check your configuration."
1. Why `application.properties` - `spring.datasource.url` is requried even the mybatis config exist
1. what should be the correct value? `jdbc:sqlite:data` or `jdbc:sqlite:data:abc` ok ,but `jdbc:sqlite` not ok.
1. In `src/main/java/com/example/demo/ApiController.java`
1. See the TODO
1. Rewrite the controller code better