2016-01-20 61 views
0

我试图用SQL数据库连接我的项目,但是我有连接问题。使用MySql连接Spring MVC

ERRORS:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa] 
Offending resource: ServletContext resource [/WEB-INF/spring/webcontext/DispatcherServlet-context.xml] 

resources.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.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_2_0.xsd"> 
    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <properties> 
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
      <property name="javax.persistence.jdbc.user" value="root" /> 
      <property name="javax.persistence.jdbc.password" value="haslo123" /> 
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/webstore" /> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> 
      <property name="hibernate.hbm2ddl.auto" value="update" /> 
     </properties> 
    </persistence-unit> 
</persistence> 

的DispatcherServlet-context.xml中

<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:jpa="http://www.springframework.org/schema/data/jpa" 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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 


    <context:component-scan base-package="com.packt.webstore" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

    <jpa:repositories base-package="com.packt.webstore"/> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="defaultPersistenceUnit"/> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager"/> 

</beans> 

InMemoryProductRepository:

@Repository 
public class InMemoryProductRepository implements ProductRepository { 

    private List<Product> listOfProducts = new ArrayList<Product>(); 


    @PersistenceContext 
    EntityManager entityManager; 

    @Transactional 
    public Product load() { 
     Product product = entityManager.find(Product.class, "1"); 
     return product; 
    } 

    public InMemoryProductRepository() { 

     listOfProducts.add(load()); 
    } 

    public List<Product> getAllProducts() { 
     return listOfProducts; 
    } 
} 

产品:

@Entity(name="Product") 
public class Product { 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
    private String productId; 
@Basic 
    private String name; 
@Basic 
    private BigDecimal unitPrice; 
@Basic 
    private String description; 
@Basic 
    private String manufacturer; 
@Basic 
    private String category; 

.. 
} 

而且我不知道,如果是resource.xml在正确的位置:

It's my project tree

谁能帮助我?

+0

resource.xml位于META-INF文件夹中。 – Jack937

+1

您可能会缺少spring-data-jpa依赖关系 – Tome

+0

,并且它不起作用 – Jack937

回答

0

它似乎有一些缺失的依赖或在你的依赖中有几个Spring数据Jar。一些弹簧罐包含具有相同名称的元信息文件。 首先尝试在pom.xml文件中添加下面的依赖关系。

<dependency> 
<groupId>org.springframework.data</groupId> 
<artifactId>spring-data-jpa</artifactId> 

如果不解决不是跟随下面的链接 http://robert-reiz.com/2011/11/14/832/#comment-506

0

你为什么不与Spring配置配置EntityManager的这样吗?它更简单,并且不需要为entityManager提供额外的persistence.xml配置。

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> 
    <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=" /> 
    <property name="username" value="sa" /> 
    <property name="password" value="12345" /> 
</bean> 


<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="model-nadra" /> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
    </property> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.dialect" >org.hibernate.dialect.SQLServer2008Dialect</prop> 
      <prop key="hibernate.show_sql" >true</prop> 
      <prop key="hibernate.format_sql" >true</prop> 
      <prop key="hibernate.hbm2ddl.auto" >update</prop> 
     </props> 
    </property> 
</bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    <property name="dataSource" ref="dataSource" /> 
</bean>