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. Unfailed 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 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% in the PATH environment variable. 3. Create a project directory, such as C: WorkspaceMy1stHibernate. 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: Workspacemy1sthibernate C: Workspacemy1sthibernateSRC C: WorkspaceMy1sthibernateClasses C: WorkspaceMy1sthibernateLib C: Workspacemy1sthibernateLibHibernate C: Workspacemy1SthibernateLibdb 4, put the C: DEV Copy your JDBC Driver file (typically a JAR file) Copy to C: WorkspaceMy1sthibernateLibdb. 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: WorkspaceMy1sthibernateSrccusTomer.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 C: Workspacemy1sthibernateSrctest.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. XML Version = "1.0"?>
"- // hibernate / hibernate mapping dtd // en" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> id> clas> hibernate-maping> This file is stored as C: WorkspaceMy1sthibernateSrccuSrcuSpace.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. XML Version = "1.0"?> fileset> path> target> fileset> copy> target> target> delete> target> provject> 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. XML Version = "1.0" encoding = "UTF-8"?>
PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> Oracle.jdbc.driver.racledriver Property> JDBC: Oracle: OCI8: @Hibernate_test Property> Your database username proty> Your database password Property> Net.sf.hibernate.dialect.Orcle9DiaLect Property> session-factory> Hibernate-Configuration> If you are not Oracle 9i, you can go to C: DEV start operation Under C: Workspacemy1Sthibernate, 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.