First, Introduction
Hibernate, is an open-source ORM framework. The so-called ORM, the object relative mapping, is the relational mapping of objects.
What is Object relational mapping?
objects, which are Java classes, also known as entity classes, are typically used to hold data;
Relationships: relational databases such as MySQL, Oracle, DB2, and so on are relational databases;
Mapping: Personal understanding of the object and database of table one by one corresponds to the bridge, that is, the entity corresponds to the database table, the attribute corresponds to the table column.
So why do you need hibernate?
Hibernate provides a set of APIs for developers to call, through the API, can simplify the operation of the database, so that developers do not have to write too many SQL statements to complete the operation of the database, such as Save (entity), Hibernate will automatically generate an insert Into statement, and then insert the data within the entity into the database;
Hibernate provides different dialects, different dialects support different relational databases, because different databases are more or less different from each other, if you switch the database so many SQL statements must be modified, so using hibernate without modifying the SQL code, Hibernate itself will generate the appropriate SQL statements in the dialect, so that you can switch databases at will;
Of course, hibernate has a lot of advantages, and of course it's just a good list of advantages.
What do I need to learn to learn hibernate?
In addition to the API that hibernate provides to developers, you need to learn how to configure it!
Why should I configure it?
If you need to insert data from an entity into a database table, how does the database know which table the data is inserted in, and how do you know which column to insert? So the configuration is to correspond the entity to the table specified in the database, and the attributes within the entity correspond to the columns in the table.
Ii. Practice (a small case)
Requirements: Depositing entity user data into the database Sys_user table
Environment:
1. Add a jar Package
HIBERNATE3.JARANTLR-2.7.6. jarcommons-collections-3.1. jardom4j-1.6.1. Jarjavassist-3.12.0. Ga.jarjta-1.1. jarslf4j-api-1.6.1. Jarhibernate-jpa-2.0-api-1.0.0.final.jar
Mysql-connector-java-5.1.5-bin.jar
2. Create Project Project---hibernate with eclipse
3. Create a Lib folder in Classpath, add the above jar package, and add the jar package to the BuildPath
4. Create the Entity class user
Public classUser {Private intID; PrivateString name; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"User [id=" + ID + ", name=" + name + "]"; }}
5. Create a Sys_user table
Create Table Sys_user ( ID int primarykeynotnull auto_increment, name varchar(ten) ) ENGINE =DEFAULT CHARSET=UTF8;
6. Related to database operations, naturally to connect to the database, under Classpath create the Hibernate.cfg.xml file, configured as follows:
<!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hibernate. Org/dtd/hibernate-configuration-3.0.dtd "><hibernate-configuration> <session-factoryname= "Foo"> <!--Configure Database Information - <!--Configure database dialect - < Propertyname= "Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</ Property> <!--Configure the database URL - < Propertyname= "Connection.url">Jdbc:mysql:///hibernate</ Property> <!--Configure database driver (MySQL) - < Propertyname= "Connection.driver_class">Com.mysql.jdbc.Driver</ Property> <!--Configure database user name - < Propertyname= "Connection.username">Root</ Property> <!--Configure Database Password - < Propertyname= "Hibernate.connection.password">1208</ Property> <!--whether to display SQL - < Propertyname= "Hibernate.show_sql">True</ Property> <!--Import a map file This is the correspondence between the entity and the database table - <MappingResource= "Com/hibernate/user.hbm.xml"/> </session-factory></hibernate-configuration>
7. Create a User.hbm.xml mapping file to configure the correspondence between user and Sys_user
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourcefo Rge.net/hibernate-mapping-3.0.dtd "><!--entity class package path -<hibernate-mapping Package= "Com.hibernate"> <!--the name of the entity, the corresponding table name - <classname= "User"Table= "Sys_user"> <!--primary key ID, primary key ID type, column name in table - <IDname= "id"type= "int"column= "id"> <!--Automatic growth - <Generatorclass= "Native"/> </ID> <!--Entity property name, entity attribute type, column name in table - < Propertyname= "Name"type= "string"column= "Name"length= " the"/> </class></hibernate-mapping>
8. Test procedures
Public classTesthibernate {Private Staticsessionfactory sessionfactory; Static{Configuration cfg=NewConfiguration (); //class cfg.configure used to maintain the configuration file ("Hibernate.cfg.xml");//reads the specified master configuration fileSessionfactory = Cfg.buildsessionfactory ();//generate session factory based on configuration file } Public Static voidMain (string[] args) {User u=NewUser (); U.setname ("Save"); Session Session= Sessionfactory.opensession ();//Open a new sessionTransaction tx = Session.begintransaction ();//start a transaction and perform a data rollback after a failed operationSession.save (U);//SaveTx.commit ();//Commit a transactionSession.close ();//Close session, release resources }}
Operation Result:
Console printing: Hibernate:insert into Sys_user (name) VALUES (?)
Run successfully!
Summarize:
Call the Save method and pass in the entity, Hibernate performs operations on the database based on the corresponding entity and data table for the configuration file one by one and generates SQL statements!
1. Learn about hibernate hibernate