In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to integrate Spring and Hibernate, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
1. Introduction
Recently, I participated in the background software design and development of the audit system of a bank in North America. In addition to using Web Service, we integrated Spring and Hibernate to access database data through Spring's HibernateTemplate. It is well known that Hibernate is a widely used and powerful data persistence layer technology. On the other hand, as a dependency injection framework supporting IOC, Spring has the advantage of being able to integrate most of the mainstream technologies very well. In this article, we will discuss how to integrate Spring and Hibernate.
Basics of 2.Spring and Hibernate
Before we actually get into integrating these two technologies, let's understand this integration requirement. We all know that Hibernate is a powerful ORM tool that sits between the application and the database. It enables applications to access data from various databases in an independent platform, and it is not necessary for applications to rely on low-level DBC details such as managing connections, dealing with statements and result sets. All the details of accessing a specific data source can be easily configured in the Xml file. Another advantage is that Hibernate and J2SE and J2EE applications are very compatible.
One of the problems with using Hibernate is that customer applications that use Hibernate Framework to access databases must rely on Hibernate APIs such as Configuration, SessionFactory and Session. These objects continue to spread in the application code. And the application code must maintain and manage these objects manually. But in the Spring environment, business objects can be configured with the help of IOC. In short, an object state can be separated from the application code. It means that it is now possible to use Hibernate objects as Spring Beans, and they can get all the convenience provided by Spring.
3. Integrated Sample
We do not intend to study the various API integrated in the Spring package, we want to learn and understand these APIs through the example source code. The following sections cover each step of integrating Spring-Hiberante with a detailed explanation.
3.1. Create a database
The following application uses an Oracle database. After installation, log in as an administrator and create a user schema,username and password, then log in as a user and run the following SQL statement in SQL command or SQL Script: create table Activitylog (id varchar (10), name varchar (20), taskcode varchar (3), tasktime timestamp); now an empty table has been created.
3.2.ActivityLog class
Now let's create code called Activitylog POJO to store data fetched from the Activitylog table, designed so that the column names of table 'Activitylog' will be mapped to variable names of the appropriate type of Java class Activitylog. It can be generated using the ant tool or plug-in. The complete code of the Activitylog class is listed below:
Package com.xxx.audit.pojo
Public class Activitylog {
Private String id
Private String name
Private String taskcode
Private Timestamp tasktime
Public Activitylog () {
}
Public String getId () {
Return id
}
Public void setId (String id) {
This.id = id
}
Public String getName () {
Return name
}
Public void setName (String name) {
This.name = name
}
Public String getTaskcode () {
Return taskcode
}
Public void setTaskcode (String taskcode) {
This.taskcode = taskcode
}
Public Timestamp getTasktime () {
Return tasktime
}
Public void setTasktime (Timestamp tasktime) {
This.tasktime = tasktime
}
Public String toString () {
Return "Id =" + id + ", Name =" + name + ", Taskcode ="
+ taskcode + ", Tasktime =" + tasktime
}
}
3.3. Create a Hibernate Mapping file
We have created the 'Activitylog' table and a corresponding Java class class in the application layer in the database. We need to create a Hibernate Mapping file to map the' Activitylog' table to the 'Activitylog' Java class,' and the column name of the Activitylog' table to the variables of the 'Activitylog' Java class, which can be generated using the ant tool or plug-in. Let's take a look at the Hibernate Mapping file:
Activitylog.hbm.xml
Note that the mapping file is a Xml file and its name is Activitylog.hbm.xml. The 'hbm'' in the file name indicates that it is an Hibernate mapping file. The class tag defines the mapping between database tables and Java classes. The 'name' attribute must point to a full-path Java class name, whereas the table attribute must point to the database table, and the next tag defines the mapping between the column name and the corresponding java variable. The' id' tag defines the identity of a row as the primary key, and the property tag has an attribute called 'name' pointing to the Java variable name, followed by the column name of the database table it maps.
3.4. Create a Spring profile
This section deals with the information needed to configure the various Spring. In Spring, all business objects are configured in the Xml file, and the configured business objects are called Spring Beans. These Spring Beans can be maintained through IOC. Let's define a data source as follows:
Spring-hibernate.xml
?
The above bean defines a data-source of type 'org.apache.commons.dbcp.BasicDataSource'. More importantly, it defines the various connection properties required to access the database.
This is the answer to the question on how to integrate Spring and Hibernate. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.