Spring DAO Pattern : Spring DAO « Spring « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Spring » Spring DAO 




Spring DAO Pattern

       
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="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:."/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
    </bean>

    <bean id="abstractJdbcDao" abstract="true" class="org.springframework.jdbc.core.support.JdbcDaoSupport">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="customerDao" class="JdbcCustomerDao" parent="abstractJdbcDao"/>

</beans>


File: Main.java

import java.sql.Types;
import java.util.Date;
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.core.RowMapper;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.SqlQuery;

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");

    CustomerDao customerDao = (CustomerDao)ac.getBean("customerDao");

    customerDao.getAll();
    customerDao.getById(1L);


  }
}
class JdbcCustomerDao extends JdbcDaoSupport implements CustomerDao {
  private SelectCustomerById selectCustomerById;

  private static class SelectCustomerById extends SqlQuery {

      SelectCustomerById(DataSource ds) {
          super(ds, "select * from t_customer where id=?");
          declareParameter(new SqlParameter(Types.INTEGER));
      }

      protected RowMapper newRowMapper(Object[] parameters, Map context) {
          return ParameterizedBeanPropertyRowMapper.newInstance(Customer.class);
      }
  }

  @Override
  protected void initDao() throws Exception {
      this.selectCustomerById = new SelectCustomerById(getDataSource());
  }

  @SuppressWarnings({"unchecked"})
  public List<Customer> getAll() {
      return getJdbcTemplate().query("select * from t_customer", ParameterizedBeanPropertyRowMapper.newInstance(Customer.class));
  }

  public Customer getById(long id) {
      return (Customer)this.selectCustomerById.findObject(id);
  }
}
interface CustomerDao {

  List<Customer> getAll();

  Customer getById(long id);
  
}


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();
  }
}




           
       














Spring-SpringDAOPattern.zip( 3,657 k)
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.