Library Management System
Library Management System
Management
System
Problem Statement
The Library Managment System is a full-stack web application developed to simplify the management of
library eperations as managing books, authors, and borrower. It allows users to perform CRUD operations
and interaction through a clean, intuitive frontend powered by a Spring Boot RESTful backend.
Objectives
1.) Develop a robust and scalable library management application,
2) Allow users to manage (create, read, update, delete) books & borrower details efficiently.
package com.example.library;
import com.example.library.model.Book;
import com.example.library.service.BookService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
SpringApplication.run(LibraryApplication.class, args);
// This method will be called to initialize the database with sample books
@Bean
};
2.Package:com.example.library.controller
package com.example.library.controller;
import com.example.library.model.Book;
import com.example.library.service.BookService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/books")
@CrossOrigin
this.service = service;
@GetMapping
return service.getAllBooks();
@GetMapping("/{id}")
return service.getBookById(id);
@PostMapping
return service.addBook(book);
@DeleteMapping("/{id}")
@PutMapping("/{id}")
3.Package:com.example.library.model
package com.example.library.model;
public Book() {
this.title = title;
this.author = author;
this.id = id;
this.title = title;
this.author = author;
this.available = available;
}
public Long getId() { return id; }
4.package:com.example.library.service
import com.example.library.model.Book;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
return books.stream().findFirst();
jdbcTemplate.update("INSERT INTO book (title, author, available) VALUES (?, ?, ?)", book.getTitle(),
book.getAuthor(), book.isAvailable());
return book;
book.setId(id);
return book;
}
}
2.src/main/resources/static
1.index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Library Management</title>
</head>
<body>
<div class="container">
<h2>Books</h2>
<div class="form-container">
<form id="add-book-form">
</form>
</div>
<h3>Books in Library</h3>
<ul id="book-list"></ul>
</div>
<script src="script.js"></script>
</body>
</html>
2.styles.css
*{
margin: 0;
padding: 0;
box-sizing: border-box;
/* Container styles */
.container {
width: 80%;
margin: auto;
padding: 20px;
/* Title styles */
h1 {
text-align: center;
color: #333;
/* Form styles */
.form-container {
margin-bottom: 20px;
form input {
padding: 8px;
margin: 5px;
width: 200px;
font-size: 16px;
form button {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
form button:hover {
background-color: #45a049;
ul {
list-style-type: none;
padding: 0;
li {
padding: 10px;
background-color: #f9f9f9;
margin: 5px;
3.script.js
// API URLs
try {
displayBooks(books);
} catch (error) {
function displayBooks(books) {
bookList.innerHTML = '';
books.forEach(book => {
const li = document.createElement('li');
bookList.appendChild(li);
});
event.preventDefault();
try {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
});
if (response.ok) {
addBookForm.reset();
} else {
} catch (error) {
});
getBooks();
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Configuration
# H2 Database Configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
# JPA Settings
spring.jpa.database-platform=org.hibernate.H2DB
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# Enable H2 Console
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
Run Application
1. Build and run the project:via cmd or console window
./mvnw spring-boot:run
or using Gradle
./gradlew bootRun
2.Access H2 Console:
o URL: http://localhost:8080/h2-console
o Username: sa
/api/entities/
GET Get entity by ID
{id}
/api/entities/
PUT Update entity
{id}
/api/entities/
DELETE Delete entity
{id}
Sample output