Searching...
Sunday, 1 June 2014

Spring BeanPropertyRowMapper Example

01:49

BeanPropertyRowMapper implementation that converts a row into a new instance of the specified mapped target class. The mapped target class must be a top-level class and it must have a default or no-arg constructor.

Spring configuration

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"  value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/springschema"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
 	<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     <constructor-arg>
        <ref bean="dataSource"/>
     </constructor-arg>    
     </bean>

Main class


ApplicationContext appContext = new ClassPathXmlApplicationContext(
"SpringConfig.xml");
JdbcTemplate template = (JdbcTemplate) appContext
.getBean("jdbctemplate");
Student st = template.queryForObject(
"select * from student where name=?",
new BeanPropertyRowMapper<Student>(Student.class), "Shiva");
System.out.println(st);

0 comments: