Skip to content

Commit 3ba2bcc

Browse files
Add files via upload
1 parent ea4bbce commit 3ba2bcc

File tree

12 files changed

+357
-0
lines changed

12 files changed

+357
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
https://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>3.0.1</version>
11+
<relativePath/> <!-- lookup parent from repository -->
12+
</parent>
13+
<groupId>com.knf.dev.demo</groupId>
14+
<artifactId>spring-boot-postgresql-mybatis-xml-crud</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<name>spring-boot-postgresql-mybatis-xml-crud</name>
17+
<description>Demo project for Spring Boot</description>
18+
<properties>
19+
<java.version>17</java.version>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.mybatis.spring.boot</groupId>
28+
<artifactId>mybatis-spring-boot-starter</artifactId>
29+
<version>3.0.0</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.postgresql</groupId>
34+
<artifactId>postgresql</artifactId>
35+
<scope>runtime</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-maven-plugin</artifactId>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.knf.dev.demo;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication
8+
@MapperScan("com.knf.dev.demo.repository")
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Application.class, args);
13+
}
14+
15+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.knf.dev.demo.controller;
2+
3+
import com.knf.dev.demo.exception.ResourceNotFoundException;
4+
import com.knf.dev.demo.model.User;
5+
import com.knf.dev.demo.repository.UserRepository;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.*;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
@RestController
13+
@RequestMapping("/api/v1/")
14+
public class UserController {
15+
@Autowired
16+
private UserRepository userRepository;
17+
18+
// get all users
19+
@GetMapping("/users")
20+
public List<User> getAllUsers()
21+
{
22+
return userRepository.findAll();
23+
}
24+
25+
// create user rest API
26+
@PostMapping("/users")
27+
public Map<String, Boolean> createUser(@RequestBody User user) {
28+
29+
Map<String, Boolean> response = new HashMap<>();
30+
31+
Boolean bool = userRepository.insert(user) > 0 ?
32+
response.put("created", Boolean.TRUE) :
33+
response.put("created", Boolean.FALSE);
34+
35+
return response;
36+
37+
}
38+
39+
// get user by id rest api
40+
@GetMapping("/users/{id}")
41+
public User findUserById(@PathVariable Integer id) {
42+
43+
User user = userRepository.findById(id).
44+
orElseThrow(() -> new ResourceNotFoundException
45+
("User not exist with id :" + id));
46+
return user;
47+
}
48+
49+
// update user rest api
50+
@PutMapping("/users/{id}")
51+
public Map<String, Boolean> updateUser(@PathVariable Integer id,
52+
@RequestBody User userDetails) {
53+
54+
User user = userRepository.findById(id)
55+
.orElseThrow(() -> new ResourceNotFoundException
56+
("User not exist with id :" + id));
57+
userDetails.setId(id);
58+
Map<String, Boolean> response = new HashMap<>();
59+
60+
Boolean bool = userRepository.update(userDetails) > 0 ?
61+
response.put("updated", Boolean.TRUE) :
62+
response.put("updated", Boolean.FALSE);
63+
64+
return response;
65+
}
66+
67+
// delete user rest api
68+
@DeleteMapping("/users/{id}")
69+
public Map<String, Boolean> deleteUser
70+
(@PathVariable Integer id) {
71+
72+
User user = userRepository.findById(id)
73+
.orElseThrow(() -> new ResourceNotFoundException
74+
("User not exist with id :" + id));
75+
76+
Map<String, Boolean> response = new HashMap<>();
77+
78+
Boolean bool = userRepository.deleteById(user.getId()) > 0 ?
79+
response.put("deleted", Boolean.TRUE) :
80+
response.put("deleted", Boolean.FALSE);
81+
return response;
82+
}
83+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.knf.dev.demo.exception;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import java.time.LocalDateTime;
5+
6+
public class CustomErrorResponse {
7+
8+
@JsonFormat(shape = JsonFormat.Shape.STRING,
9+
pattern = "yyyy-MM-dd hh:mm:ss")
10+
private LocalDateTime timestamp;
11+
private int status;
12+
private String error;
13+
public LocalDateTime getTimestamp()
14+
{
15+
return timestamp;
16+
}
17+
public void setTimestamp(LocalDateTime timestamp)
18+
{
19+
this.timestamp = timestamp;
20+
}
21+
public int getStatus()
22+
{
23+
return status;
24+
}
25+
public void setStatus(int status)
26+
{
27+
this.status = status;
28+
}
29+
public String getError()
30+
{
31+
return error;
32+
}
33+
public void setError(String error)
34+
{
35+
this.error = error;
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.knf.dev.demo.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.ControllerAdvice;
6+
import org.springframework.web.bind.annotation.ExceptionHandler;
7+
import org.springframework.web.context.request.WebRequest;
8+
import java.time.LocalDateTime;
9+
10+
@ControllerAdvice
11+
public class GlobalExceptionHandler {
12+
13+
@ExceptionHandler(ResourceNotFoundException.class)
14+
public ResponseEntity<CustomErrorResponse>
15+
globalExceptionHandler(Exception ex, WebRequest request) {
16+
CustomErrorResponse errors = new CustomErrorResponse();
17+
errors.setTimestamp(LocalDateTime.now());
18+
errors.setError(ex.getMessage());
19+
errors.setStatus(HttpStatus.NOT_FOUND.value());
20+
return new ResponseEntity<>(errors, HttpStatus.NOT_FOUND);
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.knf.dev.demo.exception;
2+
3+
public class ResourceNotFoundException extends RuntimeException{
4+
private static final long serialVersionUID = 1L;
5+
6+
public ResourceNotFoundException(String message) {
7+
super(message);
8+
}
9+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.knf.dev.demo.model;
2+
3+
public class User {
4+
5+
private Integer id;
6+
private String firstName;
7+
private String lastName;
8+
private String emailId;
9+
10+
public User() {
11+
}
12+
13+
public User(Integer id,String firstName,
14+
String lastName, String emailId) {
15+
super();
16+
this.id=id;
17+
this.firstName = firstName;
18+
this.lastName = lastName;
19+
this.emailId = emailId;
20+
}
21+
22+
public Integer getId() {
23+
return id;
24+
}
25+
26+
public void setId(Integer id) {
27+
this.id = id;
28+
}
29+
30+
public String getFirstName() {
31+
return firstName;
32+
}
33+
34+
public void setFirstName(String firstName) {
35+
this.firstName = firstName;
36+
}
37+
38+
public String getLastName() {
39+
return lastName;
40+
}
41+
42+
public void setLastName(String lastName) {
43+
this.lastName = lastName;
44+
}
45+
46+
public String getEmailId() {
47+
return emailId;
48+
}
49+
50+
public void setEmailId(String emailId) {
51+
this.emailId = emailId;
52+
}
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.knf.dev.demo.repository;
2+
3+
import com.knf.dev.demo.model.User;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
public interface UserRepository {
8+
9+
List<User> findAll();
10+
11+
Optional<User> findById(Integer id);
12+
13+
int deleteById(Integer id);
14+
15+
int insert(User user);
16+
17+
int update(User user);
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
2+
spring.datasource.username=postgres
3+
spring.datasource.password=root
4+
5+
spring.sql.init.mode=always
6+
7+
#Configure the xml Mapping path
8+
mybatis.mapper-locations=classpath:mapper/UserMapper.xml
9+
#Configure entity category names
10+
mybatis.type-aliases-package=com.knf.dev.demo.model
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE mapper
3+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5+
<mapper namespace="com.knf.dev.demo.repository.UserRepository">
6+
<resultMap type="user" id="result">
7+
<id property="id" column="id" />
8+
<result property="firstName" column="first_name" />
9+
<result property="lastName" column="last_name" />
10+
<result property="emailId" column="email_id" />
11+
</resultMap>
12+
13+
<select id="findAll" resultType="user" resultMap="result">
14+
SELECT * FROM users
15+
</select>
16+
17+
<insert id="insert" parameterType="user"
18+
keyProperty="id" useGeneratedKeys="true">
19+
INSERT INTO users(first_name, last_name, email_id)
20+
VALUES(#{firstName}, #{lastName}, #{emailId})
21+
</insert>
22+
23+
<update id="update" parameterType="user">
24+
UPDATE users SET first_name = #{firstName},
25+
email_id = #{emailId}, last_name = #{lastName}
26+
WHERE id = #{id}
27+
</update>
28+
29+
<delete id="deleteById" parameterType="int">
30+
DELETE FROM users WHERE id = #{id}
31+
</delete>
32+
33+
<select id="findById" parameterType="int"
34+
resultType="user" resultMap="result">
35+
SELECT * FROM users WHERE id = #{id}
36+
</select>
37+
</mapper>

0 commit comments

Comments
 (0)