Monday, July 30, 2018

spring codes - 1

<< this is a Data Transfer Object class (our DAO will store and fetch object of DTO to and from the Database) >>

package beans;

public class Person 
{
// data members
private String name, city;

// getters and setters
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

}


<< this is a Data Access Object >>

package dao;

import beans.Person;

public interface PersonDao 
{
        // this DAO will store the object of Person inside the database
public abstract void savePerson(Person person);
}


<< this is the implementation class of DAO >>

package dao;

import org.springframework.jdbc.core.JdbcTemplate;

import beans.Person;

public class PersonDaoImpl implements PersonDao {

        // JdbcTempltate to insert | update | delete | fetch object to from the database
private JdbcTemplate tmp;

@Override
public void savePerson(Person person) {
// create a query
String sql = "insert into person values(?,?)";

// create an array of Object class to specify
// the argument for the query
Object[] queryParam = { person.getName(), person.getCity() };

// exceute this query
tmp.update(sql, queryParam);
}

        // getter and setter for JdbcTemplate
public JdbcTemplate getTmp() {
return tmp;
}

public void setTmp(JdbcTemplate tmp) {
this.tmp = tmp;
}

}

<< the xml file in which we keep the Dependency Injection of beans >>

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
<!-- DI for DriverManagerDataSource -->
<bean name="dm" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/dao"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- DI for DriverManagerDataSource -->
<bean name="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dm"/>
</bean>
<!-- DI for the DAO -->
<bean name="objDao" class="dao.PersonDaoImpl">
<property name="tmp" ref="jt"/>
</bean>
</beans>


<< this is a test application >>


package test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import beans.Person;
import dao.PersonDao;

public class Test01 
{
public static void main(String[] args) 
{
// start container
ClassPathXmlApplicationContext ctx = new 
ClassPathXmlApplicationContext("sao.xml");
// create object of Person
Person p = new Person();
        
        // provide data for person using setters
  p.setName("Nishant");
p.setCity("Mandi Dhanora");
// fetch the object of DAO from the container
PersonDao dao = (PersonDao) ctx.getBean("objDao");
// save the object of person inside database using
// dao
dao.savePerson(p);

        System.out.println("Object saved inside database");
}
}

3 comments: