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?configuration?=?new?Configuration().configure()?;//?默認使用src文件夾下的hibernate.cfg.xml
//創建會話工廠對象
sessionFactory?=?configuration.buildSessionFactory();
//會話對象
session?=?sessionFactory.openSession();
//開啟事務
transaction?=?session.beginTransaction();
????????
}
@After
public?void?destroy(){
transaction.commit();//提交事務
session.close();//關閉會話
sessionFactory.close();//關閉會話工廠
}
@Test
public?void?testSaveStudents(){
//生成學生對象
Students?s?=?new?Students(1,"張三豐","男",new?Date(),"武當山");
session.save(s);//保存對象進入數據庫
}
}
<?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>
????<property?name="connection.username">root</property>
????<property?name="connection.password">123456</property>
????<property?name="connection.driver_class">com.mysql.jdbc.Driver</property>
????<property?name="connection.url">jdbc:mysql://localhost:3306/hibernate</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>
<?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-23?18:03:27?by?Hibernate?Tools?3.5.0.Final?-->
<hibernate-mapping>
????<class?name="hibernate001.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.sql.Date">
????????????<column?name="BIRTHDAY"?/>
????????</property>
????????<property?name="address"?type="java.lang.String">
????????????<column?name="ADDRESS"?/>
????????</property>
????</class>
</hibernate-mapping>
package?hibernate001;
import?java.util.Date;
public?class?Students?{
/**
?*?javaBean設計原則
?*?1、公有的類
?*?2、不帶參數的構造方法
?*?3、私有的屬性
?*?4、get/set方法進行封裝
?*?
?*/
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?+?"]";
}
?????
}
報的異常如下:
java.lang.Exception:?No?tests?found?matching?[{ExactMatcher:fDisplayName=saveStudents],?{ExactMatcher:fDisplayName=saveStudents(hibernate001.StudentsTest)],?{LeadingIdentifierMatcher:fClassName=hibernate001.StudentsTest,fLeadingIdentifier=saveStudents]]?from?org.junit.internal.requests.ClassRequest@6d311334
at?org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at?org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77)
at?org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68)
at?org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at?org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at?org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at?org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at?org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
我覺得錯誤是不是我裝的插件裝少了的原因,因為我下的插件包里面只有一個是hibernate.tool插件,而老師的有四個,如果是插件問題,求給個正確的下載地址。
2017-03-01
為什么沒人