1. L o g o
Java
Technology
丕仍舒舒仆弍舒舒舒 弌亞仍
2013 仂仆
Hibernate 亳亶仆 舒亶
舒亞 .舒仆亰仂亳亞
2. L o g o
舒于舒 亠仆仂仍仂亞亳 丕仍舒舒仆弍舒舒舒 弌亞仍 2013 仂仆
亞仍亞舒
仂亟仆 弍 舒亞舒舒 哦.弌-舒亶 仂仍弍仂3
Hibernate 亳亶仆 舒亶1
仂亟仆 舒亞舒舒 哦.弌-舒亶 仂仍弍仂2
3. L o g o
HIBERNATE-仆 舒亶
Hibernate-亞 仂仍弍仂仂 唏亞唏亞亟仍亳亶仆
舒仆舒亶 磿弍舒 舒亞舒舒 舒亢亳仍仍舒舒亟
亰仂亳仍舒仆 弍勵仆.
Hibernate 仆 勵仆亞勵亶, 仆仍亶 亳亶仆
Java package ミ.
Hibernate 仆 Table 舒仆亟舒亟舒舒
Object- 舒仆亟舒亢 弍舒亶亞舒舒 仄
舒仆亟舒 弍仂仍仂仄亢亳亶亞 仂仍亞仂亟仂亞.
4. L o g o
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
MySQL 亟
khiberdata 仆亶
唏亞唏亞亟唏仍亳亶仆 舒仆
勵勵亞仆.
金Contact 仆亶 table
勵勵亞仆.
5. L o g o
package myAdd;
public class Contact {
private Long id;
private String firstName;
private String lastName;
private String email;
public Contact() { }
Contact.java
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
6. L o g o
public Contact(String firstName, String lastName, String
email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public Long getId() {
return this.id;
}
Contact.java
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
7. L o g o
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
Contact.java
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
8. L o g o
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
}
Contact.java
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
9. L o g o
<?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>
<class name="myAdd.Contact" table="contact"
catalog="khiberdata">
<id name="id" type="java.lang.Long">
<column name="id/> <generator class="identity/>
</id>// update, delete 亳亶亟 亢亳亳 亞舒 弍仂仍仆仂
Contact.hbm.xml
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
10. L o g o
<property name="firstName" type="string">
<column name="firstName" length="50" not-null="true" />
</property>
<property name="lastName" type="string">
<column name="lastName" length="50" not-null="true" />
</property>
<property name="email" type="string">
<column name="email" length="50" not-null="true" />
</property> </class> </hibernate-mapping>
Contact.hbm.xml
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
11. L o g o
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-
//Hibernate/Hibernate Reverse Engineering DTD
3.0//EN" "http://hibernate.sourceforge.net/hibernate-
reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-catalog="khiberdata"/>
<table-filter match-name="contact"/>
</hibernate-reverse-engineering>
hibernate.reveng.xml
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
12. L o g o
<?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="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
hibernate.cfg.xml
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
13. L o g o
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/khiberdata</property>
<property name="hibernate.connection.username">
root</property>
<mapping resource=Contact.hbm.xml"/>
</session-factory></hibernate-configuration>
hibernate.cfg.xml
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
14. L o g o
package khiber1;
//Contact Class-亞 仂仍弍仂仆仂.
import myAdd.Contact;
//Hibernate-仆 舒舒亟仍舒亞舒舒亶 Class-亟.
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Query;
import org.hibernate.Transaction;
KHiber1View.java
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
15. L o g o
Session session = null;
//hibernate.cfg.xml-亞 舒舒舒仍仍舒亢, hibernate-亞 亞仍亟 弍仍亞仆.
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//object 勵勵亞亢 舒仆仆 亞舒 仂仍亞仂仆 舒亟亞舒仍仆舒.
Contact contact = new Contact();
contact.setFirstName(txtFName.getText());
contact.setLastName(txtLName.getText());
contact.setEmail(txtEmail.getText());
session.save(contact); //insert into values
session.flush(); //execute
session.close();
KHiber1View.java //INSERT
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
16. L o g o
Session session = null;
//hibernate.cfg.xml-亞 舒舒舒仍仍舒亢, hibernate-亞 亞仍亟 弍仍亞仆.
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Contact contact = new Contact();
contact.setId(Long.parseLong(txtId.getText())); //亢亳亳 亞舒
contact.setFirstName(txtFName.getText()); //仂仍亳 亞舒
contact.setLastName(txtLName.getText()); //仂仍亳 亞舒
contact.setEmail(txtEmail.getText()); //仂仍亳 亞舒
session.update(contact); // update set where
session.flush(); //execute
session.close();
KHiber1View.java //UPDATE
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
17. L o g o
Session session = null;
//hibernate.cfg.xml-亞 舒舒舒仍仍舒亢, hibernate-亞 亞仍亟
弍仍亞仆.
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Contact contact = new Contact();
contact.setId(Long.parseLong(txtId.getText())); //亢亳亳 亞舒
session.delete(contact); //delete where
session.flush(); //execute
session.close();
KHiber1View.java //DELETE
仂亟仆 舒亞舒舒 唏亞唏亞亟仍亳亶仆 舒仆舒亶 仂仍弍仂
18. L o g o
勤{夷出角亟舒舒 仆仄仆 hibernate, MySQL JDBC
Driver
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
19. L o g o
勤{夷出角亟舒舒 仆仄仆 hibernate, MySQL JDBC
Driver
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
20. L o g o
hibernate.cfg.xml 勵勵亞
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
21. L o g o
hibernate.cfg.xml 勵勵亞
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
22. L o g o
hibernate.cfg.xml 勵勵亞
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
23. L o g o
hibernate.cfg.xml 勵勵亞
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
24. L o g o
hibernate.cfg.xml 勵勵亞
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
25. L o g o
MyAdd package 勵勵亞
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
26. L o g o
hibernate.reveng.xml
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
27. L o g o
hibernate.reveng.xml
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
28. L o g o
Contact.hbm.xml
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂
29. L o g o
Contact.hbm.xml
仂亟仆 弍 舒亞舒舒 唏亞. 舒仆舒亶 仂仍弍仂