1. When using Hibernate as an orm application, myeclipse is recommended as a development tool.
2. Import the corresponding Hibernate jar package into the Lib directory under the Webroot file (webroot - > WEB-INF - > lib)
Notes on jar imports in Hibernate development:
(1) Provide a Hibernate jar package download link here, address http://pan.baidu.com/s/1pKCl4EJ (Baidu Yunpan)
(2) After downloading, extract it into a folder
(3) After decompression, the required jar package is copied to the project under the decompressed required package.
(4) You also need to import the jdbc driver package of mysql
(5) Finally, you need to import the jar package of the unit test tool, junit, which also provides a resource download address: link: http://pan.baidu.com/s/1c2F5TYS Password: sqj1
3. Hibernate project process in project development:
[1] Create configuration files for hibernate
[2] Create persistent classes
[3] Creating Object Relational Mapping Files
[4] Write the code to access the database through Hibernate's API
The following four points are explained in detail:
[1] Operation of creating configuration file for hibernate:
Firstly, the template of Hibernate configuration document is found in the decompression package of hibernate. The path is:... Hibernate-release-4.2.21. Final project etc also provides a download link to download the hibernate configuration document here: http://pan.baidu.com/s/1i5orw37
The second step is to configure the basic configuration in the hibernate configuration document. The template is as follows:
<property name="connection.username">root</property>//Configure the username of the database <property name="connection.password">123456</property> //Configure the password of the database <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property> //Configure jdbc to connect mysql <propertyname="connection.url">jdbc:mysql://127.0.0.1:3306/hibernateuse Unicode = true & amp; characterEncoding = UTF-8</property>// Configure the database name of the connection and format it to prevent scrambling <property name="dialect">org.hibernate.dialect.MySQLDialect</property> //Configuring the dialect of hibernate //The configuration of the following three attributes can be replicated directly: <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property>
[2] Create persistent classes
Creating persistent classes follows the design principles of javabean: (1) must be public classes
(2) Attributes use setter and getter methods (3) Provide a public default construction method without parameters (4) Attributes are private
Finally, a construction method with parameters is compiled to facilitate the creation of objects:
[3] Creating Object Relational Mapping Files
In the decompressed hibernate package, enter *. hbm.xml in the search bar, then select one of them named mouse.hbm.xml, copy it to the src directory, and then change it to a. hbm.xml document with the same name as the created persistent class. The purpose of creating an object relationship mapping file is to map the entity class to a table in the database, and map each attribute to the database. A field of the table in the following format:
<hibernate-mapping>
<class name="Students" table="STUDENTS"> <id name="sid" type="int"> <column name="SID"></column> <generator class="assigned"/> </id> <property name="sname" type="java.lang.String"> <column name="SNAME"></column> </property> <property name="gender" type="java.lang.String"> <column name="GENDER"></column> </property> <property name="birthday" type="java.util.Date"> <column name="BIRTHDAY"></column> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS"></column> </property> </class>
</hibernate-mapping>
After creating the post-relational mapping file, this relational mapping file is added to the hibernate.cfg.xml document in the following format:
<session-factory >
<property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="Students.hbm.xml"/> </session-factory>
[4] Write the code to access the database through Hibernate's API
< initialization >
(1) Create a configuration object
(2) Create service Registry objects
(3) Create session factory objects
(4) Create session
(5) Opening a transaction
<Destruction of resources after completion>
(1) Submission
(2) Close the session
(3) Closing Session Factory
<Trial case method>
(1) Generating objects
(2) Save objects using session save method
The code format is as follows:
public class StudentsTest {
private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void init() { //Creating Registration Objects Configuration config = new Configuration().configure(); //Creating Service Registration Objects ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //Creating Session Factory Objects sessionFactory = config.buildSessionFactory(serviceRegistry); //Create session objects session = sessionFactory.openSession(); //Open transaction transaction = session.beginTransaction(); } @After public void destroy() { transaction.commit(); //Submission of affairs session.close(); //Closing session sessionFactory.close(); //Close Session Factory } @Test public void testSaveStudents() { //Generating Student Objects Students s = new Students(1,"Zhang Sanfeng","male",new Date(),"Wudang Mountain"); session.save(s); //Save the object and enter the database }
}
Interested can refer to my small project, address:
Links: http://pan.baidu.com/s/1miLvpJ6 Password: aaax