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
You can download this article in various formats for your convenience. Choose from the options below:
Facing issues? Have Questions? Post them here! I am happy to answer!
- Java Jackson ObjectMapper Class with Examples
- How to add a Delay of a Few Seconds in Java Program
- Java SE JDBC Select Statement Example
- How to Word-Warp Console logs in IntelliJ
- Convert Java Array to ArrayList Code Example
- [Java] Error: Unmappable character for encoding UTF-8. Save could not be completed.
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- How to extract Java Jar/War/Ear files in Linux
- Java: Convert String to Binary
- Java Generics: Type parameter cannot be instantiated directly
- How to Create and Run a Command Line Application using Spring Boot
- How to Convert a Java Object into an Optional with Example
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to Set JAVA_HOME PATH to Mac .zshrc profile file
- Formatting Double in Java [Examples]
- Java 8: Stream map with Code Examples
- Fix: SyntaxError: ( was never closed [Python]
- Java Split String by Spaces
- [fix] Java JDBC SQLSyntaxErrorException: Unknown database
- Fix: SpringFramework BeanDefinitionValidationException: Could not find an init method named
- Java Date Time API: LocalDateTime get(TemporalField field) examples
- How to create Date in yyyy-MM-dd format in Java
- Java: Create Temporary Directory and File and Delete when application terminates
- Fix RabbitMQ: AuthenticationFailureException: ACCESS_REFUSED
- Run Java Code Every Second
- How to submit website to dmoz directory - HowTos
- Restore deleted Office 365 SharePoint group site - SharePoint
- Manually Throw an Exception in Python (raise:) - Python
- How to reset an Apple Watch without an iPhone - Apple
- How to Restart Mac using Terminal Command - MacOS
- How to query database table names [MySQL/Oracle/SQL Server] - MySQL
- Fix: POSSIBLE DATA LOSS: Some features might be lost if you save this workbook in the comma-delimited (csv) format. To preserve these features - save it in an Excel file format - Windows
- How to Sort CSV File Data in Notepad++ based on a Column - NotepadPlusPlus