Announcement Announcement Module
Collapse
No announcement yet.
Using MapSqlParameterSource with a custom class? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using MapSqlParameterSource with a custom class?

    Hello,

    I want to use the JDBC Support by using the NamedParameterJDBCTemplate in combination with the MapSqlParameterSource.

    Lets say I have a DB-Table with userdata. I want to get one user like that :

    SqlParameterSource param = new MapSqlParameterSource("userid",1);

    That works fine and I get the data of user 1. But now I want to make a custom UserId Class. Lets say like

    public class UserId {
    protected Long id;

    public Long getId() {
    return id;
    }

    public void setId(long id) {
    this.id = new Long(id);
    }
    }


    At the moment I have to use it like

    SqlParameterSource param = new MapSqlParameterSource("userid",userId.getId());

    to work. But how can I use it like

    SqlParameterSource param = new MapSqlParameterSource("userid",userId);

    I tried by overwriting the toString() Method and/or by adding "Implements Serializable" to the classdefintion - but I always get an exception and it does not work.

    Any one can help me?

    Thanks alot!

    Regards, Heiko

  • #2
    Have you tried something like this?
    String SQL = "select * from User_Table where userid = :userid";
    return getSimpleJdbcTemplate().query(
    SQL
    , new UserMapper()
    , new MapSqlParameterSource()
    .addValue("userid",userId.getId(), Types.INTEGER)
    );

    Which version of Spring? Which SDK? What is the exception?

    Comment

    Working...
    X