问题描述
使用hibernate用的是xml方式,没有用Annotation,但是运行项目是总是报错提示Exceptioninthread"main"org.hibernate.MappingException:AnAnnotationConfigurationinstanceisrequiredtouse<mappingclass="com.bjsxt.hibernate.model.teacher"/>atorg.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1648)atorg.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)atorg.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)atorg.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)atorg.hibernate.cfg.Configuration.configure(Configuration.java:1476)atorg.hibernate.cfg.Configuration.configure(Configuration.java:1462)atStudentTest.main(StudentTest.java:15)主方法:importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.cfg.Configuration;importcom.bjsxt.hibernate.model.student;publicclassStudentTest{publicstaticvoidmain(String[]args){students=newstudent();s.setName("s5");s.setAge(5);Configurationcfg=newConfiguration();SessionFactorysf=cfg.configure().buildSessionFactory();Sessionsession=sf.openSession();session.beginTransaction();session.save(s);session.getTransaction().commit();session.close();sf.close();}}配置信息:<?xmlversion='1.0'encoding='utf-8'?><!DOCTYPEhibernate-configurationPUBLIC"-//Hibernate/HibernateConfigurationDTD3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!--Databaseconnectionsettings连接接数据库--><propertyname="connection.driver_class">com.mysql.jdbc.Driver</property><propertyname="connection.url">jdbc:mysql://localhost/hibernate</property><propertyname="connection.username">root</property><propertyname="connection.password">root</property><!--JDBCconnectionpool(usethebuilt-in)连接池--><!--<propertyname="connection.pool_size">1</property>--><!--SQLdialect方言--><propertyname="dialect">org.hibernate.dialect.MySQLDialect</property><!--EnableHibernate'sautomaticsessioncontextmanagement--><propertyname="current_session_context_class">thread</property><!--二级缓存Disablethesecond-levelcache--><!--<propertyname="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>--><!--EchoallexecutedSQLtostdout打印sql语句--><propertyname="show_sql">true</property><!--Dropandre-createthedatabaseschemaonstartup自动创建表结构--><!--<propertyname="hbm2ddl.auto">create</property>--><mappingresource="com/bjsxt/hibernate/model/student.hbm.xml"/></session-factory></hibernate-configuration>
解决方案
解决方案二:
端口号没有。。。
解决方案三:
3306端口号可以不写的,上面的提示好像跟这端口号没关系啊!
解决方案四:
AnAnnotationConfigurationinstanceisrequiredtouse<mappingclass="com.bjsxt.hibernate.model.teacher"/>这个是什么配置?
解决方案五:
检查一下pojo对应的xml