Announcement Announcement Module
Collapse
No announcement yet.
rowMapper or MappingSqlQuery? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • rowMapper or MappingSqlQuery?

    Hello,

    I'm participating in the process of migrating existing DAOs of a web application to use Spring for database interaction.

    I am not sure which way is more efficient/fast: having an inner class inside the DAO which extends rowMapper for the SELECT statements, or perhaps having an inner class which extends MappingSqlQuery inside every method which executes a SELECT?

    From the amount of code point of view, the first option seems more attractive, because if I have five methods that all fetch the same information from the result set, then they can all use the same class. However, the manner in which I have to call the rowMapper in this case:

    List result = jt.query(SELECT_ACCOUNT_ON_ID, parms, parmsType, new AccountDAO().new ColumnRowMapper());

    seems too complex and maybe even improper, because such call is executed inside a static method.

    Any thoughts would be greatly appreciated.

    Thank you.

  • #2
    Personally I tend to use the JdbcTemplate/callback approach most of the time, and the RdbmsObject approach mainly for stored procedures. I'd go for JdbcTemplate here. The method being static will only be a problem depending on what you do with the RowMapper--whether it's purely local to that method, in which case things will be fine.

    HTH,
    Rod

    Comment


    • #3
      Since the RowMapper doesn't have any local state, you should create only one and use it upon each invocation of the query. Since you are using static methods, it would a static member.

      BTW, what does ColumnRowMapper do? ColumnMapRowMapper comes with Spring and creates a map with each column value using the actual name of the column.

      Comment


      • #4
        Thank you for your prompt replies!


        Originally posted by Rod Johnson
        The method being static will only be a problem depending on what you do with the RowMapper--whether it's purely local to that method, in which case things will be fine.
        The line of code that I included above is the only place where the rowMapper is used, after that I get a list and peacefully return it or convert to some other data structure.


        Originally posted by wpoitras
        BTW, what does ColumnRowMapper do?
        In my case ColumnRowMapper implements org.springframework.jdbc.core.RowMapper and has just one method, mapRow, which does exactly what its name implies, something like:

        public Object mapRow(ResultSet rs, int rowNum) throws SQLException
        {
        AccountVO vo = new AccountVO();
        vo.setName(rs.getString("NAME"));
        return vo;
        }

        Comment


        • #5
          Generally, what is the difference between the two? I know that one can use an inner class which extends MappingSqlQuery for dealing with complex SELECT statements, and JdbcTemplate.queryForObject for simple ones. So when would one use rowMapper?

          Comment

          Working...
          X