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. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>SpringExamples</groupId>
  5. <artifactId>SpringExamples</artifactId>
  6. <packaging>war</packaging>
  7. <version>1.0</version>
  8. <name>SpringExamples</name>
  9. <description></description>
  10. <build>
  11. <finalName>SpringExamples</finalName>
  12. <plugins>
  13. <plugin>
  14. <artifactId>maven-compiler-plugin</artifactId>
  15. <configuration>
  16. <verbose>true</verbose>
  17. <compilerVersion>1.6</compilerVersion>
  18. <source>1.6</source>
  19. <target>1.6</target>
  20. </configuration>
  21. </plugin>
  22. </plugins>
  23. </build>
  24.  
  25. <properties>
  26. <spring.version>3.0.5.RELEASE</spring.version>
  27. </properties>
  28.  
  29. <dependencies>
  30. <dependency>
  31. <groupId>junit</groupId>
  32. <artifactId>junit</artifactId>
  33. <version>4.8.1</version>
  34. <scope>test</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>mysql</groupId>
  38. <artifactId>mysql-connector-java</artifactId>
  39. <version>5.1.9</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>log4j</groupId>
  43. <artifactId>log4j</artifactId>
  44. <version>1.2.9</version>
  45. </dependency>
  46.  
  47. <dependency>
  48. <groupId>dom4j</groupId>
  49. <artifactId>dom4j</artifactId>
  50. <version>1.6.1</version>
  51. </dependency>
  52.  
  53. <dependency>
  54. <groupId>commons-logging</groupId>
  55. <artifactId>commons-logging</artifactId>
  56. <version>1.1.1</version>
  57. </dependency>
  58.  
  59. <dependency>
  60. <groupId>commons-collections</groupId>
  61. <artifactId>commons-collections</artifactId>
  62. <version>3.2.1</version>
  63. </dependency>
  64.  
  65. <dependency>
  66. <groupId>cglib</groupId>
  67. <artifactId>cglib</artifactId>
  68. <version>2.2</version>
  69. </dependency>
  70.  
  71. <dependency>
  72. <groupId>asm</groupId>
  73. <artifactId>asm</artifactId>
  74. <version>3.1</version>
  75. </dependency>
  76.  
  77. <dependency>
  78. <groupId>javax.transaction</groupId>
  79. <artifactId>jta</artifactId>
  80. <version>1.1</version>
  81. </dependency>
  82.  
  83. <dependency>
  84. <groupId>org.springframework</groupId>
  85. <artifactId>spring</artifactId>
  86. <version>2.5.6</version>
  87. </dependency>
  88. </dependencies>
  89. </project>

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. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  11. <bean id="dataSource"
  12. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  13. <property name="driverClassName">
  14. <value>com.mysql.jdbc.Driver</value>
  15. </property>
  16. <property name="url">
  17. <value>jdbc:mysql://localhost:3306/technicalkeeda</value>
  18. </property>
  19. <property name="username">
  20. <value>root</value>
  21. </property>
  22. <property name="password">
  23. <value></value>
  24. </property>
  25. </bean>
  26. <bean id="employeeDao" class="com.technicalkeeda.dao.EmployeeDaoImpl">
  27. <property name="dataSource" ref="dataSource" />
  28. </bean>
  29. </beans>

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<Employee> findByIds(List<String> 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<Employee> findByIds(List<String> ids) {
  24.  
  25. Map<String, Object> params = new HashMap<String, Object>();
  26. params.put("ids", ids);
  27.  
  28. List<Employee> 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<String> ids = new ArrayList<String>();
  20. ids.add("1");
  21. ids.add("2");
  22. ids.add("3");
  23.  
  24. Collection<Employee> 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. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值