Spring 弹簧&x2B;JPA&x2B;Postgresql配置

Spring 弹簧&x2B;JPA&x2B;Postgresql配置,spring,postgresql,jpa,Spring,Postgresql,Jpa,从头开始创建一个简单的SpringMVC。我的配置是Spring+JPA+Postgresql。我希望使用XML完成配置,但无法使用JPA设置数据库连接配置。我想从头开始配置它,并想了解它的每个部分 data-context.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.o

从头开始创建一个简单的SpringMVC。我的配置是Spring+JPA+Postgresql。我希望使用XML完成配置,但无法使用JPA设置数据库连接配置。我想从头开始配置它,并想了解它的每个部分

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

<context:property-placeholder location="file:${DB_CONFIG}" />

<jpa:repositories base-package="com.spring.mvc.repository" transaction-manager-ref="transactionManager"  />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.spring.mvc.domain" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
            <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
            <prop     key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
        </props>
    </property>
</bean>

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

<bean id="persistenceExceptionTranslationPostProcessor"
  class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
</beans>
 <?xml version="1.0" encoding="UTF-8"?>
 <beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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">

<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.spring.mvc" />
<?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:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->
<beans:import resource="classpath*:spring/data-context.xml" />
</beans>

您配置的初始问题

当存储库处于

com.spring.mvc.repository

所以你需要

请注意,
用于配置Spring数据JPA存储库,因此您需要指定包含存储库的包,而不是包含实体的包

还请注意,您可以删除部分
事务管理器ref=“transactionManager”
,因为您只使用一个事务管理器,因此Spring Data JPA将默认为该事务管理器。查看部分文档

您可能还需要将
entityManagerFactory
的配置更改为

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

        <property name="packagesToScan" >
            <list>
                <value>com.spring.mvc.domain</value>
            </list>
        </property>

        <property name="jpaProperties">
            <props>
                <!-- whatever you need here -->
            </props>
        </property>
    </bean>

com.spring.mvc.domain
请看一篇博客文章,以获取有关使用JPA配置Spring的完整指南


最后一点,我知道您说过要显式地配置所有内容,以便了解Spring的详细信息,但我希望您看看。它将消除因需要配置Spring而带来的大部分痛苦。

<jpa:repositories base-package="com.spring.mvc.domain" transaction-manager-ref="transactionManager">



感谢您的回复,但错误仍然是一样的。你能告诉我如何正确配置它吗?你能给我推荐一个关于配置部分的好教程吗?我做了教程中提到的所有事情,但错误仍然是sameupdate data-context.xml,added root-context.xml,servlet-context.xml,web.xml
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.mvc.repository.ContactRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />

        <property name="packagesToScan" >
            <list>
                <value>com.spring.mvc.domain</value>
            </list>
        </property>

        <property name="jpaProperties">
            <props>
                <!-- whatever you need here -->
            </props>
        </property>
    </bean>
<jpa:repositories base-package="com.spring.mvc.domain" transaction-manager-ref="transactionManager">
<jpa:repositories base-package="com.spring.mvc.repository" transaction-manager-ref="transactionManager">