Spring JDBC

Implementing RowMapper in Spring with Example

<p>In this tutorial&comma; we are implementing <b>RowMapper <&sol;b>class to map our domain objects&period; We then use this class to write fetch methods that return custom model objects&period;<br &sol;>&NewLine;<img class&equals;"aligncenter wp-image-3198 size-full" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2012&sol;12&sol;RowMapper&period;png" alt&equals;"Implementing RowMapper" width&equals;"314" height&equals;"183" &sol;><&sol;p>&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<h2>Implementing RowMapper<&sol;h2>&NewLine;<p>Spring provides a RowMapper interface for mapping a single row of a ResultSet to an object&period; It can be used for both single and multiple row queries&period; It is parameterized as of Spring 3&period;0&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public interface RowMapper&lt&semi;T&gt&semi; &lbrace; &NewLine;T mapRow&lpar;ResultSet rs&comma; int rowNum&rpar; &NewLine;throws SQLException&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>An interface used by <b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-jdbctemplate-in-spring-chapter-33&sol;">JdbcTemplate<&sol;a> <&sol;b>for mapping rows of a <b>ResultSet <&sol;b>on a per-row basis&period; Implementations of this interface perform the actual work of mapping each row to a result object&comma; but don&&num;8217&semi;t need to worry about exception handling&period; <b>SQLExceptions <&sol;b>will be caught and handled by the calling <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-jdbctemplate-in-spring-chapter-33&sol;"><b>JdbcTemplate<&sol;b><&sol;a>&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;">&NewLine;<p><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>Typically used either for JdbcTemplate&&num;8217&semi;s query methods or for out parameters of stored procedures&period; <b>RowMapper <&sol;b>objects are typically stateless and thus reusable&semi; they are an ideal choice for implementing row-mapping logic in a single place&period;<&sol;p>&NewLine;<p>Alternatively&comma; consider subclassing <b>MappingSqlQuery <&sol;b>from the <b>jdbc&period;object package<&sol;b>&colon; Instead of working with separate JdbcTemplate and <b>RowMapper <&sol;b>objects&comma; you can build executable query objects &lpar;containing row-mapping logic&rpar; in that style&period;<&sol;p>&NewLine;<p>Say for example&comma; when we are selecting records from an employee table&comma; we will iterate over the result set to get the individual values which won’t be ideal for situations&comma; especially in Java where we want to map records from a database to individual Java objects&period; Also the question of re-usability comes into the picture as the above code doesn’t represent for getting itself re-used&period; Spring Row Mapper interfaces come into the rescue for such situations&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<h2><b>Querying for Single Row&colon;<&sol;b><&sol;h2>&NewLine;<p>Here’s two ways to query or extract a single row record from database&comma; and convert it into a model class&lpar;Employee&rpar;&period;<&sol;p>&NewLine;<h3><b>1&period; Custom RowMapper&colon;<&sol;b><&sol;h3>&NewLine;<p>In general&comma; It’s always recommended to implement the RowMapper interface to create a custom <b>RowMapper <&sol;b>to suit your needs&period;&period;<b> <&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;sdnext&period;jdbc&period;utils&semi; &NewLine; &NewLine;import java&period;sql&period;ResultSet&semi; &NewLine;import java&period;sql&period;SQLException&semi; &NewLine; &NewLine;import org&period;springframework&period;jdbc&period;core&period;RowMapper&semi; &NewLine; &NewLine;import com&period;dineshonjava&period;sdnext&period;domain&period;Employee&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class EmployeeMapper implements RowMapper &lbrace; &NewLine; public Employee mapRow&lpar;ResultSet rs&comma; int rowNum&rpar; throws SQLException &lbrace; &NewLine; Employee employee &equals; new Employee&lpar;&rpar;&semi; &NewLine; employee&period;setEmpid&lpar;rs&period;getInt&lpar;"empid"&rpar;&rpar;&semi; &NewLine; employee&period;setName&lpar;rs&period;getString&lpar;"name"&rpar;&rpar;&semi; &NewLine; employee&period;setAge&lpar;rs&period;getInt&lpar;"age"&rpar;&rpar;&semi; &NewLine; employee&period;setSalary&lpar;rs&period;getLong&lpar;"salary"&rpar;&rpar;&semi; &NewLine; return employee&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>Pass it to <b><i>queryForObject&lpar;&rpar; <&sol;i><&sol;b>method&comma; the returned result will call your custom <b><i>mapRow&lpar;&rpar;<&sol;i><&sol;b> method to match the value into the property&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public Employee getEmployee&lpar;Integer empid&rpar; &lbrace; &NewLine; String SQL &equals; "SELECT &ast; FROM Employee WHERE empid &equals; &quest;"&semi; &NewLine; Employee employee &equals; &lpar;Employee&rpar; jdbcTemplateObject&period;queryForObject&lpar;SQL&comma; new Object&lbrack;&rsqb;&lbrace;empid&rcub;&comma; new EmployeeMapper&lpar;&rpar;&rpar;&semi; &NewLine; return employee&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>2&period; BeanPropertyRowMapper&colon;<&sol;b><&sol;h3>&NewLine;<p>In Spring 2&period;5&comma; comes with a handy <b>RowMapper <&sol;b>implementation called &OpenCurlyQuote;<b>BeanPropertyRowMapper<&sol;b>’&comma; which can maps a row’s column value to a property by matching their names&period; Just make sure both the property and column has the same name&comma; e&period;g property &OpenCurlyQuote;empid’ will match to column name &OpenCurlyQuote;EMPID’ or with underscores &OpenCurlyQuote;EMP&lowbar;ID’&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public Employee getEmployee&lpar;Integer empid&rpar; &lbrace; &NewLine; String SQL &equals; "SELECT &ast; FROM Employee WHERE empid &equals; &quest;"&semi; &NewLine; Employee employee &equals; &lpar;Employee&rpar; jdbcTemplateObject&period;queryForObject&lpar;SQL&comma; new Object&lbrack;&rsqb;&lbrace;empid&rcub;&comma; new BeanPropertyRowMapper&lpar;Employee&period;class&rpar;&rpar;&semi; &NewLine; return employee&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>Querying for Multiple Rows&colon;<&sol;b><&sol;h3>&NewLine;<p>Now&comma; query or extract multiple rows from database&comma; and convert it into a List&period;<&sol;p>&NewLine;<h4><b>1&period; Map it manually&colon;<&sol;b><&sol;h4>&NewLine;<p>In mutiple return rows&comma; RowMapper is not supported in queryForList&lpar;&rpar; method&comma; you need to map it manually&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public List&lt&semi;Employee&gt&semi; findAll&lpar;&rpar;&lbrace; &NewLine; String sql &equals; "SELECT &ast; FROM Employee"&semi; &NewLine; &NewLine; List employees&equals; new ArrayList&lpar;&rpar;&semi; &NewLine; &NewLine; List rows &equals; getJdbcTemplate&lpar;&rpar;&period;queryForList&lpar;sql&rpar;&semi; &NewLine; for &lpar;Map row &colon; rows&rpar; &lbrace; &NewLine; Employee employee &equals; new Employee&lpar;&rpar;&semi; &NewLine; employee&period;setEmpid&lpar;&lpar;Integer&rpar;&lpar;row&period;get&lpar;"EMPID"&rpar;&rpar;&rpar;&semi; &NewLine; employee&period;setName&lpar;&lpar;String&rpar;row&period;get&lpar;"NAME"&rpar;&rpar;&semi; &NewLine; employee&period;setAge&lpar;&lpar;Integer&rpar;row&period;get&lpar;"AGE"&rpar;&rpar;&semi; &NewLine; employee&period;setSalary&lpar;&lpar;Long&rpar;row&period;get&lpar;"SALARY"&rpar;&rpar;&semi; &NewLine; employees&period;add&lpar;employee&rpar;&semi; &NewLine; &rcub; &NewLine; return employees&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h4><b>2&period; BeanPropertyRowMapper&colon;<&sol;b><&sol;h4>&NewLine;<p>The simplest solution is using the BeanPropertyRowMapper class&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public List findAll&lpar;&rpar;&lbrace; &NewLine; String sql &equals; "SELECT &ast; FROM Employee"&semi; &NewLine; List employees&equals; getJdbcTemplate&lpar;&rpar;&period;query&lpar;sql&comma; &NewLine; new BeanPropertyRowMapper&lpar;Employee&period;class&rpar;&rpar;&semi; &NewLine; return employees&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;">&NewLine;<p><b>Spring JDBC Framework Tutorial<&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;">Spring JDBC Framework<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-jdbctemplate-in-spring-chapter-33&sol;">Using JdbcTemplate in Spring<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;dao-support-classes-in-spring-chapter-35&sol;">DAO Support Classes in Spring<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-namedparameterjdbctemplate-in&sol;">Using NamedParameterJdbcTemplate in Spring with Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-simplejdbctemplate-example&sol;">Spring SimpleJdbcTemplate example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-simplejdbcinsert-example&sol;">Spring SimpleJdbcInsert example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;stored-procedure-simplejdbccall-in&sol;">Stored Procedure with SimpleJdbcCall in Spring<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<div style&equals;"text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<&sol;b><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-jdbctemplate-in-spring-chapter-33&sol;">Using JdbcTemplate<&sol;a><&sol;b><b> &vert;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;">index<&sol;a>&vert; <&sol;b><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;dao-support-classes-in-spring-chapter-35&sol;">DAO Support Classes<&sol;a><&sol;b><b><&sol;b><b>&gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-jdbctemplate-in-spring-chapter-33&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;dao-support-classes-in-spring-chapter-35&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '635'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago