Basic application to insert data into database
Spring Boot CRUD Application
Overview
This is a simple Spring Boot CRUD application that demonstrates how to create, read, update, and delete data in a MySQL database.
Database Details
The application uses the following MySQL database details:
- Database Name:
registrationDb - Table Name:
registration
Table Structure
The registration table has the following structure:
CREATE TABLE registration.sql
CREATE TABLE registration (
id INT AUTO_INCREMENT,
name VARCHAR(255),
email VARCHAR(255),
PRIMARY KEY (id)
);
How it Works
The application works as follows:
- The user sends an HTTP request to the
UserControllerclass. - The
UserControllerclass calls theRegistrationServiceclass to perform the business logic. - The
RegistrationServiceclass calls theRegistrationDaoclass to perform the data access logic. - The
RegistrationDaoclass interacts with the MySQL database to perform the CRUD operations.
Application Flow
UserController -> RegistrationService -> RegistrationDao -> MySQL Database
Code Snippets
Here are some code snippets that demonstrate how the application works:
UserController.java
@RestController
@RequestMapping("/v1")
public class UserController {
@Autowired
private RegistrationService registrationService;
@PostMapping("/create")
public String create(@RequestBody RegistrationFormDto registration) {
return registrationService.create(registration);
}
}
RegistrationService.java
public interface RegistrationService {
public String create(RegistrationFormDto registration);
}
RegistrationServiceImpl.java
@Service
public class RegistrationServiceImpl implements RegistrationService {
@Autowired
private RegistrationDao registrationDao;
@Override
public String create(RegistrationFormDto registration) {
return registrationDao.create(registration);
}
}
RegistrationDao.java
public interface RegistrationDao {
public String create(RegistrationFormDto registration);
}
RegistrationDaoImpl.java
@Repository
public class RegistrationDaoImpl implements RegistrationDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public String create(RegistrationFormDto registration) {
String query = "INSERT INTO registration (name, email) VALUES (?, ?)";
jdbcTemplate.update(query, registration.getName(), registration.getEmail());
return "Registration created successfully";
}
}
Application Properties
The application uses the following properties:
application.properties
# MySQL database connection properties
spring.datasource.url=jdbc:mysql://localhost:3306/registrationDb
spring.datasource.username=root
spring.datasource.password=password
# Hibernate properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
Comments
Post a Comment