Spring: Data access using JdbcDaoSupport
Similar to JDBCTemplate by extending JdbcDaoSupport classes we can achieve JBDC dataaccess and just need to inject data source into EmpDaoImpl Objects through configuration file. Rest will be taken care by Spring.
In our earlier tutorials we have seen about Spring JDBC and JDBCTemplate examples. Lets take same example class and will extend JdbcDaoSupport class.
pom.xml remains same
Employee.java remains same
EmpDao.java remains same
MyTestClass.java
EmpDaoImpl.java
OUTPUT:
In our earlier tutorials we have seen about Spring JDBC and JDBCTemplate examples. Lets take same example class and will extend JdbcDaoSupport class.
pom.xml remains same
Employee.java remains same
EmpDao.java remains same
MyTestClass.java
package com.app; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.app.dao.EmpDao; import com.app.model.Employee; public class MyTestClass { public static void main(String[] args) { ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("spring-jdbc.xml"); EmpDao object = appContext.getBean(EmpDao.class); Employee emp = new Employee(143, "Bill", "Director"); object.insert(emp); appContext.close(); } }
EmpDaoImpl.java
package com.app.dao; import org.springframework.jdbc.core.support.JdbcDaoSupport; import com.app.model.Employee; public class EmpDaoImpl extends JdbcDaoSupport implements EmpDao { public void insert(Employee emp) { String query = "INSERT INTO employee (id, empname, designation) VALUES (?, ?, ?)"; getJdbcTemplate().update(query, new Object[] { emp.getId(), emp.getEmpName(), emp.getDesignation() }); } }
OUTPUT: