搭建Spring data jpa spring mvc hibernate5环境

来源:互联网 发布:关于音乐的软件 编辑:程序博客网 时间:2024/06/06 10:59

转载博客:http://blog.csdn.net/anxpp/article/details/51415366
按照博客一步一步的来就可以搭建出来
主要是遇到一个问题:

 “No persistence unit with name 'demo2' found

自己明明就在META-INF文件夹下创建了persistence
.xml文件 可以运行时就是得报这个错

persistentce.xml:

<?xml version="1.0" encoding="UTF-8"?><persistence version="1.0"    xmlns="http://java.sun.com/xml/ns/persistence"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">    <persistence-unit name="demo2" transaction-type="RESOURCE_LOCAL">        <!-- provider>org.eclipse.persistence.jpa.PersistenceProvider</provider -->        <provider>org.hibernate.ejb.HibernatePersistence</provider>        <class>com.ninelephas.meerkat.pojo.User</class>        <!-- MYSql 的连接 -->        <properties>            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />            <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/demo?createDatabaseIfNotExist=true" />            <property name="hibernate.connection.username" value="root" />            <property name="hibernate.connection.password" value="123456liu" />            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />        </properties>    </persistence-unit></persistence>

application.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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:jpa="http://www.springframework.org/schema/data/jpa"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.2.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-4.2.xsd         http://www.springframework.org/schema/aop          http://www.springframework.org/schema/aop/spring-aop-4.2.xsd         http://www.springframework.org/schema/mvc          http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd         http://www.springframework.org/schema/data/jpa           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">    <!-- 开启IOC注解扫描 -->    <import resource=""/>    <context:component-scan base-package="com.demo" />    <bean id="entityManagerFactory"        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">        <property name="jpaVendorAdapter">            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">                <property name="generateDdl" value="true" />                <property name="database" value="MYSQL" />            </bean>        </property>        <property name="persistenceUnitName" value="demo2" />    </bean>    <!-- 开启MVC注解扫描 -->    <mvc:annotation-driven />    <bean        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">        <property name="viewResolvers">            <list>                <bean                    class="org.springframework.web.servlet.view.InternalResourceViewResolver">                    <property name="prefix" value="/WEB-INF/view/" />                    <property name="suffix" value=".html" />                </bean>            </list>        </property>        <!-- 用于将对象转换为 JSON -->        <property name="defaultViews">            <list>                <bean                    class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />            </list>        </property>    </bean>    <!-- 对静态资源文件的访问,将无法mapping到Controller的path交给default servlet handler处理 -->    <mvc:default-servlet-handler />    <!-- 配置事务管理器 -->    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">        <property name="entityManagerFactory" ref="entityManagerFactory" />    </bean>    <!-- 启用 annotation事务 -->    <tx:annotation-driven transaction-manager="transactionManager" />    <!-- 配置Spring Data JPA扫描目录 -->    <jpa:repositories base-package="com.demo" /></beans>

在https://stackoverflow.com/questions/4612882/no-persistence-unit-with-name-product-found
上面找到了答案
需要把META-INF这个文件夹(包括persistentce.xml)一同放到tomcat下 当前项目下的WEB-INF/CLASSES/文件夹下才能不到这个persistence.xml 真的是活久见

原创粉丝点击