Spring MVC Maven Hibernate Mysql CRUD example
CREATE TABLE User (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL DEFAULT '',
lastname varchar(20) NOT NULL DEFAULT '',
email varchar(20) NOT NULL DEFAULT '',
password varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (id)
);
TO-DO:
- Use spring-security for password.
- Use hibernate validation for email
How to create from scratch:
-
New Maven Project Create a simple project (skip archetype selection) com.sample MVCHBRMYSQL 0.0.1 war
-
Project - Properties > Maven > Project Facets Dynamic Web Module 3.0 Java 1.8 Java Script 1.0 Runtimes: Apache Tomcat v7.0
-
Deployment Descriptor - Generate Deployment Descriptor Stub (Project –> RightClick –> Java EE Tools –> Generate Deployment Descriptor Stub) (creates web.xml)
-
Create file: /src/main/webapp/index.jsp
TEST: Run on Server
-
pom.xml > Dependencies > Add
org.springframework spring-core org.springframework spring-webmvc org.springframework spring-web
-
Modify web.xml
7 . New "Spring Bean Definition file" /WEB-INF/root-context.xml Select "beans" for XSD namespace declerations
-
pom.xml > Dependencies > Add
org.springframework spring-context org.springframework spring-tx
-
New "Spring Bean Definition file" /WEB-INF/root-context.xml Select "beans", "context", "mvc", "tx" for XSD namespace declerations
-
New "Spring Bean Definition file" /WEB-INF/dispatcher-servlet.xml Namespaces: beans, context, mvc
-
Add (see dispatcher-servlet) mvc:annotation-driven context:component-scan prefix suffix definitions
-
Create WEB-INF/views/ directory Create WEB-INF/views/index.jsp
-
Create com.sample.controller.WebController class (add "/" section that handles request mapping)
TEST: MVCHBRMYSQL/
- Delete /src/main/webapp/index.jsp
TEST: MVCHBRMYSQL/
Finally, integrate Hibernate: Add user.jsp Add other dependencies Create new classes for model /dao / service Update dispatcher-servlet