In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Citation,
Before I came into contact with Java's hibernate framework at school, I felt very powerful. After the holiday, I have time to take a good look at the related technologies of hibernate. Recently I watched the relevant video on imooc and wrote a blog to summarize it.
First, understand:
Hibernate is an ORM (Object Relational Mapping, object-relational mapping) framework that encapsulates JDBC lightweight. It enables programmers to use object-oriented methods to program the database, which makes it easier for us to use Java to do various operations on the database and complete the task of data persistence (I think it is real-time synchronization).
Second, configuration:
Now that we know what Hibernate is and what it does, let's give it a try. But first, configure.
I use Eclipse, under the Java web project:
1. Jar package
Copy all the jar packages required by hibernate to the WEB-INF\ lib directory, and then add build path to the configuration file. The required package can be downloaded from its website, and then add the jar package of the jdbc you want to use.
2. Hibernate.cfg.xml configuration
Create the hibernate.cfg.xml, the config file, in the src directory.
Post a sample configuration code:
True org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/lesson?useUnicode=true&characterEncoding=utf8 root root update
For the above parameters, from top to bottom:
The console prints sql statements, using mysql dialect, using mysql database, database connection, user name root, password root, operation table
There are two entity classes, corresponding to two tables in the database. (entity classes directly correspond to tables in the database)
3 、 TUser.hbm.xml
TUser is the name of one of the entity classes, which corresponds to the user table in the database. A xml configuration file needs to be created for each entity class. Configuration code example:
The corresponding relationship: package- package name. Class- class name, table- database table name. Id- primary key, property name in name- class, column- database table column name. The identity- primary key increment is self-increasing. The property- attribute corresponds to.
The properties of the configuration include single table mapping, one-to-many, many-to-one table mapping. If you have made a two-way configuration, you can use the inverse keyword to achieve control. Adding the mapping of tables makes the relationship between data tables easier to manage, just like adding foreign keys to the database, you can also use parameters such as cascade to make it more convenient.
4. Add tool class HibernateUtil.java
Create a new HibernateUtil class under the util package. Needless to say, go to the code:
Package com.ouc.util;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;public class HibernateUtil {public static SessionFactory sessionFactory Public HibernateUtil () {} static {try {/ * * this method is marked as obsolete in Hibernate4 * / sessionFactory = new / / Configuration () .configure () .buildSessionFactory () / * * method for Hibernate4 to get SessionFactory * / Configuration cfg = new Configuration () .configure (); / / create configuration object ServiceRegistry serviceRegistry = new ServiceRegistryBuilder () .applySettings (cfg.getProperties ()) .buildServiceRegistry () / / create service registration object sessionFactory = cfg.buildSessionFactory (serviceRegistry); / / create session factory} catch (Throwable e) {throw new ExceptionInInitializerError (e);}} public static SessionFactory getSessionFactory () {return sessionFactory;}}
We get the session through sessionFactory, and then we can do all kinds of operations.
3. Use:
HQL (Hibernate Query Language) is used in Hibernate to query, and SQL (Structured Query Language, structured query language) is used in the database to query. The syntax of the two is almost the same.
Write a Test test, the code:
Package com.ouc.util;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import com.ouc.domains.TUser;public class TestUtil {public static void main (String [] args) {Session session = HibernateUtil.sessionFactory.openSession (); String hql= "from TUser"; Query query= session.createQuery (hql); List users=query.list () For (TUser u:users) {System.out.println (u.getId () + "," + u.getUsername () + "," + u.getPassword ());}
The above should pay attention to a problem, debug for a long time-too careless, using hibernate is object-oriented programming, so when looking up the table is not from tableName, but to write from className.
For example, the from TUser used in the above hql reported an error at the beginning of writing from t_user, indicating that there was no mapping. I found that there was nothing wrong with the configuration file, so I thought I made a mistake here.
Look at the results, the data in the database:
Console output result:
Yes, that's right.
V. Summary
After this short period of video learning, although not much nor in-depth, but this is also a preliminary understanding of the Hibernate framework, but also continue to learn! The world of technology is very broad, in order to see the secrets of those technologies, but also for a better tomorrow, we have to keep learning. Come on, good night, boy.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.