Spring Data Jpa

Maven依赖

<dependency>
    <groupId>org.springframework.datagroupId>
    <artifactId>spring-data-jpaartifactId>
    <version>1.11.7.RELEASEversion>
dependency>

引入后自动依赖jar包

Spring Data Jpa_第1张图片

使用

配置xml

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.logicalcobwebs.proxool.ProxoolDriver"/>
        <property name="url" value="proxool.asset"/>
    bean>

    
    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="packagesToScan" value="com.gmtx" />
        
        <property name="persistenceProvider">
            <bean class="org.hibernate.ejb.HibernatePersistence" />
        property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                
                <property name="generateDdl" value="true" />
                <property name="database" value="MYSQL" />
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
                <property name="showSql" value="true" />
            bean>
        property>
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        property>
    bean>    

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

    
    <jpa:repositories base-package="com.gmtx" entity-manager-factory-ref="entityManagerFactory"  transaction-manager-ref="transactionManager" />

Spring Data JPA的独特之处就是简单查询根本不用写实现类,直接写接口就好,它能根据你的接口去生成sql.

find+全局修饰+By+实体属性名称+限定词+连接词+(其他实体属性)+OrderBy+排序属性+排序方向.

Spring Data Jpa_第2张图片

报错

java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment()Lorg/springframework/core/env/Environment;

spring版本过低导致,最少需要4.2.5

报错的这个类存在于spring-beans中

 

你可能感兴趣的:(Spring Data Jpa)