Spring JdbcTemplate IN CLAUSE Example

https://www.technicalkeeda.com/spring-tutorials/spring-jdbctemplate-in-clause-example

 

Useful to whom

This tutorial is useful for beginners and experience developers. It will helps you to learn step by step with the help of attached code.

Tools and Technologies

To execute sample Spring Hibernate program we use below technologies

  1. Maven 3.0.4
  2. JDK 1.6
  3. Spring 3.0.5.RELEASE

Create Database table

Create "trn_employee" table in Mysql database using below sql table script and insert below records.



 
  1.  
  2. CREATE TABLE `trn_employee` (
  3. `employee_id` bigint(20) NOT NULL auto_increment,
  4. `first_name` varchar(255) collate latin1_general_ci default NULL,
  5. `last_name` varchar(255) collate latin1_general_ci default NULL,
  6. PRIMARY KEY (`employee_id`)
  7. ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  8. );
 
  1.  
  2. INSERT INTO `trn_employee` (`employee_id`, `first_name`, `last_name`) VALUES
  3. (1, 'Yashwant','Chavan'),
  4. (2, 'Mahesh', 'Patil'),
  5. (3, 'Jai','Kumar');

Define Dependancies in pom.xml

Create Maven Project and define Spring and other dependencies in pom.xml

 
  1. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. 4.0.0
  3. SpringExamples
  4. SpringExamples
  5. war
  6. 1.0
  7. SpringExamples
  8. SpringExamples
  9. maven-compiler-plugin
  10. true
  11. 1.6
  12. 1.6
  13. 1.6
  14.  
  15. 3.0.5.RELEASE
  16.  
  17. junit
  18. junit
  19. 4.8.1
  20. test
  21. mysql
  22. mysql-connector-java
  23. 5.1.9
  24. log4j
  25. log4j
  26. 1.2.9
  27.  
  28. dom4j
  29. dom4j
  30. 1.6.1
  31.  
  32. commons-logging
  33. commons-logging
  34. 1.1.1
  35.  
  36. commons-collections
  37. commons-collections
  38. 3.2.1
  39.  
  40. cglib
  41. cglib
  42. 2.2
  43.  
  44. asm
  45. asm
  46. 3.1
  47.  
  48. javax.transaction
  49. jta
  50. 1.1
  51.  
  52. org.springframework
  53. spring
  54. 2.5.6

Spring Bean Defination (spring-beans.xml)

Define spring jdbc datasource and bean defination configuration in spring-beans.xml, Which is located under folder "/src/main/resources".

 
  1. xmlns:context="http://www.springframework.org/schema/context"
  2. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="
  4. http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  10. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  11. com.mysql.jdbc.Driver
  12. jdbc:mysql://localhost:3306/technicalkeeda
  13. root

Employee.java

Employee class is simple pojo having getter setter methods.



 
  1. package com.technicalkeeda.bean;
  2.  
  3. public class Employee {
  4.  
  5. private int employeeId;
  6. private String firstName;
  7. private String lastName;
  8.  
  9. public Employee() {
  10. }
  11.  
  12. public Employee(int employeeId, String firstName, String lastName) {
  13. super();
  14. this.employeeId = employeeId;
  15. this.firstName = firstName;
  16. this.lastName = lastName;
  17. }
  18.  
  19. public int getEmployeeId() {
  20. return employeeId;
  21. }
  22.  
  23. public void setEmployeeId(int employeeId) {
  24. this.employeeId = employeeId;
  25. }
  26.  
  27. public String getFirstName() {
  28. return firstName;
  29. }
  30.  
  31. public void setFirstName(String firstName) {
  32. this.firstName = firstName;
  33. }
  34.  
  35. public String getLastName() {
  36. return lastName;
  37. }
  38.  
  39. public void setLastName(String lastName) {
  40. this.lastName = lastName;
  41. }
  42.  
  43. }

EmployeeDao.java

EmployeeDao interface contains findByIds() method.

 
  1. package com.technicalkeeda.dao;
  2.  
  3. import java.util.Collection;
  4. import java.util.List;
  5. import com.technicalkeeda.bean.Employee;
  6.  
  7. public interface EmployeeDao {
  8.  
  9. public Collection findByIds(List ids);
  10.  
  11. }

EmployeeDaoImpl.java

This is implementation class of EmployeeDao interface. Basically we are using NamedParameterJdbcTemplate to perform the IN clause query in spring jdbc.

Here we pass simple Employee Id list to the IN clause query , Which internaly mapp each id with respected placeholder question mark sign. You don't have to worry about this because all this mapping happen behind the scene.But somehow there is limitation for IN CLAUSE parameters , It allows up-to 1000 parameters but this value is depends upon which database you are using.

 
  1. package com.technicalkeeda.dao;
  2.  
  3. import java.util.Collection;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.sql.DataSource;
  8. import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  9. import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
  10. import com.technicalkeeda.bean.Employee;
  11.  
  12. public class EmployeeDaoImpl implements EmployeeDao {
  13. private DataSource dataSource;
  14. private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
  15.  
  16. public void setDataSource(DataSource dataSource) {
  17. this.dataSource = dataSource;
  18. this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(
  19. this.dataSource);
  20. }
  21.  
  22. @Override
  23. public Collection findByIds(List ids) {
  24.  
  25. Map params = new HashMap();
  26. params.put("ids", ids);
  27.  
  28. List employees = namedParameterJdbcTemplate.query(
  29. "SELECT * FROM trn_employee where employee_id IN (:ids)",
  30. params,
  31. ParameterizedBeanPropertyRowMapper.newInstance(Employee.class));
  32.  
  33. return employees;
  34.  
  35. }
  36.  
  37. }

AppTest.java

This is the Junit test class , which verify the implementation of findByIds() method.

 
  1. package com.technicalkeeda.test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.List;
  6. import org.junit.Test;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9. import com.technicalkeeda.bean.Employee;
  10. import com.technicalkeeda.dao.EmployeeDao;
  11.  
  12. public class AppTest {
  13. @Test
  14. public void testRowMapper() {
  15. ApplicationContext context = new ClassPathXmlApplicationContext(
  16. "spring-beans.xml");
  17.  
  18. EmployeeDao employeeDao = (EmployeeDao) context.getBean("employeeDao");
  19. List ids = new ArrayList();
  20. ids.add("1");
  21. ids.add("2");
  22. ids.add("3");
  23.  
  24. Collection employees = employeeDao.findByIds(ids);
  25.  
  26. for (Employee employee : employees) {
  27. System.out.println("Employee Id:- " + employee.getEmployeeId());
  28. System.out.println("First Name:- " + employee.getFirstName());
  29. System.out.println("Last Name:- " + employee.getLastName());
  30. System.out.println("----------------------------------");
  31. }
  32. }
  33. }

你可能感兴趣的:(JdbcTemplate)