diff --git a/basic/jdbc/README.md b/basic/jdbc/README.md index 11f00839..d065f02c 100644 --- a/basic/jdbc/README.md +++ b/basic/jdbc/README.md @@ -4,12 +4,12 @@ Spring Integration - JDBC Sample # Overview This sample provides example of how the Jdbc Adapters can be used. -The example presented covers the following two use cases +The example presented covers the following use cases -* Find a User detail from the database based on the name provided +* Find a Person detail from the database based on the name provided * Create a new Person record in the table -The first example demonstrates the use of outbound gateway to search for a user record using the +The first example demonstrates the use of outbound gateway to search for a person record using the spring integration's jdbc outbound gateway The second example on other hand demonstrates how the jdbc outbound gateway be used to create a new @@ -26,16 +26,12 @@ You can run the application by either - mvn package - mvn exec:java -Make an appropriate choice for searching a User or creating a Person - -For selecting the User, on the command prompt you can enter the following valid values and get a response back: - -* 'a' -* 'b' -* 'foo' +Make an appropriate choice for searching a Person or creating a Person For creating the person record, select the appropriate steps as prompted by the application +On creation of Person records, you may choose the option of selecting the created person records and view their details + #Some details about the sample "Person Outbound Gateway" We use the outbound gateway to insert records in a Person table based on the values contained diff --git a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Main.java b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Main.java index 2a607b88..e338bff4 100644 --- a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Main.java +++ b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Main.java @@ -18,6 +18,7 @@ package org.springframework.integration.samples.jdbc; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; import java.util.Scanner; import org.slf4j.Logger; @@ -25,7 +26,6 @@ import org.slf4j.LoggerFactory; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.samples.jdbc.service.PersonService; -import org.springframework.integration.samples.jdbc.service.UserService; /** @@ -65,7 +65,7 @@ public final class Main { final Scanner scanner = new Scanner(System.in); - final UserService userService = context.getBean(UserService.class); + final PersonService personService = context.getBean(PersonService.class); LOGGER.info("\n=========================================================" @@ -75,14 +75,14 @@ public final class Main { + "\n=========================================================" ); System.out.println("Please enter a choice and press : "); - System.out.println("\t1. Find user details"); + System.out.println("\t1. Find person details"); System.out.println("\t2. Create a new person detail"); System.out.println("\tq. Quit the application"); System.out.print("Enter you choice: "); while (true) { final String input = scanner.nextLine(); if("1".equals(input.trim())) - getUserDetails(scanner,userService); + getPersonDetails(scanner, personService); else if("2".equals(input.trim())) createPersonDetails(scanner,personService); else if("q".equals(input.trim())) @@ -90,8 +90,8 @@ public final class Main { else System.out.println("Invalid choice\n\n"); - System.out.println("Please enter your choice and press : "); - System.out.println("\t1. Find user details"); + System.out.println("Please enter a choice and press : "); + System.out.println("\t1. Find person details"); System.out.println("\t2. Create a new person detail"); System.out.println("\tq. Quit the application"); System.out.print("Enter you choice: "); @@ -145,22 +145,23 @@ public final class Main { * @param service * @param input */ - private static void getUserDetails(final Scanner scanner,final UserService service) { + private static void getPersonDetails(final Scanner scanner,final PersonService service) { while(true) { - System.out.print("Please enter a string and press : "); + System.out.print("Please enter the name of the person and press: "); String input = scanner.nextLine(); - final User user = service.findUser(input); - if (user != null) { - - System.out.println( - String.format("User found - Username: '%s', Email: '%s', Password: '%s'", - user.getUsername(), user.getEmail(), user.getPassword())); - + final List personList = service.findPersonByName(input); + if(personList != null && !personList.isEmpty()) { + for(Person person:personList) { + System.out.print( + String.format("Person found - Person Id: '%d', Person Name is: '%s', Gender: '%s'", + person.getPersonId(),person.getName(), person.getGender())); + System.out.println(String.format(", Date of birth: '%1$td/%1$tm/%1$tC%1$ty'", person.getDateOfBirth())); + } } else { System.out.println( - String.format("No User found for username: '%s'.", input)); - } - System.out.print("Do you want to find another user? (y/n)"); + String.format("No Person record found for name: '%s'.", input)); + } + System.out.print("Do you want to find another person? (y/n)"); String choice = scanner.nextLine(); if(!"y".equalsIgnoreCase(choice)) break; diff --git a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/PersonService.java b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/PersonService.java index 5f5bc27f..d0370f88 100644 --- a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/PersonService.java +++ b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/PersonService.java @@ -15,6 +15,8 @@ */ package org.springframework.integration.samples.jdbc.service; +import java.util.List; + import org.springframework.integration.samples.jdbc.Person; /** @@ -31,5 +33,14 @@ public interface PersonService { * @return */ Person createPerson(Person person); + + /** + * Find the person by the person name, the name search is case insensitive, however the + * spaces are not ignored + * + * @param name + * @return the matching {@link Person} record + */ + List findPersonByName(String name); } diff --git a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/UserService.java b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/UserService.java deleted file mode 100644 index 38d03fa6..00000000 --- a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/UserService.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.integration.samples.jdbc.service; - -import org.springframework.integration.samples.jdbc.User; - -/** - * Provides user services. - */ -public interface UserService { - - /** - * Retrieves a user based on the provided username. - * - * @param username Find users by username - * @return The user if exists, null otherwise. - */ - User findUser(String username); - -} diff --git a/basic/jdbc/src/main/resources/META-INF/spring/integration/spring-integration-context.xml b/basic/jdbc/src/main/resources/META-INF/spring/integration/spring-integration-context.xml index efcd5900..675bfd7c 100644 --- a/basic/jdbc/src/main/resources/META-INF/spring/integration/spring-integration-context.xml +++ b/basic/jdbc/src/main/resources/META-INF/spring/integration/spring-integration-context.xml @@ -8,10 +8,7 @@ http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - - - - + @@ -19,39 +16,40 @@ - - + + + + + + + + + + + request-channel="findPersonRequestChannel" + query="select * from Person where lower(name)=lower(:payload)" + reply-channel="findPersonReplyChannel" row-mapper="personResultMapper" + max-rows-per-poll="100"> - - - - - - - - - - - + +