Code Monkey home page Code Monkey logo

spring-boot-jpa-student-mvc's Introduction

spring-boot-jpa-student-mvc

barebones spring boot CRUD application involving basic student data

setup

after downloading the repository I recommend opening it in your chosen development environment my development environment uses postman and intelliJ IDEA

postgresql db setup

this program uses postgres for data storage

creating the db

in postgres CLI run the following commands to set up the db

  • create database student

  • grant all privileges on database "student" to [username]

    where [username] is the user that is being used on postgres

  • \c student

    to connect to the student db

  • \d

    to check that the database is empty

setting up your application.properties file

in the repository under src/main/resources/application.properties. I have already setup the program to work with the student db. Although the username and password must be changed to contain the appropriate credentials based on the user you are logged in as in postgres.

those credentials will not work as the password is not going to be the same (the one in the file is a dummy password)

How it Works

Domain Class (Student.java)

This contains a student class the @Entity & @Table annotation indicates that the class can be mapped to a table

The id column being indicated by the @Id annotation which is configured with the @SquenceGenerator and @GeneratedValue annotations on the Id variable of type Long.

The @Transient annotation indicates the following variable to be stored in memory and not at a column in the table. In this case age is not stored but calculated based on the period of time between the dob and the current time.

Repository Interface (StudentRepository.java)

This indicates that the class being extended is capable of having storage, retrieval, search, update and delete operations performed. The interface method in this repository is what will allow use to manipulate or interact with the data in the database.

Service Class (Student.java)

This contains a majority of the logic behind the API endpoints and communicates between the repository interface and the controller class. The @Component annotation tells spring that only a single instance of the class will be made.

The @Autowired annotation indicates a dependency to the student repository that needs to be resolved before being used

The @Transactional annotation wraps the following method as a transaction that will be linked to the class that is being annotated.

Controller Class (StudentController.java)

This will be the class which will be handling requests to the server. This is indicated by the @RestController annotation, with the @RequestMapping annotation indicating what url path the requests are linked to.

The @Autowired annotaion here indicates that the controller has a dependency to the StudentService class.

Each of the annotated mapping methods take some http request with either a GET, POST, DELETE or PUT method.

GetMapping

Accepts GET requests to the server

PostMapping

Accepts POST requests to the server

DeleteMapping

Accepts DELETE requests to the server

PutMapping

Accepts PUT requests to the server

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.