Friday, 9 September 2016

Spring Framework In Java(VVI)




Spring MVC,  I think  every  Java developer  wants  to learn Spring framework, because its a "Framework of Fromeworks", Well  this  is  super  framework which provides  loose coupling,

ok,  now  i  am starting  from  simple example of  spring    with  jdbc  template, we  use  jdbc  template  instead  of  JDBC,  you  know  its a  amazing  thing,  lets start  with it then  you will get how much it is interesting,

one  more  thing  you  must  know  about  Spring  theory.ok, I have developed this example in eclipse I have downloaded spring plugins in eclipse and took  as maven project. Here we will not use any jar files but use dependency jars in to pom.xml  files,  please read about maven its very easy. J

we use generally JDBC for database connection  and you know  all exception  in  JDBC are checked Exception So  its Our  responsibility to  open and close connections  and use try catch block  for  exception handling right
Spring JDBC have one abstraction layer on top of existing JDBC technology,
We  work with Spring JDBC and this call to jdbc internally,  so there is no need of  open and close connection it will be taken care by spring. And also this converts  checked exceptios to unchecked exceptions .


Step:-1

Make database
CREATE DATABASE contact_spring;
And  column is id,name,email and mobile

Step:-2
Put all this dependencies in to your pom.xml file
<dependencies>
    <!-- Spring core & mvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

    <!-- CGLib for @Configuration -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>${cglib.version}</version>
        <scope>runtime</scope>
    </dependency>


    <!-- Servlet Spec -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
   
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.30</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.0.3.RELEASE</version>
</dependency>

</dependencies>




Step:-3
Make Bean class

package com.nee.spring.modal;

public class Customer {

     private int id;
     private String name;
     private String email;
     private String mobile;

     public Customer(int id, String name, String email, String mobile) {
           super();
           this.setId(id);
           this.setName(name);
           this.setEmail(email);
           this.setMobile(mobile);
     }
// here getter and setter methods
}


Step:-4

package com.nee.spring.modal;

import org.springframework.jdbc.core.JdbcTemplate;

public class CustomerDao {

     private JdbcTemplate jdbTemplate;

     public JdbcTemplate getJdbTemplate() {
           return jdbTemplate;
     }

     public void setJdbTemplate(JdbcTemplate jdbTemplate) {
           this.jdbTemplate = jdbTemplate;
     }

     public int saveEmployee(Customer e) {
           String query = "insert into contacts values( '" + e.getId() + "','"
                     + e.getName() + "','" + e.getEmail() + "','" + e.getMobile()
                     + "')";

           return jdbTemplate.update(query);
     }

     public int updateEmployee(Customer e) {
           String query = "update contacts set name='" + e.getName() + "',EMAIL='"
                     + e.getEmail() + "' where id='" + e.getId() + "' ";

           return jdbTemplate.update(query);
     }

     public int deleteEmployee(Customer e) {
           String query = "delete from contacts where id='" + e.getId() + "' ";
           return jdbTemplate.update(query);
     }
}

Step:-5


<?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:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
 
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
<property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
<property name="url" value="jdbc:mysql://localhost:3306/contact_spring" /> 
<property name="username" value="neeraj" /> 
<property name="password" value="neeraj" /> 
</bean> 
 
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
<property name="dataSource" ref="ds"></property> 
</bean> 
 
<bean id="edao" class="com.nee.spring.modal.CustomerDao"> 
<property name="jdbTemplate" ref="jdbcTemplate"></property> 
</bean> 
 
</beans> 

Step:-6

package com.nee.spring.modal;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {
            public static void main(String[] args) {

                        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                                                "applicationContext.xml");

                        CustomerDao customerDao = (CustomerDao) applicationContext
                                                .getBean("edao");

                        int status = customerDao.saveEmployee(new Customer(101,
                                                "Neeraj Srivastava", "neeraj.javadeveloper@gmail.com",
                                                "9876543210"));
                        System.out.println(status);

            }
}

Finally right click on project and run as java application and check your database you will get values are inserted into databse, this is just simple example of Spring MVC, I will come with web application  also. Good Luck Friends

If any query then send me mail at neeraj.javadeveloper@gmail.com

Thanks

Keep Happy Learning

2 comments:

  1. I am a regular reader of your blog and much impressed in your way of written on Java recent updates. I would like to share with my friends. Thank you admin.
    Regards:
    Best JAVA Training institute in Chennai
    Java Courses in Chennai

    ReplyDelete
  2. As technology grows, we have to be updated in our domain to sustain in the IT industry. Your post made me impressed in the way of your writing.
    Big Data Training in Chennai | Big Data Training

    ReplyDelete