标签:style class blog code java http
简单的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>
标签:style class blog code java http
原文地址:http://www.cnblogs.com/jessie-fu/p/3772076.html