import?java.util.Date;
//學生類
public?class?Students?{
private?int?sid;
private?String?sname;
private?String?gender;
private?Date?birthday;
private?String?address;
public?Students()?{
}
public?Students(int?sid,?String?sname,?String?gender,?Date?birthday,?String?address)?{
//?super();
this.sid?=?sid;
this.sname?=?sname;
this.gender?=?gender;
this.birthday?=?birthday;
this.address?=?address;
}
public?int?getSid()?{
return?sid;
}
public?void?setSid(int?sid)?{
this.sid?=?sid;
}
public?String?getSname()?{
return?sname;
}
public?void?setSname(String?sname)?{
this.sname?=?sname;
}
public?String?getGender()?{
return?gender;
}
public?void?setGender(String?gender)?{
this.gender?=?gender;
}
public?Date?getBirthday()?{
return?birthday;
}
public?void?setBirthday(Date?birthday)?{
this.birthday?=?birthday;
}
public?String?getAddress()?{
return?address;
}
public?void?setAddress(String?address)?{
this.address?=?address;
}
@Override
public?String?toString()?{
return?"Students?[sid="?+?sid?+?",?sname="?+?sname?+?",?gender="?+?gender?+?",?birthday="?+?birthday
+?",?address="?+?address?+?"]";
}
}
<?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?name="">
??<property?name="connection.username">root</property>
??<property?name="connection.password"/>
??<property?name="connection.driver_class">com.mysql.jdbc.Driver</property>
??<property?name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
??<property?name="dialect">org.hibernate.dialect.MySQLDialect</property>
??
??<property?name="show_sql">true</property>
??<property?name="format">true</property>
??<property?name="hbm2ddl.auto">create</property>
??
??<mapping?resource="Students.hbm.xml"/>
?</session-factory>
</hibernate-configuration>
import?java.util.Date;
import?org.hibernate.Session;
import?org.hibernate.SessionFactory;
import?org.hibernate.Transaction;
import?org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import?org.hibernate.cfg.Configuration;
import?org.hibernate.service.ServiceRegistry;
import?org.junit.After;
import?org.junit.Before;
import?org.junit.Test;
//測試類
public?class?StudentsTest?{
private?SessionFactory?sessionFactory;
private?Session?session;
private?Transaction?transaction;
@Before
public?void?init()?{
Configuration?config?=?new?Configuration().configure();
//?創(chuàng)建服務注冊對象
ServiceRegistry?serviceRegistry?=?new?StandardServiceRegistryBuilder().applySettings(config.getProperties())
.build();
sessionFactory?=?config.buildSessionFactory(serviceRegistry);
session?=?sessionFactory.openSession();
transaction?=?session.beginTransaction();
}
@After
public?void?destory()?{
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public?void?testSaveStudents()?{
Students?s?=?new?Students(1,?"張三",?"男",?new?Date(),?"武漢");
session.save(s);
}
}
<?xml?version="1.0"?>
<!DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--?Generated?2016-6-23?12:01:49?by?Hibernate?Tools?3.5.0.Final?-->
<hibernate-mapping>
????<class?name="Students"?table="STUDENTS">
????????<id?name="sid"?type="int">
????????????<column?name="SID"?/>
????????????<generator?class="assigned"?/>
????????</id>
????????<property?name="sname"?type="java.lang.String">
????????????<column?name="SNAME"?/>
????????</property>
????????<property?name="gender"?type="java.lang.String">
????????????<column?name="GENDER"?/>
????????</property>
????????<property?name="birthday"?type="java.util.Date">
????????????<column?name="BIRTHDAY"?/>
????????</property>
????????<property?name="address"?type="java.lang.String">
????????????<column?name="ADDRESS"?/>
????????</property>
????</class>
</hibernate-mapping>
2017-01-02
感謝樓主,我的問題也解決了,困擾了我老久了
2016-09-02
非常感謝樓主,問題解決了
2016-06-23
已在hibernate的官網(wǎng)文檔找到答案了,可直接使用
問題解決
2016-06-23
將圈中語句
改成
原因不知道。