002
003
004
005
006
007
008
Customer.hbm.xml
009
010
011
012
013
< xml version="1.0" encoding="UTF-8" >
014
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
015
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
016
017
018
023
024
033
034
035
036
037
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
hibernate.cfg.xml
062
063
064
065
066
067
068
069
< xml version="1.0" encoding="UTF-8" >
070
071
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
072
" http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
073
074
075
076
077
com.mysql.jdbc.Driver
078
jdbc:mysql://192.168.137.244:3306/aypak useUnicode=true&characterEncoding=utf8
079
root
080
root
081
082
org.hibernate.dialect.MySQL5Dialect
083
089
update
090
091
true
092
093
false
094
095
096
097
098
099
100
101
102
103
104
105
实例:CRUD
106
107
108
109
110
111
112
package cn.aypak.a_primer;
113
114
import java.util.List;
115
116
import org.hibernate.Query;
117
import org.hibernate.Session;
118
import org.hibernate.SessionFactory;
119
import org.hibernate.Transaction;
120
import org.hibernate.cfg.Configuration;
121
import org.junit.Test;
122
123
public class App {
124
125
private static SessionFactory sf = null;
126
127
static{
128
Configuration configuration = new Configuration();
129
configuration.configure("cn/aypak/a_primer/hibernate.cfg.xml");
130
configuration.addClass(Customer.class);
131
sf = configuration.buildSessionFactory();
132
}
133
134
/**新增*/
135
http://my.oschina.net/test45" target="_blank" rel="nofollow">@Test
136
public void testInsertCustomer(){
137
Session s = sf.openSession();
138
Transaction tr = s.beginTransaction();
139
140
Customer c = new Customer();
141
c.setName("小三");
142
c.setAge(18);
143
c.setDes("专业的");
144
s.save(c);
145