Spring_2. MySql. Hibernate.
In this second part of our tutorial we will provide the integration of Hibernate into the project. First of all, we have to create the database. We chosen, as usual, MySql: it’s in general the most free and complete db over the market.
We need to create a simple db to store information about some business or functional model.
Ok, imagine we want create a db where user can register itself and store a configuration of a domotic system (something sounding like home automation).
The database will contain the following tables:
user, with user general information;
address, with addresses user information;item, with domotic system general information;
Following, the creation script of the database, where at the end we will put some data to retrieve information in the browser.
create database Sql628449_2; use Sql628449_2;
create table user
(id int not null primary key auto_increment,first_name varchar(50),last_name varchar(50),email varchar(30),phone_mobile varchar(20));………
insert into item (id_user, system_id, system_description, system_configuration) values (3, 'CCCC_00', 'system 3', 'xml----xml');
GRANT ALL PRIVILEGES ON *.* TO 'Sql628449_2'@'localhost' IDENTIFIED BY 'Sql628449_2' WITH GRANT OPTION;
The full script is available in the download section.
Now we have, after launching the MySql service, and executed the script a
schema called Sql628449_2
with a user Sql628449_2 / Sql628449_2,
and having 4 rows in the user table.
Now we’ll make the hibernate layer. In detail we want an architecture with spring MVC as general framework, JPA as general persistence framework and hibernate as ORM and MySql as database.
Hence the first step, is to add the right dependencies into pom. In our case
I showed the dependencies for explanatory purpose but the pom is the same of part 1.
And now, the poject, let’s start with resources.
The applicationContext.xml has to be modified to contanins the setting relative to database. A good way to do that is to separate the configuration file and include it in the applicationContext.xml.
So we create database-context.xml and include it in applicationContext.xml.The file will contain the datasource, the EntityManagerFactoryBean, the jpaPropertyMap, the Persistence and transaction setting. See it in the whole project.
The database connection parameters are strored in applicationResources.properties. By means of
put in the applicationContext.xml we read the properties.
In the package it.extremetechnologies.Spring_Hibernate_Maven add he following
entity, dao, service.In the entity package add the following java classes (mapping between tables and java classes):UserEntity to the package entity,
AddressEntity to the package entity,ItemEntity to the package entity.In the dao package add the interface UserDAO and its implementation.In the service package add the Service_1 interface and its implementation.
Summarizing the entities map the database table, the dao manage the operations to the db and the service is a whole-life web app component.
From functional point of view, this step of the tutorial, provides a simple count of the records contained in the user table.
To achieve this goal we need to map an url to a functionality provided from Service_1 and show the results into browser.
OK, starting from the database we have:
- the table user containing the rows
- the UserEntity entity that maps the table
- the UserDAO manages the entities-the service Service_1 manages, in this case just one DAO, UserDAO; and Service_1 provides all time the data to consumers of data.- the jsp page_1.jsp manage data retrieved from Service_1 and show formatted information to the screen.
Among other informations the page will provide
where service_1 is the bean name of the service and userDAO is its internal instance of the unique DAO, obtained by means of @Autowired (a spring implementation of DI).
OK, and what about the url that map our resource ?
In the HomeController.java you can see something like that @Autowired
private Service_1 service_1;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String listEmployees(ModelMap map)
where you can notice the dependency injection of Service_1, and another spring annotation: RequestMapping.
This way we ask spring to map the “/“ url to what we put inside the method, in our case we add to the model the full instance of the the service. But this is just an example, is not common practice add the whole service to the model.
By the way is responsibility of the jsp dig into the hierarchy and retrieve the right information.
After built the project and deployed it to Tomcat, run the broser with http://127.0.0.1:8080/Spring_Hibernate_Maven/ (if you deployed to the local machine) you’ll see something like: