Skip to content

Instantly share code, notes, and snippets.

View raviyasas's full-sized avatar
🤠
Available

Ravi Yasas raviyasas

🤠
Available
View GitHub Profile
@Transactional
public List<Employee> getEmployees() {
Connection connection = dataSource.getConnection();
try (connection) {
connection.setAutoCommit(false);
return employeeRepository.findAll();
connection.commit();
} catch (SQLException e) {
connection.rollback();
}
package com.app.sr.service;
import com.app.sr.entity.Customer;
import com.app.sr.repository.CustomerRepository;
import com.app.sr.util.model.ApiResponse;
import com.app.sr.util.model.ResponseBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
package com.app.sr.controller;
import com.app.sr.service.CustomerService;
import com.app.sr.util.model.ApiResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
package com.app.sr.util.model;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class ResponseBuilder {
public ResponseEntity <ApiResponse> buildResponse(
package com.app.sr.util.model;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import java.util.Collections;
import java.util.Map;
@JsonPropertyOrder({ "httpHeaders", "httpStatusCode", "message", "data", "otherParams" })
@raviyasas
raviyasas / Car.java
Created May 29, 2020 14:33
Builder pattern - using inner class
package com.app.designPatterns.builder3;
public class Car {
private final String make;
private final Integer price;
private final Boolean sunroof;
private final Boolean alloyWheels;
private final Boolean cruiseControl;
private final Boolean electricSeats;
@raviyasas
raviyasas / BuilderApp.java
Created May 29, 2020 14:31
Builder pattern
package com.app.designPatterns.builder1;
public class BuilderApp {
public static void main(String[] args) {
Phone iphone = new PhoneBuilder().setModel("Apple").setOs("IOS").buildPhone();
System.out.println(iphone);
Phone androidPhone = new PhoneBuilder().setModel("Nokia").setOs("Android").setCamera("32MP").buildPhone();
package com.app.designPatterns.singleton;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SingletonDbManager {
private static SingletonDbManager instance;
private static Connection connection;
public enum EnumInit {
instance;
}
public class ThreadSafe {
private static volatile ThreadSafe instance;
private ThreadSafe() {
// Avoid reflection API to create objects
if(instance != null){
throw new RuntimeException("Use getInstance method");
}
}