1. Download page
Http://in.relation.to/Bloggers/HibernateORM422FinalReleased
2. Unzip hibernate and copy all the jar packages under the required directory under the lib directory to the class loading path in our application. If it is a web application, put it under/WEB-INF/lib/.
3. If you need to use the database source, the c3p0 is also placed under/WEB-INF/lib/
4. Create a persistent class. Instances of this class will be mapped to the corresponding instance in the database by hibernate:
| The code is as follows: |
Copy code |
Package org. Rudiment. hibernate; Public class News { Private Integer id; Private String title; Private String content; Public Integer getId (){ Return id; } Public void setId (Integer id ){ This. id = id; } Public String getTitle (){ Return title; } Public void setTitle (String title ){ This. title = title; } Public String getContent (){ Return content; } Public void setContent (String content ){ This. content = content; } } |
5. Configure the exclusive ing file News. hbm. xml for the News class.
| The code is as follows: |
Copy code |
<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <Hibernate-mapping package = "org. Rudiment. hibernate"> <! -- The table corresponding to this News class is news_table --> <Class name = "News" table = "news_table"> <! -- IDs in this class will be mapped to the primary key of the corresponding database as identifiers --> <Id name = "id"> <Column name = "ID"/> <Generator class = "native"/> </Id> <! -- The title in the declaration class News will be mapped to the database --> <Property name = "title"/> <! -- The content in the declaration class News will be mapped to the database --> <Property name = "content"/> </Class> </Hibernate-mapping> |
6. hibernate framework configuration/WEB-INF/classes/hibernate. cfg. xml
| The code is as follows: |
Copy code |
<! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd> <Hibernate-configuration> <Session-factory> <! -- Configure database dialect --> <Property name = "hibernate. dialect"> org. hibernate. dialect. MySQLDialect </property> <! -- Configure the database driver --> <Property name = "hibernate. connection. driver_class"> com. mysql. jdbc. Driver </property> <! -- Configure the database username --> <Property name = "hibernate. connection. username"> root </property> <! -- Configure the database password --> <Property name = "hibernate. connection. password"> root </property> <! -- Configure the database url --> <Property name = "hibernate. connection. url"> jdbc: mysql: // localhost: 3306/failure </property> <! -- Configure the maximum capacity of the data pool --> <Property name = "hibernate. c3p0. max_size"> 20 </property> <! -- Configure the minimum capacity of the data pool --> <Property name = "hibernate. c3p0. min_size"> 1 </property> <! -- Configure the timeout threshold for data links --> <Property name = "hibernate. c3p0. timeout"> 5000 </property> <! -- Display whether the executed SQL is printed in the background on the console --> <Property name = "hibernate. show_ SQL"> true </property> <! -- Whether to display printed SQL statements in a friendly format --> <Property name = "hibernate. format_ SQL"> true </property> <! -- Print some auxiliary comments --> <Property name = "hibernate. use_ SQL _comments"> true </property> <Property name = "hibernate. c3p0. max_statements"> 100 </property> <Property name = "hibernate. c3p0. idle_test_period"> 3000 </property> <Property name = "hibernate. c3p0. acquire_increment"> 2 </property> <Property name = "hibernate. c3p0. validate"> true </property> <! -- Configure the data operation method --> <Property name = "hbm2ddl. auto"> update </property> <! -- Add the ing file of News above --> <Mapping resource = "org/Rudiment/hibernate/News. hbm. xml"/> </Session-factory> </Hibernate-configuration> |
7. Write a class for testing:
| The code is as follows: |
Copy code |
Package org. Rudiment. hibernate; Import org. Rudiment. hibernate. News; Import org. hibernate. Session; Import org. hibernate. SessionFactory; Import org. hibernate. Transaction; Import org. hibernate. cfg. Configuration; Import org. hibernate. service. ServiceRegistry; Import org. hibernate. service. ServiceRegistryBuilder; Public class NewsManager { Public static void main (String [] args) { // Obtain the configuration Configuration conf = new Configuration (). configure (); // Obtain ServiceRegistry ServiceRegistry sr = new ServiceRegistryBuilder () . ApplySettings (conf. getProperties ()) . BuildServiceRegistry (); // Configure our SessionFactory SessionFactory sf = conf. buildSessionFactory (sr ); // Obtain our Session Session sess = sf. openSession (); // Start our transaction Transaction tx = sess. beginTransaction (); News n = new News (); N. setTitle ("ITkezhan "); N. setContent ("IT Inn "); // Save the persistent object Sess. save (n ); // Submit the transaction Tx. commit (); // Close the Session Sess. close (); // Close SessionFactory Sf. close (); } } |
8. After running NewsManager, you can see that there is a new table news_table in The hibernate database as follows: (create the hibernate database in MySQL in advance. Otherwise, the hibernate database will not be automatically created)
| The code is as follows: |
Copy code |
Mysql> use hibernate; Database changed Mysql> select * from news_table; + ---- + --------------- + ------------------------- + | ID | title | content | + ---- + --------------- + ------------------------- + | 1 | ITkezhan | IT inn | + ---- + --------------- + ------------------------- + 1 row in set (0.00 sec) |