Category: Development

DAO Layer & Service Manager Layer No ratings yet.

All DAO classes will extend the GenericDao.java, which includes some common database actions, such as saveOrUpdate() and findByID(). Besides, it is also using to auto-wired the sessionFactory: package com.itblogs.dao; import java.util.List; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Order; import org.hibernate.Criteria; import org.springframework.beans.factory.annotation.Autowired; /**  * @author      Kelvin Hui  * @see              it-blogs.com  *

MODEL : Create Hibernate ORM Models No ratings yet.

We will create five model classes mapping to our database tables, please download here: Java Class Files Table Name Address.java ADDRESS Customer.java CUSTOMER Orders.java ORDERS Product.java PRODUCT Shop.java SHOP Nil   We don’t need to create a model class for this table. But, we will use @ManyToMany annotation in Orders.java to state this join table

Define Database Schema and Hibernate Configuration No ratings yet.

Our example will demonstrate three different table relationships: One-to-One One-to-Many Many-to-Many You can download the schema.sql here, and run it with MySQL client. /* create database */ CREATE DATABASE itblogs DEFAULT CHARACTER SET utf8;   USE itblogs;   /* One-to-One relationship */ /* SHOP – ADDRESS */ CREATE TABLE ADDRESS( id BIGINT NOT NULL AUTO_INCREMENT,

Updating Web.xml and Dispatch-servlet.xml No ratings yet.

Before running the app, you need to setup the web.xml, dispatcher-servlet.xml and other property files properly. Let’s update the web.xml file by right-click src/main/webapp/WEB-INF/web.xml => Open With Text Editor and then update with below settings:   <?xml version=”1.0″ encoding=”UTF-8″?> <web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=”WebApp_ID” version=”2.5″>        <display-name>SpringMVCExample</display-name>        <welcome-file-list>        <welcome-file>index.html</welcome-file>    

Spring Configuration and ServletInitizer No ratings yet.

Starting from Spring 3.1, you can run the app without web.xml and dispatch-servlet.xml. Simply storing all your settings in Spring configuration and servlet initizer, it helps to simplify the web configuration with Spring. Let’s create a Spring configuration file called AppConfiguration.java under src/main/java/com/itblogs/config/ as: package com.itblogs.config; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import

Project Folder Structure No ratings yet.

Let’s create some necessary folders: 1.       src/main/java/com/itblogs/config/ 2.       src/main/java/com/itblogs/controller/ 3.       src/main/java/com/itblogs/dao/ 4.       src/main/java/com/itblogs/dao/impl/ 5.       src/main/java/com/itblogs/model/ 6.       src/main/java/com/itblogs/service/ 7.       src/main/java/com/itblogs/service/impl/ 8.       src/main/java/com/itblogs/utils/ 9.       src/main/resources/ 10.   src/main/resources/messages/ 11.   src/main/webapp/resources/css/ 12.   src/main/webapp/resources/js/ 13.   src/main/webapp/resources/img/ 14.   src/main/webapp/WEB-INF/jsp/ 15.   src/main/webapp/WEB-INF/lib/ 16.   src/test/java/     Folder Structure: Folder Details src/main/java/com/itblogs/config/ Store Spring configuration files src/main/java/com/itblogs/controller/ Controller Java classes src/main/java/com/itblogs/service/ Service Manager

Update POM.xml with necessary Jar Version No ratings yet.

Under the project, you can find pom.xml file. Right-click this file, and Open With Text Editor:   Adding below dependencies: <project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”   xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”>   <modelVersion>4.0.0</modelVersion>   <groupId>com.itblog</groupId>   <artifactId>SpringMVCExample</artifactId>   <packaging>war</packaging>   <version>0.0.1-SNAPSHOT</version>   <name>SpringMVCExample Maven Webapp</name>   <url>http://maven.apache.org</url>   <properties>        <spring.version>4.2.0.RELEASE</spring.version>     <hibernate.version>4.3.11.Final</hibernate.version>     <javax.validator.version>1.1.0.Final</javax.validator.version>     <hibernate.validator.version>5.2.2.Final</hibernate.validator.version>     <mysql.connector.version>5.1.31</mysql.connector.version>