|
From: Thomas R. <tho...@tr...> - 2005-11-19 16:50:37
|
I was talking to Mark last weekend about data access for Spring.NET and we discussed the .NET class SqlCommand that provides a nice and clean interface to the ADO.NET functionality. It got me thinking, and after adding the support for named parameters, I counted over 75 different methods for interfacing with the JdbcTemplate - that's a lot :) So, I started looking at providing a SqlCommand interface on top of JdbcTemplate to just present the most used features and limit the sometimes bewildering number of options for how to pass in the parameters etc. I decided to only support the new named parameter support. The old position based array style is still available from the JdbcTemplate directly. I added a new class org.springframework.jdbc.command.SqlCommand to the sandbox. It's simply a number of one-line wrappers on top of the JdbcTemplate. Combined with the new named parameter support, it provides a simpler interface IMHO. Here are a few examples: SqlCommand listOfBeersCommand = new SqlCommand("select id, brand, price from beers", dataSource); List beerList = listOfBeersCommand.executeQuery(new BeerMapper()); SqlCommand priceCommand = new SqlCommand("select price from beers where brand = :brand", dataSource); Map parameters = new HashMap(); parameters.put("brand", "Heineken"); Number price = (Number)priceCommand.executeScalar(parameters); Also, with the new option of passing in a JavaBean as the holder of the parameter values (SqlParameterBeanWrapper) it does provide the option of using the following code: SqlCommand readCommand = new SqlCommand("select id, brand, price from beers where id = :id", dataSource); SqlCommand saveCommand = new SqlCommand("update beers set brand = :brand, price = :price where id = :id", dataSource); Map parameters = new HashMap(); parameters.put("id", new Long(2)); Beer beer = (Beer)readCommand.executeObject(new BeerMapper (), parameters); beer.setPrice(new BigDecimal("49.87")); SqlNamedParameters updateValues = new SqlParameterBeanWrapper (beer); int updateCount = saveCommand.executeUpdate(updateValues); All this is in CVS now (SqlCommand is in the sandbox) and if you have any feedback, I'm all ears. Thomas |