|
In fact, a couple of months ago in terms of knowledge of Hibernate, but has been no good summary, and has been using myeclipse, feeling some fool-style operation can be erected Hibernate development environment, but so little not good, do not understand in the end is how to Hibernate configuration, so you special use today to build step by step Hibernate Eclipse development environment, following the formal entry to the topic.
Create a new web project, whose name it whatever you like what the name what it
Introduced hibernate dependent jar package, I am using hibernate-release-4.3.10.Final, After downloading unpack, require open file under the lib directory under the archive folder, which is to hibernate So must the jar package Next, in just the new project in a new libs folder you just said jar package copy into the other, because we need to connect to the MySQL database and the use of JUnit test, the required mysql-connector-java- 5.0.8-bin.jar and junit-4.5.jar into two jar package references about these jar package, you can search online. Next, is to add these jar package to the build environment to go, check the jar under the package libs, right click and select Build Path -> Add to Build Path, so put the dependent jar package successfully added into it.
Continue down, we need to configure the most important hibernate configuration file hibernate.cfg.xml well log4j.properties property file log processing: Open the hibernate file step unpacked folder, open the project-> etc folder, the file folder under the hibernate.cfg.xml and log4j.properties files are copied to the project src folder, opens the hibernate.cfg.xml file to replace the session-factory tag content into the following topics:
< Session-factory>
< ! - Configure mysql database connection parameters ->
< Property name = "hibernate.dialect"> org.hibernate.dialect.MySQLDialect < / property>
< ! - Driver Name ->
< Property name = "hibernate.connection.driver_class"> com.mysql.jdbc.Driver < / property>
< ! - Database name ->
< Property name = "hibernate.connection.url"> jdbc: mysql: /// hibernatedemo < / property>
< ! - Username ->
< Property name = "hibernate.connection.username"> root < / property>
< ! - Password ->
< Property name = "hibernate.connection.password"> yzp140103 < / property>
< / Session-factory>
This is configured.
Next thing to do is do development test: a new entity class com.joe.entity package in the src directory of the project, a new entity class Student in the packet, as follows:
package com.joe.entity;
import java.io.Serializable;
public class Student implements Serializable {
/ **
*
* /
private static final long serialVersionUID = 5548279324472937805L;
private int id;
private String name;
private int age;
/ **
* Affirming no-argument constructor
* /
public Student () {
}
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public int getAge () {
return age;
}
public void setAge (int age) {
this.age = age;
}
}
By the way, note, you must declare a no-argument constructor, this is a must Oh!
The key question is, we are not saying do hibernate data persistence it? That is to say the one hundred and interact with the database chant for Student class, relational database and how it, then we have to talk about this object-relational mapping files Xxx.hbm.xml files, where Xxx is the name of the entity class, the contents of our Student.hbm.xml file is:
< ? Xml version = "1.0"?>
< ! DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
< Hibernate-mapping>
< -! Tag a class corresponding to an entity class, name attribute specifies the name of the entity class, table attribute specifies the table associated with the database ->
< Class name = "com.joe.entity.Student" table = "stu_tab">
< ! - Primary key ->
< Id name = "id" column = "stu_id">
< ! - The primary key generation strategy ->
< Generator class = "native"> < / generator>
< / Id>
< ! - Other attributes, name corresponding to the attributes of the entity type, column corresponding column of relational database tables ->
< Property name = "name" column = "stu_name"> < / property>
< Property name = "age" column = "stu_age"> < / property>
< / Class>
< / Hibernate-mapping>
The file also added to the com.joe.entity package, so the list? Of course the answer is no, we have to register the entity class to hibernate.cfg.xml, after session-factory label below the last add a property tag:
< ! - Add Student.hbm.xml mapping files ->
< Mapping resource = "com / joe / entity / Student.hbm.xml" />
Then go down, we have to test, create a new resource in the project folder test, and then test the new file in the test package com.joe.test, StudentTest.java a new class in this package, and write the following code:
package com.joe.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
import com.joe.entity.Student;
public class StudentTest {
/ **
* @Test Comment is way junit4 test affirms
* Generate DDL files based on object-relational mapping
* /
@Test
public void createTable () {
Configuration cfg = new Configuration () configure ().;
SchemaExport se = new SchemaExport (cfg);
se.create (true, true);
}
}
Of course, you can also use the following ways to generate DDL, the new asking price in the hibernate.cfg.xml:
< ! - Generated DDL Configuration ->
< Property name = "hibernamr.hbm2ddl.auto"> create | update < / property>
Finally, the implementation StudentTest.java category, select Run As-> JUnit Test, the console can see the following information, it would prove a success
Of course, you can also open MySQL, further validation.
Well, hibernate development environment even if formally completed structures. |
|
|
|