Create addPerson method and delegate the call to PersonDao. Create fetchAllPersons method and delegate the call to PersonDao. Create the DbUtil see sample code below. Create members dataSource see line 11 below Create accessor methods for dataSource see lines below. Create the spring-config. Declare personService bean which depends on personDao see lines below.
Declare personDao bean which depends on jdbcTemplate see lines below. Declare jdbcTemplate bean which depends on dataSource see lines below. Declare dataSource bean see lines below. These database parameters correspond to the HyperSQL in-memory database.
Finally, we need a java program to test our setup. Create the Person object and set the name and email properties see lines below. Use the PersonService. We access the list of Persons and print the output to verify that database operations of inserting and selecting using JdbcDaoSupport have occured successfully see line 23 below.
This sample program has been packaged as a jar installer which will copy the source code along with all necessary dependencies on your machine and automatically run the program for you as shown in the steps below.
Alternatively you can go the folder containing the springjdbcdaosupport-installer. All the required libraries have also been downloaded and placed in the same location. Michael W Michael W 3, 7 7 gold badges 35 35 silver badges 61 61 bronze badges.
Thanks JavaGeek. I was hoping there would be something more convenient - I am working with a table with 50 columns, clearly specifying these in the bean AND in the update statement is duplication. Maybe I'll take a look at SimpleJdbcInsert and see how they implement the insert Personally Id be using hibernate and JPA.
All you need then is a java pojo domain object to map to your table and some connection config in the spring xml. After that the statements are very simple and you can also use native queries similiar to the above. List; import javax. DataSource; import org. Autowired; import org. Qualifier; import org. BeanPropertyRowMapper; import org.
BeanPropertySqlParameterSource; import org. MapSqlParameterSource; import org. SqlParameterSource; import org. SimpleJdbcDaoSupport; import org. ID, b. OIB, b. NAME, b. TOWN, b. Shantha Kumara 2, 3 3 gold badges 34 34 silver badges 49 49 bronze badges. Sajid Sajid 2 2 gold badges 13 13 silver badges 23 23 bronze badges.
Maybe related stackoverflow. Try to remove the persistenceXmlLocation property. It's an alternative to the dataSource property. Add a comment. Active Oldest Votes. A snippet from Spring 3's JavaDoc : This transaction manager also supports direct DataSource access within a transaction i.
Transactional public void updateDatabase.. Improve this answer. Espen Espen Espen Thaks for the answer. Do I have to write another transactional manager? How do I mix these two transaction?
I could do this mix in EJB 2. Instead of TransactionAttribute you use Transactional instead. And yes, its definitely possible without JTA and with Spring. It only requires that all queries are executed against the same datasource.
A — Espen. Can you point me to a working code so that I can sort out the differences? Just realize this is not Spring but I'll leave it up anyway.
0コメント