Extends MappingSqlQuery With Parameters : MappingSqlQuery « Spring « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Spring » MappingSqlQuery 
28. 44. 2. Extends MappingSqlQuery With Parameters

File: 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"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">

<!--
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@oracle.devcake.co.uk:1521:INTL"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>
-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:."/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
    </bean>
<!--
    <bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://dbhost-prospring-psql/prospring"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>
-->
    <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">
        <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
    </bean>

    <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>


</beans>

File: Main.java

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.object.MappingSqlQueryWithParameters;

class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml", Main.class);
    DataSource dataSource = (DataSourceac.getBean("dataSource");
    //DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");


    MappingSelectCustomer mappingSelectCustomer = new MappingSelectCustomer(dataSource);
    List result = mappingSelectCustomer.execute(new Date());

  }
}
class MappingSelectCustomer extends MappingSqlQueryWithParameters {
  private final static String SQL = "select * from t_customer";
  String LAST_LOGIN_DATE = "last_login";

  MappingSelectCustomer(DataSource ds) {
      super(ds, SQL);
  }

  protected Object mapRow(ResultSet rs, int rowNum, Object[] parameters, Map contextthrows SQLException {
      Customer customer = new Customer();
      customer.setId(rs.getLong("id"));
      customer.setFirstName(rs.getString("last_name"));
      customer.setLastName(rs.getString("first_name"));
      customer.setLastLogin(rs.getDate("last_login"));
      if (rs.wasNull()) customer.setLastLogin(null);
      if (context != null) {
          if (context.containsKey(LAST_LOGIN_DATE)) customer.setLastLogin((Date)context.get("lastLogin"));
      }
      return customer;
  }

  public List execute(Date defaultLastLoginDate) {
      Map<String, Object> context = new HashMap<String, Object>();
      context.put(LAST_LOGIN_DATE, defaultLastLoginDate);
      return execute(context);
  }
}


class Customer {
  private Long id;
  private String firstName;
  private String lastName;
  private Date lastLogin;
  private String comments;

  public Long getId() {
      return id;
  }

  public void setId(Long id) {
      this.id = id;
  }

  public String getFirstName() {
      return firstName;
  }

  public void setFirstName(String firstName) {
      this.firstName = firstName;
  }

  public String getLastName() {
      return lastName;
  }

  public void setLastName(String lastName) {
      this.lastName = lastName;
  }

  public Date getLastLogin() {
      return lastLogin;
  }

  public void setLastLogin(Date lastLogin) {
      this.lastLogin = lastLogin;
  }

  public String getComments() {
      return comments;
  }

  public void setComments(String comments) {
      this.comments = comments;
  }

  @Override
  public String toString() {
      final StringBuilder sb = new StringBuilder();
      sb.append("Customer");
      sb.append("{id=").append(id);
      sb.append(", firstName='").append(firstName).append('\'');
      sb.append(", lastName='").append(lastName).append('\'');
      sb.append(", lastLogin=").append(lastLogin);
      sb.append(", comments='").append(comments).append('\'');
      sb.append('}');
      return sb.toString();
  }
}
  Download:  Spring-ExtendsMappingSqlQueryWithParameters.zip( 3,657 k)
28. 44. MappingSqlQuery
28. 44. 1. Extends MappingSqlQuery
28. 44. 2. Extends MappingSqlQuery With Parameters
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.