# Build your first Java SSM project

I have ever built my personal blog by PHP before. However, since I worked in IFLYTEK CO., LTD, I found that its Open Platform's back-end is coded by Java. In detail, it is built by Java SSM framework. What kind of reasons cause this kind of difference? According to some developer's remarks and professional organization's experience, the Java-Based back-end performs better than the PHP-Based, especially in the high concurrence situation.

Here are some comparison between the java-based Web technology and the php-based one:

1. Environment condition: PHP supports Hot Deployment. Once change, it could be seen immediately. Almost all virtual hosts support LAMP ( Linux + Apache + MySQL + PHP) environment. On the contrary, Java need to re-compile after changing and virtual hosts are not friendly to Java. Most of time, you need to configure the environment on your own server (e.g. Linux + Tomcat + MySQL + Java).
2. Layer and structure: See the picture 1 Java SSM framework. Java is geared specifically very large project for its multi-layer structure which is easily to separate front-end and back-end.
3. Speed of developing project: Java is suitable for large project and at the same time its sophisticated structure need to have more time be designed and programmed carefully. However PHP is well adapted for fast development. If you have the right developing environment, you can build a functional system in a flash.
4. IDE support: Java - Intellij IDEA Community ( free ) / Ultimate ( paid ), Eclipse ( free ) . PHP - phpStorm ( paid ), Sublime ( free ), NetBeans ( free ). In my opinion, I think Java IDE is better than the PHP because it is more powerful to configure and expand, more intelligent to complete code and more friendly to developers.
5. Framework: PHP has many famous framework like Laravel, ThinkPHP and etc. Java has SSH ( Struts + Spring + Hibernate ), SSM ( Spring MVC + Spring + Mybatis ), SM ( Spring boot + Mybatis ) and etc. Although two competitors both have various frameworks to choose, PHP's language feature is process-oriented (PHP > 5.0 supports object-oriented but it's not perfect) which is unsuitable to framework actually.
6. Resources occupation and cost: Java occupies much more memory resources and CPU than PHP. The small-scale server usually can't run JVM smoothly. This also illustrates that the developing cost of Java is higher than PHP. It need more humans, more resources and more time to code and maintain.
7. Performance: Java performs much better than PHP.
8. Team work: My tutor in company told that Java is more easily to unify regulation than PHP. As far as I know,Many team work using Alibaba's standard of Java which is de facto standard. And like the reason of PHP's inapplicability of framework, this is caused by its process-oriented feature.

First, we need to talk about what is Java SSM framework?

## 1 Java SSM Framework

SSM = Spring + Spring MVC + Mybatis

The following picture is given to let you have a better understanding of Java SSM Framework.

Picture 1 Java SSM framework

## 2 Overview Our First Java SSM Project

In order to have a better understanding of the SSM Framework, I fork a source code of an simple HR Managing System from Github.

https://github.com/aszx826477/SSM_HRMS

## 2.1 Run this project

Remember to set the related environment configurations properly. The following list shows my own configurations which you can check one by one when you get trouble.

Project management: Maven

Development tool ( IDE ): Intellij IDEA 2018.3.2 (Community Edition)

ItermsEnvironmentVersion
DBMySQL8.0.13
Java Web ContainerTomcat9.0.14
OSWin10 x86/

Table 1 Environment Configuration

[ Warning ] Keep an important eye on the pom.xml because it is relevant to the environment configuration. In this source code pom.xml configuration is right in this ENVIRONMENT ( Table 1 ). However, if change another environment, I cannot promise having the same result.

PS. I have met this problem that the version of mysql-connector-java is incompatible with the version of MySQL. If the version of MySQL is 8.0.13 and the mysql-connector-java must have the version 8.0.13.

<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>

### MySQL configuration

1.Download MySQL installer for windows. Install and make sure Mysql service is running.

3.Configure MyBatis.xml. Set value of username, password, driverClassName and url.

<!-- DriverManagerDataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm_hrms?serverTimezone=GMT%2B8&amp;useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"/>
</bean>

4.Create DATABASE ssm_hrms and create TABLE tbl_emp and TABLE tbl_dept in it.

tbl_emp：

mysql>DROP TABLE IF EXISTS tbl_emp;

mysql>CREATE TABLE tbl_emp(
emp_id int(11) UNSIGNED NOT NULL auto_increment,
emp_name VARCHAR(22) NOT NULL DEFAULT '',
emp_email VARCHAR(256) NOT NULL DEFAULT '',
gender CHAR(2) NOT NULL DEFAULT '',
department_id int(11) NOT NULL DEFAULT 0,
PRIMARY KEY(emp_id)
) ENGINE=INNODB DEFAULT CHARSET=UTF8;

tbl_dept：

mysql>DROP TABLE IF EXISTS tbl_dept;

mysql>CREATE TABLE tbl_dept(
dept_id int(11) NOT NULL DEFAULT 0,
dept_name VARCHAR(255) NOT NULL DEFAULT '',
dept_leader VARCHAR(255) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

### Tomcat

2.User intellij IDEA to open this project by maven. Then the IDE will import all related packages automatically.

3.For the reason of community edition of our IDE, we must download the extra plugin if we want to run the project.

File > Settings > Plugin > Search 'tomcat' > Download 'Smart Tomcat' > Restart IDE

4.Edit configuration > Add new configuration > Smart Tomcat

Then fill the blank of Tomcat Server, selecting your installation dictionary. Click Configuration to check if it's valid. The deployment is the project's webapp content and context path should be set by '/'. The server port(8080), AJP port(8009) and tomcat port(8005) just keep default.

### Test and run

1.Run DepartmentMapperTest.java and EmployeeMapperTest.java to test if the project is configured correctly. If you pass, run the project by tomcat.

2.OK, open the link http://localhost:8080 in Chrome. If you succeed, Congratulations!

## 2.2 Analyse the project structure

|-- java
|   |-- com.hrms
|       |-- bean
|       |-- controller
|       |-- mapper
|       |-- service
|       |-- test
|       |-- util
|-- resources
|   |-- com.hrms.mapper
|   |   |-- DepartmentMapper.xml
|   |   |-- EmployeeMapper.xml
|   |-- applicationContext.xml
|   |-- MyBatis.xml               //mybatis configuration file
|   |-- springmvc.xml             //springmvc configuration file
|-- webapp
|   |-- static
|   |-- WEB-INF
|       |-- jsp


In this Project Tree, we can clearly see the SSM structure built by controller, service and mapper.

Front-end sends request by annotation @RequestMapping links and the controller processes it and return data as result. Mapper contains SQL sentences for CURD (database operation - create, update, retrieve, delete). Service calls the Mapper if it need to operate database.

Webapp contains the jsp for browser display and the framework would auto fetch data from back-end.