Program Structure
1. Build a table
This example selects SQL Server2008 as a database to add a ztest database in MySQL to create a CUSTOMER table
CREATE TABLE CUSTOMER ( INTEGERnotNULLPRIMARYKEY, VARCHAR (not NULL, VARCHAR());
2. Create a PO Object
Supplement: Pojo is an abbreviation for plain Ordinaryjava object, which refers to a generic Java object that does not use entity beans, and pojo can be used as an aid class to support business logic. Pojo can be understood as a simple entity class, as the name implies, the role of the Pojo class is to facilitate programmers to use the data tables in the database, Customer.java
Package Com.session;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; }}
3.XML image File
To tell the Hibernate object how to image to a database table, you need to write an XML mapping file named Customer.hbm.xml
Consistent with the corresponding table name, the XML mapping file is as follows
<?XML version= "1.0" encoding= "Utf-8"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.source Forge.net/hibernate-mapping-3.0.dtd "><hibernate-mapping> <classname= "Com.session.Customer"Table= "CUSTOMER"> <IDname= "id"column= "CID"type= "Java.lang.Integer"> <Generatorclass= "Increment" /> </ID> < Propertyname= "username"column= "USERNAME"type= "Java.lang.String"/> < Propertyname= "Password"column= "PASSWORD"type= "Java.lang.String"/> </class></hibernate-mapping>
4. Define Hibernate configuration file
Primarily for sessionfactory configuration, hibernate can be configured using XML or a property file, which is configured using XML
The configuration file name is Hibernate.cfg.xml and can also be named other
<?XML version= "1.0" encoding= "Utf-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hi Bernate.sourceforge.net/hibernate-configuration-3.0.dtd "><hibernate-configuration> <session-factory> <!--SQL Server database driver - < Propertyname= "Hibernate.connection.driver_class">Com.microsoft.sqlserver.jdbc.SQLServerDriver</ Property> <!--MySQL database name - < Propertyname= "Hibernate.connection.url">jdbc:sqlserver://localhost:1433; Databasename=ztest</ Property> <!--login user name for database - < Propertyname= "Hibernate.connection.username">Sa</ Property> <!--login password for the database - < Propertyname= "Hibernate.connection.password">123</ Property> <MappingResource= "Com/session/customer.hbm.xml"/> <!--Note: Must be "/" and not ".". - </session-factory> </hibernate-configuration>
Note: The file header of this file is not the same as the file header in 3, note the third line of the version number do not write wrong
5. Writing the application
Here is a demo program
Package Com.session;import Org.hibernate.hibernateexception;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;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 < i++) { Customer customer = new Customer (); Customer.setusername ("customer" + i); Customer.setpassword ("Customer"); Session.save (customer); } Tx.commit (); Session.close (); } catch (Hibernateexception e) { e.printstacktrace ();}} }
Compile execution Test.java
Execute query statement in SQL Server2008: Select * from CUSTOMER
Results
An article with another 2004-year link: http://blog.csdn.net/doodoofish/article/details/43207/
Good technology will be constantly developed and perfected.
Java Learning notes--the first Hibernate framework Program