关于如何配置请参看:Hibernate学习笔记(一)--用MyEclipse 6.5+MySQL 5.0的环境跑起来
准备:建表
用MySQL在名为STMS数据库中建表persons
src/org.lxh.hibernate.Contact.java
1
package
org.lxh.hibernate;
2
3 /** */ /**
4 * @author ∪∩BUG E-mail: [email protected]
5 * @version Aug 30, 2008 8:02:48 PM
6 */
7 public class Contact {
8
9 private String address;
10 private String zipcode;
11 private String tel;
12
13 public String getAddress() {
14 return address;
15 }
16
17 public void setAddress(String address) {
18 this.address = address;
19 }
20
21 public String getZipcode() {
22 return zipcode;
23 }
24
25 public void setZipcode(String zipcode) {
26 this.zipcode = zipcode;
27 }
28
29 public String getTel() {
30 return tel;
31 }
32
33 public void setTel(String tel) {
34 this.tel = tel;
35 }
36
37}
38
2
3 /** */ /**
4 * @author ∪∩BUG E-mail: [email protected]
5 * @version Aug 30, 2008 8:02:48 PM
6 */
7 public class Contact {
8
9 private String address;
10 private String zipcode;
11 private String tel;
12
13 public String getAddress() {
14 return address;
15 }
16
17 public void setAddress(String address) {
18 this.address = address;
19 }
20
21 public String getZipcode() {
22 return zipcode;
23 }
24
25 public void setZipcode(String zipcode) {
26 this.zipcode = zipcode;
27 }
28
29 public String getTel() {
30 return tel;
31 }
32
33 public void setTel(String tel) {
34 this.tel = tel;
35 }
36
37}
38
src/org.lxh.hibernate.Name.java
1
package
org.lxh.hibernate;
2
3 /** */ /**
4 * @author ∪∩BUG E-mail: [email protected]
5 * @version Aug 30, 2008 7:59:53 PM
6 */
7 public class Name {
8
9 private String firstname;
10 private String lastname;
11
12 public String getFirstname() {
13 return firstname;
14 }
15
16 public void setFirstname(String firstname) {
17 this.firstname = firstname;
18 }
19
20 public String getLastname() {
21 return lastname;
22 }
23
24 public void setLastname(String lastname) {
25 this.lastname = lastname;
26 }
27
28}
29
2
3 /** */ /**
4 * @author ∪∩BUG E-mail: [email protected]
5 * @version Aug 30, 2008 7:59:53 PM
6 */
7 public class Name {
8
9 private String firstname;
10 private String lastname;
11
12 public String getFirstname() {
13 return firstname;
14 }
15
16 public void setFirstname(String firstname) {
17 this.firstname = firstname;
18 }
19
20 public String getLastname() {
21 return lastname;
22 }
23
24 public void setLastname(String lastname) {
25 this.lastname = lastname;
26 }
27
28}
29
src/org.lxh.hibernate.Persons.java
1
package
org.lxh.hibernate;
2 /** */ /**
3 * @author ∪∩BUG E-mail: [email protected]
4 * @version Aug 30, 2008 8:04:57 PM
5 * @本类包含Nane类和Contact类
6 */
7 public class Persons {
8 private int id;
9 private Name name;
10 private Contact contact;
11
12 public int getId() {
13 return id;
14 }
15
16 public void setId(int id) {
17 this.id = id;
18 }
19
20 public Name getName() {
21 return name;
22 }
23
24 public void setName(Name name) {
25 this.name = name;
26 }
27
28 public Contact getContact() {
29 return contact;
30 }
31
32 public void setContact(Contact contact) {
33 this.contact = contact;
34 }
35
36}
37
2 /** */ /**
3 * @author ∪∩BUG E-mail: [email protected]
4 * @version Aug 30, 2008 8:04:57 PM
5 * @本类包含Nane类和Contact类
6 */
7 public class Persons {
8 private int id;
9 private Name name;
10 private Contact contact;
11
12 public int getId() {
13 return id;
14 }
15
16 public void setId(int id) {
17 this.id = id;
18 }
19
20 public Name getName() {
21 return name;
22 }
23
24 public void setName(Name name) {
25 this.name = name;
26 }
27
28 public Contact getContact() {
29 return contact;
30 }
31
32 public void setContact(Contact contact) {
33 this.contact = contact;
34 }
35
36}
37
src/org.lxh.hibernate.PersonsOperate.java
1
package
org.lxh.hibernate;
2
3 import java.util.List;
4
5 import org.hibernate.Query;
6 import org.hibernate.Session;
7 import org.hibernate.SessionFactory;
8 import org.hibernate.cfg.Configuration;
9
10 /** */ /**
11 * @author ∪∩BUG E-mail: [email protected]
12 * @version Aug 30, 2008 8:55:43 PM @ 具体操作Hibernate的类
13 */
14 public class PersonsOperate {
15 // 在Hibernate中所有的操作都是通过Session来完成
16 private Session session;
17
18 // Session 是一个接口,必须实例化
19 // 在构造方法中实例实化Session对象
20 public PersonsOperate() {
21 // 找到Hibernae配置文件
22 Configuration config = new Configuration().configure();
23
24 // 从全局文件中取出SessionFactory
25 SessionFactory factory = config.buildSessionFactory();
26
27 // 从sessionFactory中取出一个session
28 this.session = factory.openSession();
29 }
30
31 // 所有的操作都是通过Session进行
32 // (1)增加操作
33 public void insert(Persons p) {
34 // 将数据存放到数据库中
35 this.session.save(p);
36
37 // 事务提交
38 this.session.beginTransaction().commit();
39 }
40
41 // 通过HQL查询全部数据
42 public List queryAll() {
43 String hql = "FROM Persons as p";
44 Query q = this.session.createQuery(hql);
45 List l = q.list();
46 return l;
47 }
48}
49
2
3 import java.util.List;
4
5 import org.hibernate.Query;
6 import org.hibernate.Session;
7 import org.hibernate.SessionFactory;
8 import org.hibernate.cfg.Configuration;
9
10 /** */ /**
11 * @author ∪∩BUG E-mail: [email protected]
12 * @version Aug 30, 2008 8:55:43 PM @ 具体操作Hibernate的类
13 */
14 public class PersonsOperate {
15 // 在Hibernate中所有的操作都是通过Session来完成
16 private Session session;
17
18 // Session 是一个接口,必须实例化
19 // 在构造方法中实例实化Session对象
20 public PersonsOperate() {
21 // 找到Hibernae配置文件
22 Configuration config = new Configuration().configure();
23
24 // 从全局文件中取出SessionFactory
25 SessionFactory factory = config.buildSessionFactory();
26
27 // 从sessionFactory中取出一个session
28 this.session = factory.openSession();
29 }
30
31 // 所有的操作都是通过Session进行
32 // (1)增加操作
33 public void insert(Persons p) {
34 // 将数据存放到数据库中
35 this.session.save(p);
36
37 // 事务提交
38 this.session.beginTransaction().commit();
39 }
40
41 // 通过HQL查询全部数据
42 public List queryAll() {
43 String hql = "FROM Persons as p";
44 Query q = this.session.createQuery(hql);
45 List l = q.list();
46 return l;
47 }
48}
49
src/org.lxh.hibernate.Persons.hbm.xml
1
xml version="1.0" encoding="utf-8"?>
2 DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
7<hibernate-mapping>
8 <class name="org.lxh.hibernate.Persons" table="persons"
9 catalog="stms">
10 <id name="id" type="java.lang.Integer">
11 <column name="id" />
12 <generator class="assigned" />
13 id>
14
19 <component name="name" class="org.lxh.hibernate.Name">
20 <property name="firstname" type="java.lang.String">
21 <column name="firstname" length="20" not-null="true" />
22 property>
23 <property name="lastname" type="java.lang.String">
24 <column name="lastname" length="20" not-null="true" />
25 property>
26 component>
27 <component name="contact" class="org.lxh.hibernate.Contact">
28
29 <property name="address" type="java.lang.String">
30 <column name="address" length="20" not-null="true" />
31 property>
32 <property name="zipcode" type="java.lang.String">
33 <column name="zipcode" length="6" not-null="true" />
34 property>
35 <property name="tel" type="java.lang.String">
36 <column name="tel" length="20" />
37 property>
38 component>
39 class>
40 hibernate-mapping>
41
2 DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
7<hibernate-mapping>
8 <class name="org.lxh.hibernate.Persons" table="persons"
9 catalog="stms">
10 <id name="id" type="java.lang.Integer">
11 <column name="id" />
12 <generator class="assigned" />
13 id>
14
19 <component name="name" class="org.lxh.hibernate.Name">
20 <property name="firstname" type="java.lang.String">
21 <column name="firstname" length="20" not-null="true" />
22 property>
23 <property name="lastname" type="java.lang.String">
24 <column name="lastname" length="20" not-null="true" />
25 property>
26 component>
27 <component name="contact" class="org.lxh.hibernate.Contact">
28
29 <property name="address" type="java.lang.String">
30 <column name="address" length="20" not-null="true" />
31 property>
32 <property name="zipcode" type="java.lang.String">
33 <column name="zipcode" length="6" not-null="true" />
34 property>
35 <property name="tel" type="java.lang.String">
36 <column name="tel" length="20" />
37 property>
38 component>
39 class>
40 hibernate-mapping>
41
src/org.lxh.hibernate.TestDemo.java
1
package
org.lxh.hibernate;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 import sun.security.action.GetBooleanAction;
7
8 /** */ /**
9 * @author ∪∩BUG E-mail: [email protected]
10 * @version Aug 30, 2008 8:55:03 PM
11 * @测试类
12 */
13 public class TestDemo {
14
15 /** *//**
16 * @param args
17 */
18 public static void main(String[] args) {
19
20 PersonsOperate po = new PersonsOperate();
21 /**//*
22 //测试插入数据
23 Persons p = new Persons();
24 Name n = new Name();
25 Contact c = new Contact();
26
27 n.setFirstname("My");
28 n.setLastname("SQL");
29
30 c.setAddress("mysql.com");
31 c.setTel("12345678");
32 c.setZipcode("54321");
33
34 p.setId(1);
35 p.setName(n);
36 p.setContact(c);
37
38 po.insert(p);
39 */
40
41 //测试查询全部数据
42 List l = po.queryAll();
43 Iterator iter = l.iterator();
44 while (iter.hasNext()) {
45 Persons p = (Persons)iter.next();
46 System.out.println("ID:\t" + p.getId());
47 System.out.println("FIRSTNAME:\t" + p.getName().getFirstname());
48 System.out.println("LASTNAME:\t" + p.getName().getLastname());
49 System.out.println("ADDRESS:\t" + p.getContact().getAddress());
50 System.out.println("TEL:\t" + p.getContact().getTel());
51 System.out.println("ZIPCODE:\t" + p.getContact().getZipcode());
52 System.out.println("----------------------------------------------");
53 }
54 }
55
56}
57
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 import sun.security.action.GetBooleanAction;
7
8 /** */ /**
9 * @author ∪∩BUG E-mail: [email protected]
10 * @version Aug 30, 2008 8:55:03 PM
11 * @测试类
12 */
13 public class TestDemo {
14
15 /** *//**
16 * @param args
17 */
18 public static void main(String[] args) {
19
20 PersonsOperate po = new PersonsOperate();
21 /**//*
22 //测试插入数据
23 Persons p = new Persons();
24 Name n = new Name();
25 Contact c = new Contact();
26
27 n.setFirstname("My");
28 n.setLastname("SQL");
29
30 c.setAddress("mysql.com");
31 c.setTel("12345678");
32 c.setZipcode("54321");
33
34 p.setId(1);
35 p.setName(n);
36 p.setContact(c);
37
38 po.insert(p);
39 */
40
41 //测试查询全部数据
42 List l = po.queryAll();
43 Iterator iter = l.iterator();
44 while (iter.hasNext()) {
45 Persons p = (Persons)iter.next();
46 System.out.println("ID:\t" + p.getId());
47 System.out.println("FIRSTNAME:\t" + p.getName().getFirstname());
48 System.out.println("LASTNAME:\t" + p.getName().getLastname());
49 System.out.println("ADDRESS:\t" + p.getContact().getAddress());
50 System.out.println("TEL:\t" + p.getContact().getTel());
51 System.out.println("ZIPCODE:\t" + p.getContact().getZipcode());
52 System.out.println("----------------------------------------------");
53 }
54 }
55
56}
57
src/hibernate.cfg.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
7<hibernate-configuration>
8
9 <session-factory>
10 <property name="connection.username">root property>
11 <property name="connection.url">
12 jdbc:mysql://localhost:3306/STMS
13 property>
14 <property name="dialect">
15 org.hibernate.dialect.MySQLDialect
16 property>
17 <property name="myeclipse.connection.profile">
18 MySql_localhost
19 property>
20 <property name="connection.password">root property>
21 <property name="connection.driver_class">
22 com.mysql.jdbc.Driver
23 property>
24 <property name="show_sql">true property>
25
26
27 <mapping resource="org/lxh/hibernate/Persons.hbm.xml" />
28
29 session-factory>
30
31 hibernate-configuration>
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
7<hibernate-configuration>
8
9 <session-factory>
10 <property name="connection.username">root property>
11 <property name="connection.url">
12 jdbc:mysql://localhost:3306/STMS
13 property>
14 <property name="dialect">
15 org.hibernate.dialect.MySQLDialect
16 property>
17 <property name="myeclipse.connection.profile">
18 MySql_localhost
19 property>
20 <property name="connection.password">root property>
21 <property name="connection.driver_class">
22 com.mysql.jdbc.Driver
23 property>
24 <property name="show_sql">true property>
25
26
27 <mapping resource="org/lxh/hibernate/Persons.hbm.xml" />
28
29 session-factory>
30
31 hibernate-configuration>
例子结构: