Java

Tuesday, 20 November 2012

spring mvc spring provided hibernate example



Spring mvc-spring provided hibernate example:



TUTIORALS FROM MADHAV:
 

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY






Index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
       <meta http-equiv="pragma" content="no-cache">
       <meta http-equiv="cache-control" content="no-cache">
       <meta http-equiv="expires" content="0">   
       <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
       <meta http-equiv="description" content="This is my page">
       <!--
       <link rel="stylesheet" type="text/css" href="styles.css">
       -->
  </head>
 
  <body bgcolor="wheat">
    <a href="dept.spring"> click hear to call dept controller</a> <br>
  </body>
</html>


Success.jsp



<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'success.jsp' starting page</title>
   
       <meta http-equiv="pragma" content="no-cache">
       <meta http-equiv="cache-control" content="no-cache">
       <meta http-equiv="expires" content="0">   
       <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
       <meta http-equiv="description" content="This is my page">
       <!--
       <link rel="stylesheet" type="text/css" href="styles.css">
       -->

  </head>
 
  <body bgcolor="wheat">
   <h3></h3> record inserted successfully</h3> <br>
  </body>
</html>



Department.java



package madhav.dao;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Department {
           @Id
           private int deptno;
           private String dname;
           private String loc;
           public int getDeptno() {
                       return deptno;
           }
           public void setDeptno(int deptno) {
                       this.deptno = deptno;
           }
           public String getDname() {
                       return dname;
           }
           public void setDname(String dname) {
                       this.dname = dname;
           }
           public String getLoc() {
                       return loc;
           }
           public void setLoc(String loc) {
                       this.loc = loc;
           }
}


TestSupport.java

package madhav.dao;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class TestSupport {

                private HibernateTemplate hibernateTemplate;
                private SessionFactory sessionFactory;
               
               
           public SessionFactory getSessionFactory() {
                                return sessionFactory;
                }


                public void setSessionFactory(SessionFactory sessionFactory) {
                                this.sessionFactory = sessionFactory;
                }


                                public void execute()
           {
                   try
                   {
                                     // Create the SessionFactory from hibernate.cfg.xml
                                    /* SessionFactory  sf= new
                                     AnnotationConfiguration().configure().buildSessionFactory();
                                     Session s=sf.openSession();
                                     Transaction tx=s.beginTransaction();*/
                                   
                                   hibernateTemplate=new HibernateTemplate(sessionFactory);
                                   
                                     Department d=new Department();
                                     d.setDeptno(78);
                                     d.setDname("hugo");
                                     d.setLoc("hyd");
                                     hibernateTemplate.save(d);
                                   
                                    
                                     } catch (Throwable ex) {
                                     // Make sure you log the exception, as it might be swallowed
                                     System.err.println("Initial SessionFactory creation failed." + ex);
                                 
                                     }
                                     }

           }           


DeptController.java

package madhav.controller;

import madhav.dao.TestSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DeptController {
                @RequestMapping(value="/dept.spring")
                public String createDept()
                {
                                 ApplicationContext beanfactory;
         beanfactory=new ClassPathXmlApplicationContext("applicationContext.xml");
                   TestSupport ts=(TestSupport)beanfactory.getBean("sessionfact");
                   ts.execute();
                   System.out.println("execution over");
       
                               
                                return "success.jsp";
                }
}



Web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
       xmlns="http://java.sun.com/xml/ns/j2ee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
       http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID">
       <display-name>speingmvcacc</display-name>
 
  <servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>*.spring</url-pattern>

</servlet-mapping>
<welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


Dispatcher-servlet.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

       <context:component-scan base-package="madhav.controller" />

       </beans>


applicationContext.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: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="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mysql"/>
<property name="username" value="root"/>
<property name="password" value="mysql"/>
</bean>

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>madhav.dao.Department</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>

      
       <bean id="sessionfact" class="madhav.dao.TestSupport">
    <property name="sessionFactory" ref="mySessionFactory"/>
   </bean>
       </beans>




Output:





No comments:

Post a Comment