Hibernate 第一例

简单的hibernate例子,不涉及jsp页面,直接java代码运行

java类:HibernateUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package dao;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class HibernateUtil {
     
    SessionFactory sf=new Configuration().configure().buildSessionFactory();
     
    public Session openSession(){
        return sf.openSession();
    }
 
}

  

java类:UserDaoImlp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package dao;
 
import org.hibernate.Session;
import org.hibernate.Transaction;
 
import entity.User;
 
public class UserDaoImlp {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        User u = new User(); 
        u.setId(4);  
        u.setName("张三");
        u.setBirthday("2014-05-21");
        save(u);
 
 
    }
 
     
    public static void save(final Object obj) 
    
        Session s = null
        Transaction t = null
        try 
        
            s = new HibernateUtil().openSession();
            t = s.beginTransaction(); 
            s.save(obj); 
            t.commit(); 
        
        finally 
        
            if (s != null
            
                s.close(); 
            
        
        System.out.println("保存完成"); 
    
     
     
     
    public static void update(final User u) 
    
        Session s = null
        Transaction t = null
        try 
        
            s = new HibernateUtil().openSession();
            t = s.beginTransaction(); 
            s.update(u); 
            t.commit(); 
        
        finally 
        
            if (s != null
            
                s.close(); 
            
        
        System.out.println("修改完成"); 
    
}

  

java类:User实体

package entity;

public class User {
    private int id;
    private String name;
    private String birthday;
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    
    

}

 

 

配置文件:实体User.hbm.xml

<?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="entity">

   <class name="User" table="aa_f">
      <id name="id" column="id" type="java.lang.Integer">
         <generator class="increment" />
      </id>
      <property name="name"  column="name" type="java.lang.String"/>
      <property name="birthday"  column="birthday" type="java.lang.String" />
   </class>

</hibernate-mapping>

 

hibernate配置:hibernate.cfg.xml

<?xml version=1.0 encoding=utf-8?>

<!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>

        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
        <property name="connection.username">scott</property>
        <property name="connection.password">tiger</property>

        <property name="show_sql">true</property>
        
        <mapping resource="entity/User.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

 

Hibernate 第一例,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。