hibernater基础

来源:互联网 发布:sap数据质量核对 编辑:程序博客网 时间:2024/06/06 00:31

Hibernate 的所有操作都基于Session进行的,增、删、改、查都是通过Session完成的。

 

Hibernate 最基础的内容包括以下内容:

 

1. 一个主配置文件,hibernate.cfg.xml ,最基本的内容如下:

 

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.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  7. <hibernate-configuration>  
  8.   
  9. <session-factory>  
  10.     <property name="myeclipse.connection.profile">Oracle Conn</property>  
  11.     <property name="connection.url">jdbc:mysql://localhost:3306/vvop?useUnicode=true&amp;characterEncoding=UTF-8</property>  
  12.     <property name="connection.username">root</property>  
  13.     <property name="connection.password">123456</property>  
  14.     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  15.     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  16.     <property name="show_sql">true</property>  
  17.     <mapping resource="net/kentop/testhibernate/demo01/Titem.hbm.xml"></mapping>  
  18.   
  19.   
  20. </session-factory>  
  21.   
  22. </hibernate-configuration>  

 

 

2. 一个Pojo 类, 内容如下:

 

Java代码  收藏代码
  1. package net.kentop.testhibernate.demo01;  
  2.   
  3. public class User {  
  4.       
  5.     private String id;  
  6.     private String name;  
  7.     private String sex;  
  8.     private Integer age;  
  9.     public String getId() {  
  10.         return id;  
  11.     }  
  12.     public void setId(String id) {  
  13.         this.id = id;  
  14.     }  
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.     public String getSex() {  
  22.         return sex;  
  23.     }  
  24.     public void setSex(String sex) {  
  25.         this.sex = sex;  
  26.     }  
  27.     public Integer getAge() {  
  28.         return age;  
  29.     }  
  30.     public void setAge(Integer age) {  
  31.         this.age = age;  
  32.     }  
  33.       
  34. }  

 

 

 

3. 一个Pojo类的描述文件

 

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.                             "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.                             "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  
  5.   
  6. <!-- DO NOT EDIT: This is a generated file that is synchronized -->  
  7. <!-- by MyEclipse Hibernate tool integration.                   -->  
  8. <!-- Created Mon Dec 18 19:38:27 CST 2006                         -->  
  9. <hibernate-mapping package="net.kentop.testhibernate.demo01">  
  10.   
  11.     <class name="User" table="user">  
  12.         <id name="id" column="id" type="string">  
  13.             <generator class="uuid.hex" />  
  14.         </id>  
  15.   
  16.         <property name="name" column="name" type="string" not-null="true" />  
  17.         <property name="sex" column="sex" type="string" />  
  18.         <property name="age" column="age" type="int" />  
  19.     </class>  
  20.   
  21. </hibernate-mapping>  

 

 

4. 一个进行增\删\改\查的操作类

 

Java代码  收藏代码
  1. package net.kentop.testhibernate.demo01;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6.   
  7. import org.hibernate.Query;  
  8. import org.hibernate.Session;  
  9. import org.hibernate.SessionFactory;  
  10. import org.hibernate.cfg.Configuration;  
  11.   
  12. public class UserOpera {  
  13.       
  14.     private Session session;  
  15.       
  16.     public UserOpera() {  
  17.         Configuration config = new Configuration().configure();  
  18.           
  19.         SessionFactory factory = config.buildSessionFactory();  
  20.           
  21.         this.session = factory.openSession();  
  22.     }  
  23.       
  24.       
  25.     public void insert(User user) {  
  26.         session.save(user);  
  27.         this.session.beginTransaction().commit();  
  28.           
  29.         this.session.close();  
  30.     }  
  31.       
  32.     public void insertUsers(ArrayList<User> users) {  
  33.           
  34.         int i = 0;  
  35.         for(User user:users ) {  
  36.             session.save(user);  
  37.             i ++;  
  38.             if(i % 3 == 0) {  
  39.                 session.beginTransaction().commit();  
  40.             }  
  41.         }  
  42.         session.beginTransaction().commit();  
  43.           
  44.     }  
  45.       
  46.     public User getUserById(String id) {  
  47.           
  48.         User user = null;  
  49.           
  50.         String hql = "from User where id=?";  
  51.           
  52.         Query query = this.session.createQuery(hql);  
  53.         query.setString(0, id);  
  54.         List l = query.list();  
  55.           
  56.         Iterator iter = l.iterator();  
  57.           
  58.         while(iter.hasNext()) {  
  59.             user = (User)iter.next();  
  60.         }  
  61.           
  62.           
  63.           
  64.         return user;  
  65.           
  66.           
  67.     }  
  68.       
  69.     public void deleteById(String id) {  
  70.           
  71.         String hql = "delete User where id=?";  
  72.         Query q = this.session.createQuery(hql);  
  73.         q.setString(0, id);  
  74.           
  75.         q.executeUpdate();  
  76.           
  77.         this.session.beginTransaction().commit();  
  78.           
  79.     }  
  80.       
  81.     public List getAllUser() {  
  82.           
  83.         String hql = "from User";  
  84.         Query q = session.createQuery(hql);  
  85.           
  86.         return q.list();  
  87.           
  88.     }  
  89. }