Results 1 to 2 of 2

Thread: Using MapSqlParameterSource with a custom class?

  1. #1
    Join Date
    Feb 2013
    Posts
    1

    Question 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. #2
    Join Date
    Jan 2006
    Posts
    2

    Default

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •