Hibernate, an O / R mapping product, entry is very easy, starting today, I have made an example. Post it out! The environment is as follows: Developed IDE: JBuilderx Used Database: My SQL 4.0 Database Drive: MySQL JDBC Driver
1. The web application Hib.jpx is established under the JBX. 2.Project Properties-> Paths-> Required libraries-> add-> New here define hibernate class library to put all JAR packages under Hibernate's LIB in course, there is also hibernate2.jar; new definition mysql's mysql JDBC Driver JAR package in. 2. Join Hibernate and MySQL in Required Libraries in the properties. 3. Establish a corresponding file, the detailed structure is as follows: --- HiB | | ----- HiB (Auto Generate below) | ----- SRC | ----- HiB | ----- Create.java | ----- Insert.java (Insert Record to Person Table) | ----- Person.java | --Mysql.txt (Person Table SQL Script) | ----- Person.hbm.xml (Mapping File) | ----- Hibernate.properties (found from Hibernate's SRC, you want to modify) | ---log4j.properties (found under SRC from Hibernate) 4. Original files are as follows: Create.javaPackage Hib;
import net.sf.hibernate.Session; import net.sf.hibernate.Transaction; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.tool.hbm2ddl.SchemaExport ;
/ ** * This class is only used to create a table, and it is not inserted into any data inside the table and can only be used once, otherwise it will delete the existing table * / public class create {private static sessionFactory sessionFactory;
Public static void main (string [] args) throws exception {configuration conf = new configuration (); addclass (person.class);
// The first time running is used to create a table / / in the database and output the SQL statement to the TXT file // will not use the segment code, otherwise, each time you will delete the original table, then create the table Schemaexport dbexport = new schemaexport (conf); dbexport.setoutputfile ("mysql.txt"); dbexport.create (true, true);}} insert.javaPackage HIB;
import net.sf.hibernate.Session; import net.sf.hibernate.Transaction; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.tool.hbm2ddl.SchemaExport Public class insert {private static sessionFactory sessionFactory;
public static void main (String [] args) throws Exception {Configuration conf = new Configuration () addClass (Person.class);. sessionFactory = conf.buildSessionFactory (); Session s = sessionFactory.openSession ();
Transaction t = s.begintransaction ();
Person Yuj = New Person (); YUJ.SetName ("john"); YUJ.SetAddress ("Shanghai");
Person x = new person (); x.setname ("zhaoyh"); X.Setaddress ("Shanghai");
// Persistence S.Save (YUJ); // At this point, YUJ can already be found in the database; // At this time, X can already be found in the database.
T.CMmit (); sclose ();}}.
Public Class Person {Private String ID; Private String Name; Private String Address;
Public void setId (string value) {this.id = value;}
Public string getId () {return id;}
Public void setname (string value) {this.name = value;
Public string getname () {return name;}
Public void setaddress (String value) {this.address = value;
public String getAddress () {return address;}} hibernate.propertieshibernate.dialect net.sf.hibernate.dialect.MySQLDialecthibernate.connection.driver_class org.gjt.mm.mysql.Driverhibernate.connection.url jdbc: mysql: /// test ? useUnicode = true & characterEncoding = gb2312hibernate.connection.username roothibernate.connection.password Person.hbm.xml xml version = "1.0" encoding = "GB2312"?>