Hibernate preliminary, hibernate
Hibernate development steps
1. Create a new project and import related packages
Mainly hibernate and mysql JAR packages.
Note: whether the newly imported hibernate-related JAR package is compatible with your current jdk version, and do not forget to import the database driver JAR package. For example, 5.2 of hibernate requires JDK to be compiled.
2. Design of POJO persistence class ------- classes associated with database tables by javabean.
public class UserInfo { private int id; private String name; private String password; private String role; public UserInfo() { } public UserInfo(String name, String password, String role) { super(); this.name = name; this.password = password; this.role = role; } public UserInfo(int id, String name, String password, String role) { super(); this.id = id; this.name = name; this.password = password; this.role = role; } public int getId() { return id; } public void setId(int 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; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } @Override public String toString() { return "UserInfo [id=" + id + ", name=" + name + ", password=" + password + ", role=" + role + "]"; } }
3. Compile the *. hbm. xml ing file, which mainly describes the ing between the object class and the tables in the database.
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4. Compile the hibernate configuration file ------- database connection information and display the underlying SQL statements.
<! --~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later .~ See the lgpl.txt file in the root directory or
5. Use the hibernate api to operate the database, which includes the following steps.
1) obtain the Configuration object.
2) obtain the SessionFactory object.
3) obtain the Session and open the transaction.
4) operate databases in an object-oriented manner.
5) Close the transaction and close the Session.
Public class HibernateUtils {// insert data information into the database. Public static void save (Object obj ){
1) obtain the Configuration object. Configuration config = new Configuration (); config. configure ("hibernate. cfg. xml ");
2) obtain the SessionFactory object. SessionFactory sessionFactory = config. buildSessionFactory ();
3) Obtain Session session = sessionFactory. openSession ();
3) Open the transaction. Transaction tx = session. beginTransaction ();
4) operate databases in an object-oriented manner. Session. save (obj );
5) Close the transaction and close the Session. Tx. commit (); session. close ();} public static void deleteById (int id) {Configuration config = new Configuration (); config. configure ("hibernate. cfg. xml "); SessionFactory sessionFactory = config. buildSessionFactory (); Session session = sessionFactory. openSession (); Transaction tx = session. beginTransaction (); UserInfo userInfo = session. get (UserInfo. class, id); session. delete (userInfo); tx. commit (); session. close ();} public static void update (Object obj) {Configuration config = new Configuration (); config. configure ("hibernate. cfg. xml "); SessionFactory sessionFactory = config. buildSessionFactory (); Session session = sessionFactory. openSession (); Transaction tx = session. beginTransaction (); session. update (obj); tx. commit (); session. close ();} public static Object findObjById (Class c, Serializable id) {Configuration config = new Configuration (); config. configure ("hibernate. cfg. xml "); SessionFactory sessionFactory = config. buildSessionFactory (); Session session = sessionFactory. openSession (); Object obj = session. get (c, id); session. close (); return obj ;}}6. Test class
public class IDUQTest { @Test public void testSave(){ UserInfo userInfo = new UserInfo("lili","123","admin"); HibernateUtils.save(userInfo); } @Test public void testDelete(){ HibernateUtils.deleteById(4); } @Test public void testUpdate(){ UserInfo userInfo = (UserInfo) HibernateUtils.findObjById(UserInfo.class, 3); userInfo.setPassword("123456"); HibernateUtils.update(userInfo); }}
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hibernate running process
Hibernate runs as follows:
1) The application first calls the Configuration class, which reads the information in the Hibernate Configuration file and the ing file, and generates a SessionFactory object with the information.
2) generate a Session object from the SessionFactory object
3) generate a Transaction object using the Session object
4) You can use get (), load (), save (), update (), delete (), and saveOrUpdate () of the Session object () to load, save, update, delete, and perform other operations on the POJO class;
5) When querying, you can use the Session object to generate a Query object, and then use the Query object to perform the Query operation. If there is no exception, the Transaction object will submit these operations to the database.
// End