|
1 | | -# My-SpringBoot-MySQL-Application |
| 1 | +# My Spring Boot MySQL Application |
2 | 2 |
|
3 | | -# Read Me First |
4 | | -The following was discovered as part of building this project: |
| 3 | +This is an educational Spring Boot REST API for managing employees, |
| 4 | +departments, addresses, profiles, and simple login/logout state. The app uses |
| 5 | +Spring Web, Spring Data JPA, Bean Validation, and MySQL for local development. |
5 | 6 |
|
6 | | -* The original package name 'com.hemant.db.springboot-mysql' is invalid and this project uses 'com.hemant.db.springbootmysql' instead. |
| 7 | +## Tech Stack |
7 | 8 |
|
8 | | -# Getting Started |
| 9 | +- Java 11 |
| 10 | +- Spring Boot 2.7.x |
| 11 | +- Spring Web |
| 12 | +- Spring Data JPA |
| 13 | +- MySQL Connector/J |
| 14 | +- H2 for automated tests |
| 15 | +- Maven Wrapper |
9 | 16 |
|
10 | | -### Reference Documentation |
11 | | -For further reference, please consider the following sections: |
| 17 | +## Project Structure |
12 | 18 |
|
13 | | -* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) |
14 | | -* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.5.2/maven-plugin/reference/html/) |
15 | | -* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.5.2/maven-plugin/reference/html/#build-image) |
16 | | -* [Spring Web](https://docs.spring.io/spring-boot/docs/2.5.2/reference/htmlsingle/#boot-features-developing-web-applications) |
17 | | -* [Spring Data JPA](https://docs.spring.io/spring-boot/docs/2.5.2/reference/htmlsingle/#boot-features-jpa-and-spring-data) |
18 | | -* [JDBC API](https://docs.spring.io/spring-boot/docs/2.5.2/reference/htmlsingle/#boot-features-sql) |
| 19 | +```text |
| 20 | +. |
| 21 | +|-- pom.xml |
| 22 | +|-- mvnw / mvnw.cmd |
| 23 | +|-- src/ |
| 24 | +| |-- main/ |
| 25 | +| | |-- java/com/hemant/db/ |
| 26 | +| | | |-- SpringbootMysqlApplication.java |
| 27 | +| | | |-- exception/ |
| 28 | +| | | |-- model/ |
| 29 | +| | | |-- repository/ |
| 30 | +| | | |-- resource/ |
| 31 | +| | | `-- service/ |
| 32 | +| | `-- resources/application.yml |
| 33 | +| `-- test/ |
| 34 | +| |-- java/com/hemant/db/ |
| 35 | +| `-- resources/application-test.yml |
| 36 | +`-- .github/workflows/ci.yml |
| 37 | +``` |
19 | 38 |
|
20 | | -### Guides |
21 | | -The following guides illustrate how to use some features concretely: |
| 39 | +### Main Packages |
22 | 40 |
|
23 | | -* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/) |
24 | | -* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/) |
25 | | -* [Building REST services with Spring](https://spring.io/guides/tutorials/bookmarks/) |
26 | | -* [Accessing Data with JPA](https://spring.io/guides/gs/accessing-data-jpa/) |
27 | | -* [Accessing Relational Data using JDBC with Spring](https://spring.io/guides/gs/relational-data-access/) |
28 | | -* [Managing Transactions](https://spring.io/guides/gs/managing-transactions/) |
| 41 | +`model` contains the JPA entities that map to database tables. Examples: |
| 42 | +`Employee`, `Department`, `Address`, and `Profile`. |
| 43 | + |
| 44 | +`repository` contains Spring Data JPA repositories. These interfaces provide |
| 45 | +database access methods such as `findByEmail`, `findByCity`, and `findByName`. |
| 46 | + |
| 47 | +`service` contains business logic. Controllers call services instead of talking |
| 48 | +directly to repositories, which keeps request handling and application rules |
| 49 | +separate. |
| 50 | + |
| 51 | +`resource` contains REST controllers. These preserve the original `/rest/...` |
| 52 | +routes while delegating work to services. |
| 53 | + |
| 54 | +`exception` contains shared API error handling. Missing records, duplicate |
| 55 | +records, bad requests, and validation failures return consistent JSON error |
| 56 | +responses. |
| 57 | + |
| 58 | +## Configuration |
| 59 | + |
| 60 | +The app reads database settings from environment variables with local defaults: |
| 61 | + |
| 62 | +| Variable | Default | |
| 63 | +| --- | --- | |
| 64 | +| `MYSQL_URL` | `jdbc:mysql://localhost:3306/employee_db` | |
| 65 | +| `MYSQL_USERNAME` | `root` | |
| 66 | +| `MYSQL_PASSWORD` | empty | |
| 67 | +| `JPA_DDL_AUTO` | `update` | |
| 68 | +| `JPA_SHOW_SQL` | `true` | |
| 69 | + |
| 70 | +Example PowerShell setup: |
| 71 | + |
| 72 | +```powershell |
| 73 | +$env:MYSQL_URL="jdbc:mysql://localhost:3306/employee_db" |
| 74 | +$env:MYSQL_USERNAME="root" |
| 75 | +$env:MYSQL_PASSWORD="your-password" |
| 76 | +``` |
| 77 | + |
| 78 | +## Run Locally |
| 79 | + |
| 80 | +Start MySQL and create the database: |
| 81 | + |
| 82 | +```sql |
| 83 | +CREATE DATABASE employee_db; |
| 84 | +``` |
| 85 | + |
| 86 | +Then run the application: |
| 87 | + |
| 88 | +```powershell |
| 89 | +.\mvnw.cmd spring-boot:run |
| 90 | +``` |
| 91 | + |
| 92 | +On macOS/Linux: |
| 93 | + |
| 94 | +```bash |
| 95 | +./mvnw spring-boot:run |
| 96 | +``` |
| 97 | + |
| 98 | +The API starts on the default Spring Boot port: |
| 99 | + |
| 100 | +```text |
| 101 | +http://localhost:8080 |
| 102 | +``` |
| 103 | + |
| 104 | +## API Overview |
| 105 | + |
| 106 | +The existing route style is preserved. |
| 107 | + |
| 108 | +### Employees |
| 109 | + |
| 110 | +- `GET /rest/Employee/all` |
| 111 | +- `POST /rest/Employee/insert` |
| 112 | +- `GET /rest/Employee/findbyname?name=...` |
| 113 | +- `GET /rest/Employee/findbydesignation?designation=...` |
| 114 | +- `POST /rest/Employee/updatedesignation?Id=...&designation=...` |
| 115 | +- `POST /rest/Employee/updatemobile?Id=...&mobile=...` |
| 116 | +- `POST /rest/Employee/updatepassword?Id=...&password=...` |
| 117 | +- `DELETE /rest/Employee/delete?Id=...` |
| 118 | + |
| 119 | +### Departments |
| 120 | + |
| 121 | +- `GET /rest/Department/all` |
| 122 | +- `POST /rest/Department/insert` |
| 123 | +- `GET /rest/Department/findbyname?name=...` |
| 124 | +- `GET /rest/Department/findbyaddress?address=...` |
| 125 | +- `POST /rest/Department/updatefloor?Id=...&floor=...` |
| 126 | +- `POST /rest/Department/updateaddress?Id=...&address=...` |
| 127 | +- `DELETE /rest/Department/delete?Id=...` |
| 128 | + |
| 129 | +### Addresses |
| 130 | + |
| 131 | +- `GET /rest/Address/all` |
| 132 | +- `POST /rest/Address/insert` |
| 133 | +- `GET /rest/Address/findbycity?city=...` |
| 134 | +- `GET /rest/Address/findbystate?state=...` |
| 135 | +- `POST /rest/Address/updateaddress?...` |
| 136 | +- `DELETE /rest/Address/delete?Id=...` |
| 137 | + |
| 138 | +### Profiles |
| 139 | + |
| 140 | +- `GET /rest/Profile/all` |
| 141 | +- `POST /rest/Profile/insert` |
| 142 | +- `GET /rest/Profile/findbygender?gender=...` |
| 143 | +- `GET /rest/Profile/findbyhobbies?hobbies=...` |
| 144 | +- `POST /rest/Profile/updatehobbies?Id=...&hobbies=...` |
| 145 | +- `DELETE /rest/Profile/delete?Id=...` |
| 146 | + |
| 147 | +### Login |
| 148 | + |
| 149 | +- `GET /rest/Login/findbyemail?email=...` |
| 150 | +- `POST /rest/Login/login?email=...&password=...` |
| 151 | +- `POST /rest/Login/logout?email=...` |
| 152 | + |
| 153 | +## Testing |
| 154 | + |
| 155 | +Tests use H2 in MySQL compatibility mode, so they do not require a local MySQL |
| 156 | +server. |
| 157 | + |
| 158 | +```powershell |
| 159 | +.\mvnw.cmd test |
| 160 | +``` |
| 161 | + |
| 162 | +On macOS/Linux: |
| 163 | + |
| 164 | +```bash |
| 165 | +./mvnw test |
| 166 | +``` |
| 167 | + |
| 168 | +The GitHub Actions workflow also runs the Maven test suite on Java 11 for pushes |
| 169 | +to `main` and pull requests. |
| 170 | + |
| 171 | +## Notes |
| 172 | + |
| 173 | +- Passwords are accepted for create/update/login operations but are hidden from |
| 174 | + JSON responses. |
| 175 | +- This project intentionally keeps the original route naming style to avoid |
| 176 | + breaking existing clients. |
| 177 | +- Authentication is educational only. It stores a login status and plain |
| 178 | + password values, so it should not be used as-is for production security. |
0 commit comments