Javatpoint Logo

Spring MVC with Hibernate

By: krupah*** On: Sun Jul 26 02:39:38 IST 2015     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
Hi ,

Could you please share example for Spring MVC with Hibernate.

Many thanks in advance.

Regards,
Krupaharan M
Up0Down

 
Step#1: Create the CONTACTS Table

01
CREATE TABLE CONTACTS
02
(
03
id int(10) unsigned NOT NULL AUTO_INCREMENT,
04
name varchar(45) NOT NULL,
05
address varchar(45) DEFAULT NULL,
06
gender char(1) DEFAULT 'M',
07
dob datetime DEFAULT NULL,
08
email varchar(45) DEFAULT NULL,
09
mobile varchar(15) DEFAULT NULL,
10
phone varchar(15) DEFAULT NULL,
11
PRIMARY KEY (id)
12
);

Step#2: Copy the SpringMVC, Hibernate and their dependent jars into WEB-INF/lib folder.
If you are using Maven you can mention the following dependencies.

001
<dependencies>
002
<dependency>
003
<groupid>junit</groupid>
004
<artifactid>junit</artifactid>
005
<version>4.8.1</version>
006
<type>jar</type>
007
<scope>compile</scope>
008
</dependency>
009
<dependency>
010
<groupid>org.springframework</groupid>
011
<artifactid>spring-web</artifactid>
012
<version>3.0.5.RELEASE</version>
013
<type>jar</type>
014
<scope>compile</scope>
015
</dependency>
016
<dependency>
017
<groupid>org.springframework</groupid>
018
<artifactid>spring-core</artifactid>
019
<version>3.0.5.RELEASE</version>
020
<type>jar</type>
021
<scope>compile</scope>
022
<exclusions>
023
<exclusion>
024
<artifactid>commons-logging</artifactid>
025
<groupid>commons-logging</groupid>
026
</exclusion>
027
</exclusions>
028
</dependency>
029
<dependency>
030
<groupid>log4j</groupid>
031
<artifactid>log4j</artifactid>
032
<version>1.2.14</version>
033
<type>jar</type>
034
<scope>compile</scope>
035
</dependency>
036
<dependency>
037
<groupid>org.springframework</groupid>
038
<artifactid>spring-tx</artifactid>
039
<version>3.0.5.RELEASE</version>
040
<type>jar</type>
041
<scope>compile</scope>
042
</dependency>
043
<dependency>
044
<groupid>jstl</groupid>
045
<artifactid>jstl</artifactid>
046
<version>1.1.2</version>
047
<type>jar</type>
048
<scope>compile</scope>
049
</dependency>
050
<dependency>
051
<groupid>taglibs</groupid>
052
<artifactid>standard</artifactid>
053
<version>1.1.2</version>
054
<type>jar</type>
055
<scope>compile</scope>
056
</dependency>
057
<dependency>
058
<groupid>org.springframework</groupid>
059
<artifactid>spring-webmvc</artifactid>
060
<version>3.0.5.RELEASE</version>
061
<type>jar</type>
062
<scope>compile</scope>
063
</dependency>
064
<dependency>
065
<groupid>org.springframework</groupid>
066
<artifactid>spring-aop</artifactid>
067
<version>3.0.5.RELEASE</version>
068
<type>jar</type>
069
<scope>compile</scope>
070
</dependency>
071
<dependency>
072
<groupid>commons-digester</groupid>
073
<artifactid>commons-digester</artifactid>
074
<version>2.1</version>
075
<type>jar</type>
076
<scope>compile</scope>
077
</dependency>
078
<dependency>
079
<groupid>commons-collections</groupid>
080
<artifactid>commons-collections</artifactid>
081
<version>3.2.1</version>
082
<type>jar</type>
083
<scope>compile</scope>
084
</dependency>
085
<dependency>
086
<groupid>org.hibernate</groupid>
087
<artifactid>hibernate-core</artifactid>
088
<version>3.3.2.GA</version>
089
<type>jar</type>
090
<scope>compile</scope>
091
</dependency>
092
<dependency>
093
<groupid>javax.persistence</groupid>
094
<artifactid>persistence-api</artifactid>
095
<version>1.0</version>
096
<type>jar</type>
097
<scope>compile</scope>
098
</dependency>
099
<dependency>
100
<groupid>c3p0</groupid>
101
<artifactid>c3p0</artifactid>
102
<version>0.9.1.2</version>
103
<type>jar</type>
104
<scope>compile</scope>
105
</dependency>
106
<dependency>
107
<groupid>org.springframework</groupid>
108
<artifactid>spring-orm</artifactid>
109
<version>3.0.5.RELEASE</version>
110
<type>jar</type>
111
<scope>compile</scope>
112
</dependency>
113
<dependency>
114
<groupid>org.slf4j</groupid>
115
<artifactid>slf4j-api</artifactid>
116
<version>1.6.1</version>
117
<type>jar</type>
118
<scope>compile</scope>
119
</dependency>
120
<dependency>
121
<groupid>org.slf4j</groupid>
122
<artifactid>slf4j-log4j12</artifactid>
123
<version>1.6.1</version>
124
<type>jar</type>
125
<scope>compile</scope>
126
</dependency>
127
<dependency>
128
<groupid>cglib</groupid>
129
<artifactid>cglib-nodep</artifactid>
130
<version>2.2</version>
131
<type>jar</type>
132
<scope>compile</scope>
133
</dependency>
134
<dependency>
135
<groupid>org.hibernate</groupid>
136
<artifactid>hibernate-annotations</artifactid>
137
<version>3.4.0.GA</version>
138
<type>jar</type>
139
<scope>compile</scope>
140
</dependency>
141
<dependency>
142
<groupid>jboss</groupid>
143
<artifactid>javassist</artifactid>
144
<version>3.7.ga</version>
145
<type>jar</type>
146
<scope>compile</scope>
147
</dependency>
148
<dependency>
149
<groupid>mysql</groupid>
150
<artifactid>mysql-connector-java</artifactid>
151
<version>5.1.14</version>
152
<type>jar</type>
153
<scope>compile</scope>
154
</dependency>
155
</dependencies>

Step#3: Configure SpringMVC

a) Configure DispatcherServlet in web.xml
01
<servlet>
02
<servlet-name>dispatcher</servlet-name>
03
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
04
<load-on-startup>1</load-on-startup>
05
</servlet>
06

07
<servlet-mapping>
08
<servlet-name>dispatcher</servlet-name>
09
<url-pattern>*.do</url-pattern>
10
</servlet-mapping>
11

12
<listener>
13
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
14
</listener>
15
<context-param>
16
<param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param>

b) Configure View Resolver in WEB-INF/dispatcher-servlet.xml

1
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver" p:prefix="/jsp/" p:suffix=".jsp">
2
Image Created0Down

By: [email protected] On: Sun Jul 26 16:37:52 IST 2015 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
Dear all,

Many thanks for your reply.

Regards,
Krupaharan M
Image Created0Down

By: [email protected] On: Fri Jul 31 10:21:19 IST 2015 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
Hi All ,

Many thanks for your response

Regards,
Krupaharan M
Image Created0Down

By: [email protected] On: Sat Aug 01 16:33:18 IST 2015 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No