初识Hibernate

1)首先建一个Java Project

插件自行安装:在官网(http://hibernate.org/orm/)下载

hibernatetools-Update-4.1.1.Final_2013-12-08_01-06-33-B605-1.zip

hibernate-release-4.3.4.Final.zip

  1)如何配置Hibernate环境

  前面已经建了一个Java工程 ,解压hibernate-release-4.3.4.Final.zip

  在目录hibernate-release-4.3.4.Finallib equired中,复制所有的 jar 包

  如图:

      

  将其复制到Java工程的 lib 目录下

  在目录( hibernate-release-4.3.4.Finalprojectetc )中找到  hibernate.cfg.xml 配置文件

  将其复制到src目录下,也就是classpath下

  2)接着关联  http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd

  如图

  

  3)编辑Java文件

JavaBean---User

 1 package com.baidu.hibernate;
 2 
 3 public class User {
 4 
 5     private Integer id;
 6     private String username;
 7     private String password;
 8     private String email;
 9 
10     public Integer getId() {
11         return id;
12     }
13 
14     public void setId(Integer id) {
15         this.id = id;
16     }
17 
18     public String getUsername() {
19         return username;
20     }
21 
22     public void setUsername(String username) {
23         this.username = username;
24     }
25 
26     public String getPassword() {
27         return password;
28     }
29 
30     public void setPassword(String password) {
31         this.password = password;
32     }
33 
34     public String getEmail() {
35         return email;
36     }
37 
38     public void setEmail(String email) {
39         this.email = email;
40     }
41 
42 }
User

单元测试类--TestHibernate

 1 package com.baidu.hibernate;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.Transaction;
 6 import org.hibernate.cfg.Configuration;
 7 
 8 public class TestHibernate {
 9 
10     public static void main(String[] args) {
11 
12         try {
13 
14             SessionFactory sf = new Configuration().configure()
15                     .buildSessionFactory();
16 
17             Session session = sf.openSession();
18 
19             Transaction tx = session.beginTransaction();
20             User user = new User();
21             
22             user.setId(1);
23             user.setUsername("hibernate");
24             user.setPassword("1235");
25             user.setEmail("506666@qq.com");
26             session.save(user);
27             tx.commit();
28 
29             session.close();
30 
31         } catch (Exception e) {
32             e.printStackTrace();
33         }
34     }
35 }
TestHibernate

用eclipse自动生成的--User.hdm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2014-3-14 12:00:57 by Hibernate Tools 3.4.0.CR1 -->
 5 <hibernate-mapping>
 6     <class name="com.baidu.hibernate.User" table="USER">
 7         <id name="id" type="java.lang.Integer">
 8             <column name="ID" />
 9             <generator class="assigned" />
10         </id>
11         <property name="username" type="java.lang.String">
12             <column name="USERNAME" />
13         </property>
14         <property name="password" type="java.lang.String">
15             <column name="PASSWORD" />
16         </property>
17         <property name="email" type="java.lang.String">
18             <column name="EMAIL" />
19         </property>
20     </class>
21 </hibernate-mapping>
User.hbm.xml

配置的--hibernate.cfg.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6         <session-factory>
 7         
 8             <!-- 数据库连接的驱动 -->
 9             <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10             <!-- 连接字符串 -->
11             <property name="connection.url">jdbc:mysql://localhost:3306/hibernate5</property>
12             <!-- 数据库名 -->
13             <property name="connection.username">root</property>
14             <!-- 数据库密码 -->
15             <property name="connection.password">miao</property>
16             <property name="connection.pool_size">1</property>
17             
18             <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
19             <property name="current_session_context_class">thread</property>
20             <property name="cache.provider_class">org.hibernate.cache.NocacheProvider</property>
21             <!-- 显示执行的sql语句 -->
22             <property name="show_sql">true</property>
23             <property name="hbm2ddl.auto">create</property>
24             <!-- 映射文件 -->
25             <mapping resource="com/baidu/hibernate/User.hbm.xml"/>
26             
27         </session-factory>
28 
29 </hibernate-configuration>
hibernate.cfg.xml

                          本人是一个Java爱好者欢迎交流

                                      ----By 小苗

  

  

  

  

原文地址:https://www.cnblogs.com/sxmcACM/p/3602172.html