Skip to content

Commit 3ad3ecb

Browse files
committed
Spring Boot JDBC POSTMAN
1 parent 415b47c commit 3ad3ecb

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

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

Lines changed: 11 additions & 6 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,22 +18,22 @@
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(){
35-
return userRepository.getUser();
36+
return userRepository.getUsers();
3637
}
3738

3839
@GetMapping(value = "/{id}")
@@ -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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
public class UserRepository {
2525

2626
@Autowired
27-
JdbcTemplate jdbcTemplate;
27+
private JdbcTemplate jdbcTemplate;
2828

29-
public List<User> getUser(){
30-
return jdbcTemplate.query("select id,firstname,lastname,city , country , phoneno,emailid from user", new UserRowMapper());
29+
public List<User> getUsers() {
30+
String sql = "SELECT * FROM user";
31+
32+
return jdbcTemplate.query(sql, new UserRowMapper());
33+
//return jdbcTemplate.query("select id, firstname, lastname, city, country, phoneno, emailid from user", new UserRowMapper());
3134
}
3235

33-
public User findById(Integer id){
36+
public User findById(Integer id) {
3437

3538
String sql = "SELECT * FROM user WHERE ID = ?";
3639
try{
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package spring.database.model;
22

3-
import javax.swing.tree.RowMapper;
3+
import org.springframework.jdbc.core.RowMapper;
44
import java.sql.ResultSet;
55
import java.sql.SQLException;
66

@@ -9,9 +9,10 @@
99
* @project database
1010
* @Author Hamdamboy
1111
*/
12-
public class UserRowMapper implements RowMapper {
13-
//
14-
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
12+
public class UserRowMapper implements RowMapper<User> {
13+
14+
@Override
15+
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
1516
User user = new User(); // Obj of User, and mapping DB elements.
1617
user.setId(rs.getInt("id"));
1718
user.setFirstname(rs.getString("firstname"));
@@ -20,9 +21,6 @@ public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
2021
user.setCountry(rs.getString("country"));
2122
user.setPhoneno(rs.getString("phoneno"));
2223
user.setEmailid(rs.getString("emailid"));
23-
2424
return user;
2525
}
26-
27-
2826
}

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

0 commit comments

Comments
 (0)