JdbcTemplate using Spring Boot provides update(String sql, Object... args) method which supports upsert (nsert or update if exists) operation. You can use this method to perform an upsert operation in the database.
Code Snippet:String sql = "INSERT INTO employee (employeeId, employeeName, employeeDateOfBirth, employeeDepartment, employeeJoiningDate, employeeSalary) " +
"VALUES (:id, :name, :dob, :department, :joiningDate, :salary) " +
"ON DUPLICATE KEY UPDATE " +
"employeeName=VALUES(employeeName), " +
"employeeDateOfBirth=VALUES(employeeDateOfBirth), " +
"employeeDepartment=VALUES(employeeDepartment), " +
"employeeJoiningDate=VALUES(employeeJoiningDate), " +
"employeeSalary=VALUES(employeeSalary)";
MapSqlParameterSource[] batchParams = employees.stream()
.map(employee -> new MapSqlParameterSource()
.addValue("id", employee.getEmployeeId())
.addValue("name", employee.getEmployeeName())
.addValue("dob", Date.valueOf(employee.getEmployeeDateOfBirth()))
.addValue("department", employee.getEmployeeDepartment())
.addValue("joiningDate", Date.valueOf(employee.getEmployeeJoiningDate()))
.addValue("salary", employee.getEmployeeSalary()))
.toArray(MapSqlParameterSource[]::new);
namedParameterJdbcTemplate.batchUpdate(sql, batchParams);
Related Examples:
- Spring Boot: NamedParameterJdbcTemplate batch insert example
- JdbcTemplate Batch Insert Example using Spring Boot
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- Spring Boot: JdbcTemplate Update Query With Parameters Example
- JDBCTemplate Querying Examples with Spring Boot 3
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- Git: Step-by-Step - How to Push Local Brach to GitHub - Git
- How to run React Native App using Android Studio Emulator - Android-Studio
- How to Add New Entry to ZSH PATH - zsh
- The Date Command and its usage [Linux - Unix - macOS] - Linux
- Cannot access Windows application shortcuts on Start menu and Taskbar - Windows
- JavaScript : redirect page to other url - Android
- Is Facebook is down? Is it just for me? - Facebook
- SharePoint List excel import - This table exceeds the maximum number of supported rows - SharePoint