|
From: Thomas R. <tho...@tr...> - 2007-05-15 15:27:25
|
I've added named parameter support to the SimpleJdbcTemplate . Combining
this with the ParameterizedBeanPropertyRowMapper, that I committed
recently, and the BeanPropertySqlParameterSource we can now retrieve and
store JavaBeans(TM) to the database without having to write custom code
to populate or retrieve the bean property values.
Here is an example:
Customer cust = simpleJdbcTemplate.queryForObject(
"select id, customer_number, name from customer where id
= :id",
new
ParameterizedBeanPropertyRowMapper<Customer>(Customer.class),
new MapSqlParameterSource().addValue("id", 5L));
cust.setName("Thomas");
int rows = simpleJdbcTemplate.update(
"update customer set name = :name where id = :id",
new BeanPropertySqlParameterSource(cust));
I added the named parameter methods to the existing SimpleJdbcTemplate
class rather than creating a new SimpleNamedParameterJdbcTemplate
class. If you think this would be better split up, let me know.
If anyone can come up with a better way of instantiating this:
ParameterizedBeanPropertyRowMapper<Customer>(Customer.class) - then that
would be great, I haven't found a way of avoiding specifying Customer
twice there due to the type erasure.
Thomas
|
|
From: <don...@gm...> - 2007-05-15 15:50:01
|
On 5/15/07, Thomas Risberg <tho...@tr...> wrote: If anyone can come up with a better way of instantiating this: > ParameterizedBeanPropertyRowMapper<Customer>(Customer.class) - then that > would be great, I haven't found a way of avoiding specifying Customer > twice there due to the type erasure. > This won't avoid specifiying the class twice but it will ensure they are the same: public ParameterizedBeanPropertyRowMapper(Class<T> mappedClass) instead of public ParameterizedBeanPropertyRowMapper(Class mappedClass) Donnchadh |
|
From: Thomas R. <tho...@tr...> - 2007-05-15 16:21:43
|
Donnchadh, That's a good point, thanks. I've added that. Thomas Donnchadh Ó Donnabháin wrote: > On 5/15/07, Thomas Risberg <tho...@tr...> wrote: > If anyone can come up with a better way of instantiating this: > >> ParameterizedBeanPropertyRowMapper<Customer>(Customer.class) - then that >> would be great, I haven't found a way of avoiding specifying Customer >> twice there due to the type erasure. >> >> > This won't avoid specifiying the class twice but it will ensure they > are the same: > public ParameterizedBeanPropertyRowMapper(Class<T> mappedClass) > instead of > public ParameterizedBeanPropertyRowMapper(Class mappedClass) > > Donnchadh > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > https://lists.sourceforge.net/lists/listinfo/springframework-developer > > |
|
From: Guillaume P. <gpo...@sy...> - 2007-05-16 00:10:48
|
You can avoid it with a static factory method, e.g. :
|public class GenericsTest<T> {
public static <T> GenericsTest<T> create(Class<T> clazz) {
return new GenericsTest<T>(clazz);
}
public GenericsTest(Class<T> clazz) { }
public T get() { return null; };
public static void main(String[] args) {
String s = create(String.class).get();
System.out.println(s);
}
}|
This way the generic type is inferred by the parameter type.
Guillaume
Thomas Risberg wrote:
> I've added named parameter support to the SimpleJdbcTemplate . Combining
> this with the ParameterizedBeanPropertyRowMapper, that I committed
> recently, and the BeanPropertySqlParameterSource we can now retrieve and
> store JavaBeans(TM) to the database without having to write custom code
> to populate or retrieve the bean property values.
>
> Here is an example:
>
> Customer cust = simpleJdbcTemplate.queryForObject(
> "select id, customer_number, name from customer where id
> = :id",
> new
> ParameterizedBeanPropertyRowMapper<Customer>(Customer.class),
> new MapSqlParameterSource().addValue("id", 5L));
>
> cust.setName("Thomas");
>
> int rows = simpleJdbcTemplate.update(
> "update customer set name = :name where id = :id",
> new BeanPropertySqlParameterSource(cust));
>
>
> I added the named parameter methods to the existing SimpleJdbcTemplate
> class rather than creating a new SimpleNamedParameterJdbcTemplate
> class. If you think this would be better split up, let me know.
>
> If anyone can come up with a better way of instantiating this:
> ParameterizedBeanPropertyRowMapper<Customer>(Customer.class) - then that
> would be great, I haven't found a way of avoiding specifying Customer
> twice there due to the type erasure.
>
>
> Thomas
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>
>
|
|
From: Thomas R. <tho...@tr...> - 2007-05-16 01:55:42
|
Guillaume,
That's a good alternative, thanks.
That reminds me - I should have consulted "Effective Java" - Item 1:
Consider providing static factory methods instead of constructors :)
Thomas
Guillaume Poirier wrote:
> You can avoid it with a static factory method, e.g. :
>
> |public class GenericsTest<T> {
> public static <T> GenericsTest<T> create(Class<T> clazz) {
> return new GenericsTest<T>(clazz);
> }
>
> public GenericsTest(Class<T> clazz) { }
>
> public T get() { return null; };
>
> public static void main(String[] args) {
> String s = create(String.class).get();
> System.out.println(s);
> }
> }|
>
> This way the generic type is inferred by the parameter type.
>
> Guillaume
>
>
> Thomas Risberg wrote:
>> I've added named parameter support to the SimpleJdbcTemplate . Combining
>> this with the ParameterizedBeanPropertyRowMapper, that I committed
>> recently, and the BeanPropertySqlParameterSource we can now retrieve and
>> store JavaBeans(TM) to the database without having to write custom code
>> to populate or retrieve the bean property values.
>>
>> Here is an example:
>>
>> Customer cust = simpleJdbcTemplate.queryForObject(
>> "select id, customer_number, name from customer where id
>> = :id",
>> new
>> ParameterizedBeanPropertyRowMapper<Customer>(Customer.class),
>> new MapSqlParameterSource().addValue("id", 5L));
>>
>> cust.setName("Thomas");
>>
>> int rows = simpleJdbcTemplate.update(
>> "update customer set name = :name where id = :id",
>> new BeanPropertySqlParameterSource(cust));
>>
>>
>> I added the named parameter methods to the existing SimpleJdbcTemplate
>> class rather than creating a new SimpleNamedParameterJdbcTemplate
>> class. If you think this would be better split up, let me know.
>>
>> If anyone can come up with a better way of instantiating this:
>> ParameterizedBeanPropertyRowMapper<Customer>(Customer.class) - then that
>> would be great, I haven't found a way of avoiding specifying Customer
>> twice there due to the type erasure.
>>
>>
>> Thomas
>>
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by DB2 Express
>> Download DB2 Express C - the FREE version of DB2 express and take
>> control of your XML. No limits. Just data. Click to get it now.
>> http://sourceforge.net/powerbar/db2/
>> _______________________________________________
>> Springframework-developer mailing list
>> Spr...@li...
>> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>>
>>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> ------------------------------------------------------------------------
>
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>
|