java - spring mvc整合hibernate5訪問報(bào)錯(cuò)Could not locate cfg.xml resource
問題描述
Spring MVC整合Hibernate5框架—dispatcher-servlet.xml文件里已經(jīng)配置了數(shù)據(jù)庫連接等信息,寫了個(gè)新建數(shù)據(jù)表的簡單功能(就幾個(gè)文件),運(yùn)行沒有報(bào)錯(cuò)但訪問時(shí)報(bào)HTTP Status 500錯(cuò)誤org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml],不過很奇怪是數(shù)據(jù)表成功建立但沒數(shù)據(jù)寫入。我很納悶的是hibernate4之后不是使用sessionFactoryBean代替hibernate.cfg.xml文件么,我沒有建hibernate.cfg.xml文件。具體的一些文件和錯(cuò)誤截圖如下
dispatcher-servlet.xml文件
<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xmlns:tx='http://www.springframework.org/schema/tx' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd'> <!-- 掃描使用注解的類所在包 --> <context:component-scan base-package='com.hiber.*'/> <!-- 配置數(shù)據(jù)源 --> <bean destroy-method='close'><property name='driverClassName' value='com.mysql.jdbc.Driver' /><property name='url' value='jdbc:mysql://localhost:3306/hiber?useUnicode=yes&characterEncoding=UTF-8' /><property name='username' value='root' /><property name='password' value='3443'/> </bean> <bean class='org.springframework.orm.hibernate5.LocalSessionFactoryBean'><!-- 注入數(shù)據(jù)源 --><property name='dataSource' ref='dataSource'/><!-- 找到實(shí)體包(pojo) --><property name='packagesToScan' value='com.hiber.*' /><property name='hibernateProperties'> <props> <prop key='hibernate.dialect'>org.hibernate.dialect.MySQL57Dialect</prop> <prop key='hibernate.hbm2ddl.auto'>create</prop> <prop key='hibernate.show_sql'>true</prop> </props></property> </bean> <bean class='org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean'><property name='dataSource' ref='dataSource' /><!-- 找到實(shí)體包(pojo) --><property name='packagesToScan' value='com.hiber.*' /><!--指定jpa適配器--><property name='jpaVendorAdapter'> <bean /></property><!--<!–指定jpa屬性–>--><!--<property name='jpaProperties'>--> <!--<props>--><!--<prop key='hibernate.dialect'>org.hibernate.dialect.MySQL57Dialect</prop>--><!--<prop key='hibernate.hbm2ddl.auto'>create</prop>--><!--<prop key='hibernate.show_sql'>true</prop>--> <!--</props>--><!--</property>--> </bean> <!-- 配置hibernate事務(wù)管理器 --> <bean class='org.springframework.orm.hibernate5.HibernateTransactionManager'><property name='sessionFactory' ref='sessionFactory' /> </bean> <tx:annotation-driven /></beans>
Message.java文件
package com.hiber.entity;import javax.persistence.*;@Entitypublic class Message{ @Id @GeneratedValue(strategy = GenerationType.AUTO) int id; @Column(nullable = false) String text; public Message(String text) {setText(text); } public Message(){} public int getId() {return id; } public void setId(int id) {this.id = id; } public String getText() {return text; } public void setText(String text) {this.text = text; }}
IndexController.java文件
package com.hiber.controllers;import com.hiber.entity.Message;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class IndexController { @RequestMapping(value = '/persist') public String saveMessage(){Message message = new Message('Hello, world');StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();session.persist(message);tx.commit();return '數(shù)據(jù)添加成功!'; }}
瀏覽器錯(cuò)誤截圖
數(shù)據(jù)表新建成功截圖
項(xiàng)目結(jié)構(gòu)
大家?guī)兔纯淳烤故悄睦锍隽藛栴},Thanks in advance!
問題解答
回答1:Hibernate5.2版本以上這樣寫:
Message message = new Message('Hello,world!');Configuration configuration = new Configuration();StandardServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();session.persist(message);tx.commit();return '數(shù)據(jù)添加成功!';
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]的問題解決了,但又出現(xiàn)了org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment],同樣求助!
回答2:查看web.xml配置,如下:
<!-- 加載Spring --><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- spring默認(rèn)的配置文件名稱是:applicationContext.xml,如果是默認(rèn)則不需要配置 --><context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/daoContext.xml</param-value></context-param>
其中daoContext.xml中就配置了數(shù)據(jù)源、sessionFactory,事務(wù)管理器、事務(wù);你的有加這些嗎?看錯(cuò)誤是沒有
相關(guān)文章:
1. mysql - 在不允許改動(dòng)數(shù)據(jù)表的情況下,如何優(yōu)化以varchar格式存儲(chǔ)的時(shí)間的比較?2. javascript - 網(wǎng)頁打印頁另存為pdf的代碼一個(gè)問題3. vim - docker中新的ubuntu12.04鏡像,運(yùn)行vi提示,找不到命名.4. docker網(wǎng)絡(luò)端口映射,沒有方便點(diǎn)的操作方法么?5. css - chrome下a標(biāo)簽嵌套img 顯示會(huì)多個(gè)小箭頭?6. java中返回一個(gè)對象,和輸出對像的值,意義在哪兒7. css3 - 純css實(shí)現(xiàn)點(diǎn)擊特效8. javascript - 關(guān)于apply()與call()的問題9. 推薦好用mysql管理工具?for mac和pc10. javascript - 有適合開發(fā)手機(jī)端Html5網(wǎng)頁小游戲的前端框架嗎?
