-
Feb 13th, 2013, 04:20 AM
#1
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
-
Mar 7th, 2013, 02:44 PM
#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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules