Lao Kang: Java Open source project Hibernate Quick Start

Source: Internet
Author: User
Tags copy file copy sql mysql postgresql jboss root directory log4j
Quick Start | project
Author: Old Kang

  In fact, hibernate itself is a stand-alone framework that does not require the support of any Web server or application server. However, most hibernate introductory introductions have added a lot of hibernate things, such as Tomcat, Eclipse, Log4j,struts, XDoclet, and even JBoss.   This is easy to produce hibernate complex and difficult to understand the misunderstanding, especially the impact of the enthusiasm of beginners. 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 as well as the most basic functions, so as to give beginners a low can not be lower entry threshold.  Download files You need Java SDK, hibernate package, ant package, 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 according to your database to set, general database on the official website will have. Hibernate supports commonly used database, such as MySQL, Oracle, PostgreSQL, and Ms-sql Server. These databases all have jdbc driver:oracle jdbc Driver download address (Oracle agreement must be agreed before downloading) http://otn.oracle.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, Hibernate and ant packets are extracted separately 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\< your ANT package directory.  and add%ant_home%\bin to the PATH environment variable. 2, you need to add a new environment variable: java_home, let it point to your J2SDK root directory.  and add%java_home%\bin to the PATH environment variable.  3, create a project directory, such as C:\workspace\My1stHibernate.  In the project directory, create three additional directories: SRC, classes, lib.  In the Lib directory, create two directories: Hibernate and DB. So you have the following file structure: C:\workspace\my1sthibernate\c:\workspace\my1sthibernate rcc:\workspace\my1sthibernate\classesc:\ Workspace\my1sthibernate\libc:\workspace\my1sthibernate\lib\hibernatec:\workspace\my1sthibernate\lib\db 4, will C:\  Dev\< your Hibernate package directory >\hibernate2.jar file copy to C:\workspace\My1stHibernate\lib\hibernate.  Copy all files under the directory >\lib\ your hibernate package to c:\workspace\My1stHibernate\lib\hibernate under c:\dev\<. Copy your JDBC driver file (typically a jar file) to c:\workspace\My1stHibernate\lib\db.
C:\workspace\my1sthibernate\c:\workspace\my1sthibernate rcc:\workspace\my1sthibernate\classesc:\workspace\ My1sthibernate\libc:\workspace\my1sthibernate\lib\hibernatec:\workspace\my1sthibernate\lib\db 4, will c:\dev\  The directory of your hibernate package >\hibernate2.jar files copy to C:\workspace\My1stHibernate\lib\hibernate.  Copy all files under the directory >\lib\ your hibernate package to c:\workspace\My1stHibernate\lib\hibernate under c:\dev\<.  Copy your JDBC driver file (typically a jar file) to c:\workspace\My1stHibernate\lib\db.  Create database 1, with your favorite database software, create a hibernate_test. 2. Under this database, create a new table named Customercreate table CUSTOMER (CID INTEGER NOT null PRIMARY KEY, USERNAME VARCHAR (a) not NULL, PASSWO  RD VARCHAR (12)); Write Java file public class Customer {private int id; private string username; private string password. public int getId () {RE Turn ID; public string GetPassword () {return password.} public string GetUserName () {return username;} T id) {this.id = ID;} public void SetPassword (String password) {this.password = password;} PUBlic void Setusername (String username) {this.username = username;}}  Save this class as a c:\workspace\My1stHibernate Rc\customer.java file. Write test class import Net.sf.hibernate.*;import net.sf.hibernate.cfg.*;p ublic 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 i++) {Customer customer = new Customer ();    Customer.setusername ("customer" + i);    Customer.setpassword ("Customer");   Session.save (customer);   } tx.commit ();  Session.close ();  catch (Hibernateexception e) {e.printstacktrace ();  Save this class as a c:\workspace\My1stHibernate Rc\test.java file. Create 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"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping dtd//en" "Http://hibernate.sourceforge. Net/hibernate-mapping-2.0.dtd ">

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.