Skip to content

Commit aeb3fd7

Browse files
committed
Spring Boot JDBC Template MySQL POSTMAN REST API
2 parents 40fdd21 + 384b3fd commit aeb3fd7

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

Project1-SpringBoot-RestAPI-JDBC-MySQL-Gradle/SpringDatabase/src/main/java/spring/database/controller/UserController.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import spring.database.dao.UserRepository;
88
import spring.database.model.User;
99

10+
import java.lang.annotation.Documented;
1011
import java.sql.SQLIntegrityConstraintViolationException;
1112
import java.util.List;
1213

@@ -17,20 +18,20 @@
1718
*/
1819

1920
@RestController
20-
@RequestMapping("/user")
21+
@RequestMapping("/users")
2122
public class UserController {
2223
//
2324

2425
@Autowired
25-
UserRepository userRepository;
26+
private UserRepository userRepository;
2627

2728
@GetMapping("/main")
2829
public String test(){
2930
String testing = "testing";
3031
return testing;
3132
}
3233

33-
@GetMapping
34+
@GetMapping("/list")
3435
public List<User> getAllUsers(){
3536
return userRepository.getUsers();
3637
}
@@ -44,12 +45,11 @@ public ResponseEntity<?> getUser (@PathVariable("id") Integer id) {
4445
return new ResponseEntity<User>(user, HttpStatus.OK);
4546
}
4647

47-
@PostMapping
48+
@PostMapping(value = "/add")
4849
public ResponseEntity<String> createUser(@RequestBody User user) throws SQLIntegrityConstraintViolationException {
4950
if(userRepository.findById(user.getId()) != null) {
5051
return new ResponseEntity<String>("Duplicate Entry: " + user.getId(), HttpStatus.IM_USED);
5152
}
52-
5353
userRepository.saveUser(user);
5454
return ResponseEntity.status(HttpStatus.CREATED).build();
5555
}
@@ -64,6 +64,11 @@ public ResponseEntity<?> updateUser(@RequestBody User user) {
6464
return new ResponseEntity<User>(user, HttpStatus.OK);
6565
}
6666

67+
/**
68+
* Deletes user by id
69+
* @param id user's id where is being deleted
70+
* @return ResponseEntity deleted user entity
71+
* */
6772
@DeleteMapping(value = "/{id}")
6873
public ResponseEntity<?> deleteUser(@PathVariable("id") Integer id) {
6974
User user = userRepository.findById(id);

Project1-SpringBoot-RestAPI-JDBC-MySQL-Gradle/SpringDatabase/src/main/java/spring/database/dao/UserRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class UserRepository {
2424

2525
@Autowired
2626
private JdbcTemplate jdbcTemplate;
27+
<<<<<<< HEAD
2728

2829
public UserRepository(JdbcTemplate jdbcTemplate) {
2930
this.jdbcTemplate = jdbcTemplate;
@@ -32,6 +33,14 @@ public UserRepository(JdbcTemplate jdbcTemplate) {
3233
public List<User> getUsers() {
3334
// return jdbcTemplate.query(sql, new UserRowMapper());
3435
return jdbcTemplate.query("select id,firstname,lastname,city , country , phoneno,emailid from user", new UserRowMapper());
36+
=======
37+
38+
public List<User> getUsers() {
39+
String sql = "SELECT * FROM user";
40+
41+
return jdbcTemplate.query(sql, new UserRowMapper());
42+
//return jdbcTemplate.query("select id, firstname, lastname, city, country, phoneno, emailid from user", new UserRowMapper());
43+
>>>>>>> 384b3fd98fd1c82ac0ed0c83fd266f42d71a1c87
3544
}
3645

3746
public User findById(Integer id) {
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package spring.database.model;
22

3+
<<<<<<< HEAD
34
import javax.swing.tree.RowMapper;
45
import javax.swing.tree.TreePath;
6+
=======
7+
import org.springframework.jdbc.core.RowMapper;
8+
>>>>>>> 384b3fd98fd1c82ac0ed0c83fd266f42d71a1c87
59
import java.sql.ResultSet;
610
import java.sql.SQLException;
711

@@ -10,9 +14,10 @@
1014
* @project database
1115
* @Author Hamdamboy
1216
*/
13-
public class UserRowMapper implements RowMapper {
14-
//
15-
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
17+
public class UserRowMapper implements RowMapper<User> {
18+
19+
@Override
20+
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
1621
User user = new User(); // Obj of User, and mapping DB elements.
1722
user.setId(rs.getInt("id"));
1823
user.setFirstname(rs.getString("firstname"));
@@ -21,13 +26,15 @@ public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
2126
user.setCountry(rs.getString("country"));
2227
user.setPhoneno(rs.getString("phoneno"));
2328
user.setEmailid(rs.getString("emailid"));
24-
2529
return user;
2630
}
31+
<<<<<<< HEAD
2732

2833

2934
@Override
3035
public int[] getRowsForPaths(TreePath[] path) {
3136
return new int[0];
3237
}
38+
=======
39+
>>>>>>> 384b3fd98fd1c82ac0ed0c83fd266f42d71a1c87
3340
}

Project1-SpringBoot-RestAPI-JDBC-MySQL-Gradle/SpringDatabase/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# Database (MySQL)
55

6-
spring.datasource.url= jdbc:mysql://localhost:3306/management
6+
spring.datasource.url= jdbc:mysql://localhost:3306/management?serverTimezone=UTC
77
spring.datasource.username=root
88
spring.datasource.password=posilka2020
99
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
11
# SpringBoot-Database
2-
Spring, Spring Boot, JDBC, JPA, MySQL
2+
3+
This provides Database implementation in the Spring Boot. Indeed, We should breifly inform here concept of Spring, Spring Boot, JDBC, JPA, H2.
4+
5+
Spring
6+
* The Spring Framework is an application framework and inversion of control container for the Java platform. Spring is HA (high avilability).
7+
8+
Spring Boot
9+
* Most Spring Boot applications need minimal Spring configuration. Features. Create stand-alone Spring applications
10+
11+
JPA
12+
* JPA (The Java Presistence API) = This module deals with enhanced support for JPA based data access layers.Indeed, the JPA is set of the rules to interfaces. JPA follows Object-Relation Mapping (ORM). It is a set of interfaces. It also provides a runtime EntityManager API for processing queries and transactions on the objects against the database. It uses a platform-independent object-oriented query language JPQL (Java Persistent Query Language).
13+
14+
Why should we use JPA?
15+
JPA is simpler, cleaner, and less labor-intensive than JDBC, SQL, and hand-written mapping. JPA is suitable for non-performance oriented complex applications.
16+
17+
API (Application programming interface) is a document that contains a description of all the features of a product or software. It represents classes and interfaces that software programs can follow to communicate with each other. An API can be created for applications, libraries, operating systems, etc.
18+
19+
20+
JPA Implementations
21+
JPA is an open-source API. There is various enterprises vendor such as Eclipse, RedHat, Oracle, etc. that provides new products by adding the JPA in them. There are some popular JPA implementations frameworks such as Hibernate, EclipseLink, DataNucleus, etc. It is also known as Object-Relation Mapping (ORM) tool.
22+
23+
JDBC
24+
* Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database.
25+
##
26+
Architecute of the Project Implementation
27+
28+
![JDBC JPA](https://user-images.githubusercontent.com/11626327/76680830-9b1bae00-662f-11ea-84e8-941623ed7283.JPG)
29+
30+
##
31+
H2
32+
* H2 is a relational database management system written in Java. It can be embedded in Java applications or run in client-server mode.
33+
34+
![H2 DB](https://user-images.githubusercontent.com/11626327/76680851-d61de180-662f-11ea-80c8-3a49632ff246.JPG)

0 commit comments

Comments
 (0)