The simplest introduction in Hibernate in history

zhaozj2021-02-16  62

In fact, Hibernate itself is a separate framework that does not require any support for Web Server or Application Server. However, most of the Hibernate introductions have added a lot of non-Hibernate, such as Tomcat, Eclipse, Log4j, Struts, XDoclet, and even JBoss. This is easy to generate hibernate complex misunderstandings, especially the enthusiasm of the initiator.

In this article will not involve Eclipse, Log4j, Struts, Tomcat, XDoclet, and JBoss. The purpose of this article is to demonstrate the Hibernate installation process and the most basic function, thus giving the beginners a low gate threshold.

download file

You need Java SDK, Hibernate Package, ANT Pack, and JDBC Driver.

1, Hibernate package download address:

Http://prdownloads.sourceforge.net/hibernate/?sort_by=date&sort=desc

2, ANT package download address:

http://apache.130th.net/ant/binaries/apache-ant-1.6.1-bin.zip

3, JDBC Driver To set according to the Database you used, there will be on the Database official website. Hibernate supports common Database, such as MySQL, Oracle, PostgreSQL, and MS-SQL Server. These databases have JDBC Driver:

Oracle JDBC Driver download address (must agree to Oracle Agreement before downloading)

Http://otn.racle.com/software/htdocs/distlic.html?/software/tech/java/sqlj_jdbc/htdocs/jdbc9201.html

MySQL JDBC DRIVER download address

http://dev.mysql.com/downloads/connector/j/3.0.html

PostgreSQL JDBC DRIVER download address

http://jdbc.postgreSQL.org/download.html

MS-SQL Server JDBC DRIVER download address

http://www.microsoft.com/downloads/details.aspx?familyid=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&displaylang=en

4. Differentiate the Hibernate package and the ANT package to C: / DEV / (this directory is not important, you can change any other directory).

Configuration Environment

1. You need to add a new environment variable: Ant_home, let it point to the C: / dev /

2, you need to add a new environment variable: java_home, let it point to your J2SDK root directory. And add% java_home% / bin in the PATH environment variable.

3. Create a project directory, such as C: / Workspace / My1Sthibernate.

In the project directory, create three directories: SRC, Classes, LIB.

In the lib directory, create two directories: Hibernate and DB.

This way you have the following file structure:

C: / Workspace / My1Sthibernate /

C: / Workspace / My1Sthibernate / SRC

C: / Workspace / MY1STHIBERNATE / CLASSES

C: / Workspace / My1Sthibernate / LIB

C: / Workspace / My1Sthibernate / lib / hibernate

C: / Workspace / MY1STHIBERNATE / LIB / DB4, directory C: / dev / /Hibernate2.jar file Copy to C: / Workspace / my1sthibernate / lib / hibernate.

The same copy to C: / Workspace / my1sthibernate / lib / hibernate will be on the directory> / lib / 你.

Copy your JDBC Driver file (typically a JAR file) Copy to C: / Workspace / My1Sthibernate / lib / dB.

Create a database

1. Use your favorite Database software to create a hibernate_test's database.

2, under this database, create a new Table called Customer

Create Table Customer

(

CID Integer Not Null Primary Key,

Username varchar (12) Not null,

Password varchar (12)

);

Write a Java file

Public class customer {

Private int ID;

PRIVATE STRING UserName;

PRIVATE STRING Password;

Public int getId () {

Return ID;

}

Public string getpassword () {

Return Password;

}

Public string getUsername () {

Return UserName;

}

Public void setid (int id) {

THIS.ID = ID;

}

Public void setpassword (string password) {

this.password = password;

}

Public void setusername (String username) {

THIS.USERNAME = UserName;

}

}

This class is stored as a C: /Workspace/my1sthibernate/src/customer.java file.

Write TEST class

Import net.sf.hibernate. *;

Import net.sf.hibs. *;

Public class test {

Public static void main (String [] args) {

Try {

SessionFactory sf =

New Configuration (). configure (). BuildSessionFactory ();

Session session = sf.opensession ();

Transaction tx = session.begintransaction ();

For (int i = 0; i <200; i ) {

Customer Customer = New Customer ();

Customer.SetUsername ("Customer" i);

Customer.SetPassword ("Customer");

Session.save (CUSTOMER);

}

TX.comMit ();

session.close ();

} catch (hibernateException e) {

E.PrintStackTrace ();

}

}

}

This class is stored as a C: /Workspace/my1sthibernate/src/test.java file.

Create a Hibernate mapping file

Because there is only one class - Customer and a table --- Customer, you only need to create a mapping file - Customer.hbm.xml, to correspond to the relationship between the Customer class and the Customer table.

"- // hibernate / hibernate mapping dtd // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

This file is stored as C: /Workspace/my1sthibernate/src/customer.hbm.xml, and Customer.java is placed in the same directory.

Write an Ant Build.xml file

You don't have to know the details of this build.xml, actually Ant is not Hibernate. Here are Ant to simplify some tasks, such as compilation, COPY, running, etc.

">

$ {src.dir} "{src.dir}"

Configure hibernate description files

The Hibernate description file can be a Properties or XML file, which is most important to define the connection of the database. I am listed here is an XML format hibernate.cfg.xml description file.

PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN"

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

True

Oracle.jdbc.driver.racledriver

JDBC: Oracle: OCI8: @Hibernate_test

Your database username

Your database password

Net.sf.hibernate.dialect.Orcle9DiaLect

If you are not Oracle 9i, you can find your database in the directory> /src/hipernate.properties file where you have a C: / DEV /

start operation

Under C: / Workspace / My1Sthibernate, run Ant Run. If you strictly follow the steps, you should see

Run:

[Java] log4j: warn no appenders could be found for logger (net.sf.hibernate.cfg.environment).

[Java] log4j: war4j system prot all

[Java] Hibernate: INSERT INTO CUSTOMER (Username, Password, CID) VALUES (?,?,?)

Build Successful

Take a look at your hibernate_test database, add 200 records in the CustMor table, but you have not written any JDBC CODE.

In the future, if you want to change the database, you only need to change the corresponding value in the hibernate.cfg.xml description file.

in conclusion

This article is a very low gateway introduction. I gave a friend who didn't understand Hibernate. He used his first Hibernate program for less than 30 minutes, which has caused him to interested Hibernate. However, the reader must recognize that this is just a beginning, and this article is a small ice crystal on a hibernate iceberg. A journey of a thousand miles begins with a single step, you can regard this article as a starting point towards Hibernate Avenue.

转载请注明原文地址:https://www.9cbs.com/read-15795.html

New Post(0)