I. hibernate introduction Hibernate is an open source object relationship ing framework. It encapsulates JDBC objects in a very lightweight manner, so that Java programmers can use the object programming thinking to manipulate the database as they wish. Hibernate can be used in any situation where JDBC is used. It can be used in Java client programs or Servlet/JSP Web applications. The most revolutionary is that, hibernate can replace CMP in the J2EE architecture of application EJB to fulfill the task of data persistence.
2. hibernate environment build 1. Import hibernate core jar package needs to import hibernate3.jar and lib/required file under all the jar package plus a hibernate-jpa-2.0-api-1.0.1.Final.jar. 2. Add the hibernate. cfg. xml Core configuration file.
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibernate
root
123456
org.hibernate.dialect.MySQLDialect
true
update
Where Is the root of the configuration file, under the session-factory tag Com. mysql. jdbc. Driver Jdbc: mysql: // localhost: 3306/hibernate Root 123456 Is the data source configuration: Driver, url, user name, password Org. hibernate. dialect. MySQLDialect Which database is used to configure the database dialect? True Is to configure whether to print database operation statements Update Default database operations Specifies the ing file between the table and the object to be loaded.
3. Add the xxx ing file xxx. hbm. xml between the table and the object.
3. Seven steps to use hibernate
1. step 1: Create Configuration to read Configuration information: Configuration cfg = new Configuration (). configure (); 2. step 2: Create sessionFactory SessionFactory factory = cfg. buildSessionFactory (); 3. step 3: Open session Session = factory. Z role? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcGVuU2Vzc2lvbigpOwo8aDM + primary/ejrLy01PbJvrLpuMS1yLLZ1/primary" aaa "); user. setPassword (" 123456 "); sessio N. save (user); 6. step 6: Submit the transaction session. getTransaction (). commit (); 7. close the resource, that is, close the session. close (); the first step is to read hibernate by creating a Configuration object. cfg. xml configuration file information to prepare for creating the corresponding session. Step 2: Create the sessionfactory object based on the read configuration file information. The hibernate. cfg. xml file contains the sessionfactory configuration information. In sessinofactory, the data source and some information about database operations are configured, and sessionfactory creates the corresponding session object based on the information. The session object is a handle object for hibernate to operate on the database. It is used to persist data or perform other operations, and has no essential connection with HttpSession. In general, hibernate. cfg. xml configuration file information is mainly used to describe (configure) the session object of the object used during data persistence creation, but it encapsulates configuration and sessionfactory in hibernate, configuration is used to read the configuration file. As the session factory object, sessionfactory creates the corresponding session according to the specified configuration provided by configuration, and then uses the session object to complete data persistence.
Iv. instance code
Hibernate. cfg. xml
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibernate
root
123456
true
org.hibernate.dialect.MySQLDialect
update
User. hbm. xml
User class:
/*****/Package com. zhouxiang. model;/*** @ ClassName: User * @ Description: TODO * @ author zx * @ date 10:40:43 on January 1, May 15, 2014 **/public class User {private String id; private String name; private String password; public User () {} public String getId () {return id;} public void setId (String id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password ;}}
Test class:
/*****/Package com. zhouxiang. test; import org. hibernate. hibernateException; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; import com. zhouxiang. model. user;/*** @ ClassName: Test1 * @ Description: TODO * @ author zx * @ date 10:09:55 on April 9, May 16, 2014 **/public class Test1 {public static void main (String args []) {SessionFactory factory = null; Session ses Sion = null; try {Configuration cfg = new Configuration (). configure (); factory = cfg. buildSessionFactory (); session = factory. openSession (); session. getTransaction (). begin (); User user = new User (); user. setName ("bbb"); user. setPassword ("123456"); session. save (user); session. getTransaction (). commit ();} catch (HibernateException e) {// TODO Auto-generated catch blocke. printStackTrace (); session. getTransacti On (). rollback ();} finally {if (session! = Null) {if (session. isOpen () {session. close ();}}}}}
5. in summary, in short, hibernate only does one thing, that is, it persists the object and object relationships to relational databases, and this ing relationship directly uses object-oriented programming thinking to operate databases, in this way, programmers only need to focus on how to deal with the relationship between objects in the programming process, instead of how objects are persisted into the database. Hibernate enables programmers to focus more on object processing in the software development process, simplifying the data persistence process, speeding up development, and enhancing development efficiency, reduces risks.