hibernate.cfg.xml
<?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="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="Students.hbm.xml" />
</session-factory>
</hibernate-configuration>
Students.hbm.xml
<?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 2017-2-16 0:11:54 by Hibernate Tools 3.4.0.CR1 -->
<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>
Students.java
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) {
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;
}
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", birthday=" + birthday + ", address=" + address
+ "]";
}
}
StudentsTest.java
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
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 configuration=new Configuration().configure();
sessionFactory=configuration.buildSessionFactory();
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);
}
}
报错信息:
二月 16, 2017 1:14:19 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.7.Final}
二月 16, 2017 1:14:19 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
二月 16, 2017 1:14:19 上午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.... Use namespace http://www.hibernate.org/dtd/... instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
提示找不到hibernate.properties 这个文件。。怎么回事?
搞定了 首先参考这篇文章,http://blog.163.com/xh_ding/b...


因为Hibernate 4废弃buildSessionFactory,所以先按照我给的网页去修改,可能会报错,导入这两个包import org.hibernate.service.ServiceRegistryBuilder;
运行成功后如图
其实我5.10号就答辩了,现在才学hibernate,真是作死啊