Caused by: org.xml.sax.SAXParseException;必须为元素类型 "mapping" 声明属性 "resourse"。

求教,hibernate使用Junit测试时报错,不知道具体怎么修改,请问下大佬们怎么处理?
Caused by: org.xml.sax.SAXParseException;必须为元素类型

Caused by: org.xml.sax.SAXParseException;必须为元素类型

Caused by: org.xml.sax.SAXParseException;必须为元素类型

hibernate.cfg.xml



<hibernate-configuration>
    <session-factory>
    <property name="connection.username">rootproperty>
    <property name="connection.password">123456property>
    <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
    <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8property>
    <property name="dialect">org.hibernate.dialect.MySQLDialectproperty>
    <property name="show_sql">trueproperty>
    <property name="format_sql">trueproperty>
    <property name="hbm2ddl.auto">createproperty>

    <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




<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



<hibernate-configuration>
    <session-factory>
        <property name="connection.username">rootproperty>
        <property name="connection.password">123456property>
        <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
        <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8property>
        <property name="dialect">org.hibernate.dialect.MySQLDialectproperty>

        <property name="show_sql">trueproperty>
        <property name="format_sql">trueproperty>
        <property name="hbm2ddl.auto">createproperty>

        <mapping resource="Students.hbm.xml"/>
    session-factory>
hibernate-configuration>

Students.hbm.xml




<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);
    }

}

你可能感兴趣的:(hibernate)