Caused by: org.xml.sax.SAXParseException;必須為元素型別 "mapping" 宣告屬性 "resourse"。
阿新 • • 發佈:2019-02-01
求教,hibernate使用Junit測試時報錯,不知道具體怎麼修改,請問下大佬們怎麼處理?
hibernate.cfg.xml
<?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:///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 resourse="Students.hbm.xml" />
</session-factory>
</hibernate-configuration>
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.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
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();
//建立服務註冊物件
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//建立會話工廠物件
sessionFactory=config.buildSessionFactory(serviceRegistry);
//會話物件
session=sessionFactory.openSession();
//開啟事務
transaction= session.beginTransaction();
}
@After
public void destroy(){
transaction.commit();//提交事務
session.close(); //關閉會話
sessionFactory.close();//關閉會話工廠
}
@Test
public void testSaveStudents() {
Students s=new Students();
s.setSname("nihao");
//Students s=new Students(1,"張三丰","男",new Date(),"武當山");
session.save(s);//儲存物件加入到資料庫
}
}
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) {
// 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 + "]";
}
}
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 2018-9-3 19:24:47 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>
9.10 錯誤已解決,參考網上大神,上面程式碼沒有問題,是系統版本問題,具體細節沒貼,直接貼全部成功程式碼,碰到此類問題的朋友可以直接複製測試看看。
Students.java
package com.hibernate_1;
/**
* 學生類
*
* @author wxy
*
*/
public class Students {
private int sid;
private String sname;
private String gender;
private String address;
public Students() {
}
public Students(int sid, String sname, String gender, String address) {
super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", address=" + address + "]";
}
}
hibernate.cfg.xml
<?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:///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-4-2 18:05:47 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.hibernate_1.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="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>
StudentsTest.java
package com.hibernate_2;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.hibernate_1.Students;
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init() {
Configuration config=new Configuration().configure();
//建立服務註冊物件
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//建立會話工廠物件
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 student = new Students(1, "張三丰", "男", "武當山");
System.out.println(student);
session.save(student);
System.out.println(session);
}
}