我試圖用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在正確的位置:
誰能幫助我?
resource.xml位於META-INF文件夾中。 – Jack937
您可能會缺少spring-data-jpa依賴關係 – Tome
,並且它不起作用 – Jack937