简单的hibernate例子,不涉及jsp页面,直接java代码运行
java类:HibernateUtil
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
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 |
|
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>