3. Hibernate development in Spring
Bromon original, please respect the copyright
Spring is very powerful to Hibernate, from a simple example, from this example we will also discuss the so-called lightweight containers.
First, you need to configure the data source, usually we have two ways to get Connection, one is to write code to get the connection, and the second is to get DataSource from the JNDI environment and then generate a connection. In any case, since it is the object below Spring, it should be registered in the configuration file. Suppose we need a database that connects to Mysql, which is a manual configuration:
Property>
Property>
Property>
Property>
bean>
Is it good to read? If we use the JNDI data source, then DataSource's statement should be:
Property>
bean>
You need to bind a JDBC / SPRINGEXAMER in the JNDI environment, this code has practical meaning. In addition, it is necessary to remind that all Beans declare that its ID must be unique.
In this system, the database operation is encapsulated by the Hibernate, so DataSource does not need to be injected into a specific logic class, which will only be brought to Hibernate's sessionFactory.
According to routine ideas, we need to register with Hibernate in Spring, which should be a class we have written in our own, get DataSource, return SessionFactory, and other logical classes Get the session for database operations.
But we have another choice, Spring provides a package of sessionFactory, you only need to register a Spring your own class, give it a must-must-have, it will return an org.springframework.orm.hibernate.hibernateTemplate, this class Encapsulated add, del, etc., its package is quite high, and it is very simple to write Hibernate applications. But the problem came out, how should we choose? On the surface, using Spring is undoubtedly simpler, but please note that Spring is a lightweight framework, so-called lightweight, an important feature is no invasive, that is, you use this frame, will not be It is bound to manage the classes managed by Spring, you should not need to use its interfaces and abstractions, so your system does not depend on Spring. But if you use the Spring package to operate Hibernate, you must inherit the org.springframework.orm.hibernate.Support.HibernatedAosupport class, which leads to the binding. So do this choice is a bit painful, if one day the Spring frame does not exist, how do your code upgrade maintenance? Specific problems can only be analyzed, in our application, using the HibernateTemplate of Spring package, it is too easy to add, so it is easy to addictive.
Suppose we have a Student table, the structure is simple:
ID automatic growth
Name varchar (40)
Password varchar (32)
Grade Int (4) grade
Sex Boolean Gender (True is male, false is female)
Design a Student class to map this table:
/ *
* Create date 2005-3-17
* /
Package Net.Bromon.spring.examer.pojo;
/ **
* @Author bromon
* /
Public Class Student
{
Private int ID;
PRIVATE STRING NAME;
PRIVATE STRING Password;
Private int grade; // grade
Private boolean sex;
Get / set method ........
}
Write Student.hbm.xml, let Hibernate know how to associate Student tables and Student classes, the file and student.java in the same directory:
id>
clas>
hibernate-maping>
Then we can configure sessionFactory in Spring:
Property>
prOPS>
Property>
list>
Property>
bean>
This is quoted for the DataSource, which is registered, the mappingdirectoryLocations property indicates where the .hbm.xml file is in the path, which is all loaded below .hbm.xml files.
Everything is ready, now we have to join a StudentManager class to add an operation of the check and change:
/ *
* Create date 2005-3-17
* /
Package net.bromon.spring.examer.student;
Import net.bromon.spring.examer.pojo.student;
Import org.springframework.orm.hibernate.HibernateTemplate;
Import org.springframework.orm.hibernate.localsessionFactoryBean;
Import org.springframework.orm.hibernate.support.hibernatedaosupport;
/ **
* @Author bromon
* /
Public Class StudentManager Extends Hibernatedaosupport
{
PRIVATE LOCALESSIONFAACTORYBEAN SessionFactory;
Private hibernateTemplate HT;
Public studentManager ()
{
THIS.HT = Super.GethibernateTemplate ();
}
Public Void Add (Student S)
{
Ht.save (s); // Insert a data only needs this line of code
}
}
This class only demonstrates how to add a Student. HibernateTemplate also encapsulates a lot of useful methods. Please consult the Spring document. The sessionFactory in StudentManager is injected by Spring, but StudentManager does not do anything to SessionFactory because all processing is encapsulated by HibernatedAosupport.GethibernateTemplate (). Any exception handling is also seen in the entire StudentManager, and they are also encapsulated by the base class. The last step is to register StudentManger in Spring and then inject SessionFactory to it:
Property>
bean>
All configurations are completed, and the unit test is made:
/ *
* Create date 2005-3-17
* /
Package net.bromon.spring.examer.student.test;
Import java.io.fileinputstream;
Import org.springframework.beans.Factory.xml.xmlbeanfactory;
Import org.springframework.context.ApplicationContext;
Import org.springframework.context.support.classpathxmlapplicationContext;
Import net.bromon.spring.examer.pojo.student;
Import net.bromon.spring.examer.student.studentManager;
Import junit.framework.testcase;
/ **
* @Author bromon
* /
Public class teststudentmanager extends testcase {
Public void testAdd ()
{
Try
{
ApplicationContext context = new classpathXMLApplicationContext ("Springconfig.xml");
Student S = new student ();
s.setname ("bromon");
S.SETPASSWORD ("123");
s.SETGRADE (3);
S.SetSex (TRUE);
(StudentManager) Context.getBean ("StudentManager")). Add (s);
} catch (Exception E)
{
E.PrintStackTrace ();
}
}
}
Spring has simplified Hibernate's operation to a very high level, the most critical is that the entire development can be driven by design. If a team is enough to be familiar with Spring, then you can plan all classes by designers, finishing clear classes The relationship is written, and then writes the Hibernate mapping file, and the data sheet is associated with the POJO. Members can work in design, using Spring to encapsulate Hibernate template, developed very fast, and easy debugging. It can solve the problem of how to implement design in the team. Since this article does not explain Hibernate's use, please check the Hibernate documentation.