- Overview
- Apache Tomcat
- Eclipse IDE
- MySQL
- First Project (
spring-demo-one
) - Spring MVC Project (
spring-mvc-demo
) - Hibernate Project (
hibernate-tutorial
)
- Must have the Java Development Kit (JDK) installed.
- Java Application Server: Tomcat
- Java Integrated Development Environment (IDE): Eclipse
- Database Server: MySQL
- Tomcat 10 was released to suppoert Jakarta EE 9. The breaking change for Java EE apps is that it renamed packages
javax.*
tojakarta.*
. - Spring 5 currently doesn't support the new package renaming Jakarta EE 9.
- Use Tomcat 9 for Spring 5 Applications. (Check and track the issues #25354)
- Download and install Tomcat 9 from Tomcat 9 Software Downloads.
- After installation, visit
http://localhost:8080
for verification. - We can start/stop the Tomcat server from services manager in Windows.
- Download and install Eclipse IDE for Java EE Developers from Eclipse IDE Packages.
- Connect Eclipse and Tomcat:
- Move down to the bottom section and click the "Servers" tab.
- Click "No servers are available. Click this link to create a new server...".
- Select "Tomcat v9.0 Server" and click "Next" button.
- Select the Tomcat Installation folder e.g. "C:\Program Files\Apache Software Foundation\Tomcat 9.0".
- Click "Next" and then "Finish" button.
- Now, there is "Tomcat v9.0 Server at localhost" shown in the "Servers" tab.
- Setup Perspective by clicking [Winodws] -> [Perspective] -> [Open Perspective] -> [Java].
- Download and install MySQL from MySQL Community Server Downloads.
- Keep the "Developer Default" and click [Next].
- Check requirements and click [Next].
- Download and then click [Execute] to install products.
- Keep network settings as default.
- Setup database table by loading and running the SQL files.
- Create project by clicking [File] -> [New] -> [Java Project].
- Project name:
spring-demo-one
. - Click "Don't Create" in the dialog for creating
module-info.java
.
- Project name:
- Add library folder named
lib
by right clicking [spring-demo-one
] (Project Name) -> [New] -> [Folder]. - Visit Spring Download and choose [Artifactory] -> [Artifacts].
- Browse the path
libs-release > org > springframework > spring
. - Download the lastest version of
spring-5.x.x-dist.zip
. - Unzip and copy all the
/lib/*.jar
files to the project's/lib
directory.
- Edit project properties by right clicking [
spring-demo-one
] (Project Name) -> [Properties]. - Select [Java Build Path] and click the [Libraries] tab.
- Select [Classpath] and click [Add JARs].
- Select all the
*.jar
files in the/lib
directory. - Click [Close and Apploy].
- Change Perspective by clicking [Winodws] -> [Perspective] -> [Open Perspective] -> [Other]
- Select [Java EE (default)] and click [Open].
- Create project by clicking [File] -> [New] -> [Dynamic Web Project].
- Project name:
spring-mvc-demo
. - Dynamic web module version:
3.0
. - Remove the default
src/main/java
and add folder namedsrc
. - Content directory:
WebContent
.
- Project name:
- Download the lastest version of
spring-5.x.x-dist.zip
from Spring Download. - Unzip and copy all the
/lib/*.jar
files to the project'sWebContent/WEB-INF/lib
directory. - Copy
javax.servlet.jsp.jstl-1.2.1.jar
andjavax.servlet.jsp.jstl-api-1.2.1.jar
to the project'sWebContent/WEB-INF/lib
directory.
- Copy spring-mvc-demo-servlet.xml and web.xml to the project's
WebContent/WEB-INF
directory. - Create the
WebContent/WEB-INF/view/
folder since we define the Spring MVC view resolver with<property name="prefix" value="/WEB-INF/view/" />
.
- Create project by clicking [File] -> [New] -> [Java Project].
- Project name:
hibernate-tutorial
. - Click "Don't Create" in the dialog for creating
module-info.java
.
- Project name:
- Add library folder named
lib
by right clicking [hibernate-tutorial
] (Project Name) -> [New] -> [Folder]. - Download the latest version of Hibernate from Hibernate ORM
- Unzip and copy all the
/lib/required/*.jar
files to the project's/lib
directory. - Download the MySQL JDBC Driver from MySQL Download Connector/J.
- Select Operating System with "Platform Independent".
- Download the ZIP archive file.
- Unzip and copy the
mysql-connector-java-*-bin.jar
files to the project's/lib
directory.
- Edit project properties by right clicking [
spring-demo-one
] (Project Name) -> [Properties]. - Select [Java Build Path] and click the [Libraries] tab.
- Click [Add JARs].
- Select all the
*.jar
files in the/lib
directory. - Click [Close and Apploy].
- Create new package named
com.luv2code.jdbc
. - Create new class named
TestJdbc
with following code under the package. - Run for testing JDBC connection.
package com.luv2code.jdbc;
public class TestJdbc {
public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&serverTimezone=UTC";
String user = "hbstudent";
String pass = "hbstudent";
try {
System.out.println("Connecting to database: " + jdbcUrl);
Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
System.out.println("Connection successful!!!");
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
We can also setup the connection information in the Hibernate config file hibernate.cfg.xml
:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC Database connection settings -->
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&serverTimezone=UTC</property>
<property name="connection.username">hbstudent</property>
<property name="connection.password">hbstudent</property>
<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">1</property>
<!-- Select our SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>
<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>