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 / srcc: / workspace / My1stHibernate / classesc: / workspace / My1stHibernate / libc: / workspace / My1stHibernate / lib / hibernatec: / workspace / My1stHibernate / lib / db4, the c : / DEV / 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, create a hibernate_test's database with your favorite Database software. 2, under this database, create a new Table called Customer Create Table Custom (CID Integer Not Null Primary Key, Username Varchar (12) Not Null, Password Varchar (12); Write Java files 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;}} C: / Workspace / my1sthibernate / src / customer.java file. Write TEST class import net.sf.hibernate *;. import net.sf.hibernate.cfg *;.. 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 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. XML Version = "1.0"?> Xml version = "1.0"?> / target> project> Configuring Hibernate Description File Hibernate Description Files can be a Properties or XML file, which is the most important thing 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"?>