`

Hibernate 学习笔记(一)

 
阅读更多

注:记录了使用Hibernate向数据库插入一条数据的简单示例。仅作为个人学习记录,如果对你有所帮助,不胜荣幸。

 

1. New  Java Project      命名为  myhibernate

 

 

2. 添加所需要的jar包(不知各个jar包有何用,但是却是我花费了N久,在解决多次报错下形成的)

 

1)antlr-2.7.6.jar

2)commons-collections-3.1.jar

3)commons-lang-2.4.jar

4)dom4j-1.6.1.jar

5)ejb3-persistence.jar

6)hibernate-commons-annotations-3.2.0.Final.jar

7)hibernate-core-3.6.3.Final.jar

8)hibernate-search-3.4.0.Final.jar

9)javassist.jar

10)jta.jar

11)lucene-core-3.1.0.jar

12)slf4j-api-1.6.1.jar

13)slf4j-nop-1.6.1.jar

14)sqljdbc4.jar  (这里使用SQL Server数据库)

右键,Add to Build Path 。

 

 

 

 

 

3.在src目录下建立hibernate配置文件 hibernate.cfg.xml (Eclipse下有Hibernate插件HibernateTools,当然也可以直接建立xml文件)。

hibernate.cfg.xml如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
     
     <!--数据库连接信息 ,根据实际情况更改-->
     <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
     <property name="connection.url">jdbc:sqlserver://192.168.11.139:1433;DatabaseName=myweb_ims</property>
     <property name="connection.username">sa</property>
     <property name="connection.password">123456</property>
    
     <!--显示数据库查询语句 -->
     <property name="show_sql">true</property>
     
     <!--数据库方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        
        <!--自动在数据库中建立表 -->
        <property name="hbm2ddl.auto">update</property>
        
        <!--类的映射文件 -->
        <mapping resource="wuyechun/myhibernate/model/HibernateUser.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

 

4. 建立实体类,HibernateUser.java ,包名wuyechun.myhibernate.model。

 

HibernateUser.java 如下:

 

package wuyechun.myhibernate.model;

public class HibernateUser {
 
 private int nativeId;  //另作字段,作为主键。
 private String userId;
 private String userName;
 private String telphone;
 
 public int getNativeId() {
  return nativeId;
 }
 public void setNativeId(int nativeId) {
  this.nativeId = nativeId;
 }
 public String getUserId() {
  return userId;
 }
 public void setUserId(String userId) {
  this.userId = userId;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getTelphone() {
  return telphone;
 }
 public void setTelphone(String telphone) {
  this.telphone = telphone;
 }
}

 

 

5.在包 wuyechun.myhibernate.model  建立实体类 HibernateUser.java  的映射文件 HibernateUser.hbm.xml。

 

HibernateUser.hbm.xml  如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="wuyechun.myhibernate.model">
  <class name="HibernateUser" table="tbl_hibernateuser">
  
    <!--主键自动生成 -->
    <id name="nativeId">
     <generator class="native"></generator> 
    </id>
    <property name="userId"></property>
    <property name="userName"></property>
    <property name="telphone"></property>
  </class>
</hibernate-mapping>

 

6.建立hibernate 工具类 HibernateUtil.java ,包名 wuyechun.myhibernate.service。

 

 

HibernateUtil.java 如下:

package wuyechun.myhibernate.service;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {
 
 private static final SessionFactory sessionFactory = buildSessionFactory();

 private static SessionFactory buildSessionFactory() {
 
     // Create the SessionFactory from hibernate.cfg.xml
     return new Configuration().configure().buildSessionFactory();
  
 }
 

 public static SessionFactory getSessionFactory() {
    return sessionFactory;
 }
}

 

 

7. 建立测试类,Test.java,包名 wuyechun.myhibernate.service 。

 

Test.java 如下:

package wuyechun.myhibernate.service;

import org.hibernate.Session;

import wuyechun.myhibernate.model.HibernateUser;

public class Test {

 public static void main(String[] args) {  
    Test test = new Test();
   
    HibernateUser huser = new HibernateUser();
    huser.setUserId("07212151113");
    huser.setUserName("zhangsan");
    huser.setTelphone("15856958888");
   
   
    test.createAndStoreEvent(huser);
    HibernateUtil.getSessionFactory().close();
 }

 private void createAndStoreEvent(HibernateUser huser) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    session.save(huser);
    session.clear();
    session.flush();
    session.getTransaction().commit();
 }

 }

 

注:为了更方便看清hibernate 执行的相关过程,将 6、7合并成 MyTest.java 。

MyTest.java  如下:

 

package wuyechun.myhibernate.service;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import wuyechun.myhibernate.model.HibernateUser;

public class MyTest {

 public static void main(String[] args) {

  Configuration cfg = new Configuration().configure(); // 读取hibernate.cfg.xml 文件
  SessionFactory factory = cfg.buildSessionFactory(); // 创建SessionFactory
  Session session = null;

  try {
   session = factory.openSession();
   session.beginTransaction();   // 开启事务

   HibernateUser huser = new HibernateUser();
   huser.setUserId("07212151114");
   huser.setUserName("lisi");
   huser.setTelphone("13956219999");

   session.save(huser); // 保存数据
   session.getTransaction().commit(); // 提交事务
  } catch (Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback(); // 回滚事务
  } finally {

   if (session != null) {

    if (session.isOpen()) {
     session.close(); // 关闭事务
    }

   }
  }

 }

}

 

 

8.在SQL Server数据库中,建立数据库 myweb_ims 。

9. 右键Test.java     Run As ——Java Application,测试。

10.测试结果。

1)控制台显示:

Hibernate: insert into tbl_hibernateuser (userId, userName, telphone) values (?, ?, ?)

2)数据库:

  (1)生成了表 tbl_hibernateuser。

  (2)在表中插入了一条数据。

 

 

http://hi.baidu.com/wuyechun_2008/blog/item/022a1f646642393eaa184c2c.html

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics