How to query using org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport

First of all you will need a pojo and a dao:


package net.istya.vrsbrazil.model;

import java.io.Serializable;

public class Data implements Serializable{

private Long id;
 private String name;

public Long getId() {
 return id;
 }
 public void setId(Long id) {
 this.id = id;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }



}


package net.istya.vrsbrazil.dao;

import net.istya.vrsbrazil.model.Data;

public interface DataDao {

public Data getFromId(Long id);

}

Now write a class that extends SimpleJdbcDaoSupport and implements your Dao:


package net.istya.vrsbrazil.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import net.istya.vrsbrazil.model.Data;
import net.istya.vrsbrazil.dao.DataDao;

import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class JdbcDataDao extends SimpleJdbcDaoSupport implements DataDao{

private Logger logger = Logger.getLogger(JdbcDataDao.class.getName());

 @Override
 public Place getFromId(Long id) {

 logger.info("getFromId: " + id);
 String query = "select * from PLACE where ID= :id";

 Map<String, Long> args = new HashMap<String, Long>();

 args.put("id", id);

 SimpleJdbcTemplate template = getSimpleJdbcTemplate();

Data data= template.queryForObject(query, Data.class, args);

 return data;
 }

}

What you need now is a context file, which in my case is WEB-INF/classes/jdbc/context.xml

 


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

<bean id="dataDao" class="net.istya.vrsbrazil.jdbc.JdbcDataDao">

<property name="dataSource" ref="dataSource" />
 </bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="${jdbc.driverClassName}"/>
 <property name="url" value="${jdbc.url}"/>
 <property name="username" value="${jdbc.username}"/>
 <property name="password" value="${jdbc.password}"/>
 </bean>
 <bean id="propertyConfigurer"
 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="locations">
 <list>
 <value>classpath:jdbc.properties</value>
 </list>
 </property>
 </bean>

 <bean id="transactionManager"
 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource" />
 </bean>

</beans>

 

Now call the config from a main:


//imports

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import  net.istya.vrsbrazil.jdbc.JdbcDataDao;

import  net.istya.vrsbrazil.dao.DataDao;
<pre>import net.istya.vrsbrazil.model.Data;</pre>
//code

ApplicationContext context = new ClassPathXmlApplicationContext("jdbc/context.xml");
<pre>DataDao dao = (JdbcDataDao) context.getBean("dataDao");</pre>
Data data = dao.getFromId(0L);

 

Thank you,

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s