n同样需要在Spring的配置文件中配置,基本跟使用注解的配置类似:
<?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:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
">
<context:component-scan base-package="cn.javass">
<context:exclude-filter type="annotation“ expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 开启注解事务 只对当前配置文件有效 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<jpa:repositories
base-package="cn.javass"
repository-impl-postfix="Impl"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager">
</jpa:repositories>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="cn.javass"/>
<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="false"/>
<property name="database" value="MYSQL"/>
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="showSql" value="true"/>
</bean>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.query.substitutions" value="true 1, false 0"/>
<entry key="hibernate.default_batch_fetch_size" value="16"/>
<entry key="hibernate.max_fetch_depth" value="2"/>
<entry key="hibernate.generate_statistics" value="true"/>
<entry key="hibernate.bytecode.use_reflection_optimizer" value="true"/>
<entry key="hibernate.cache.use_second_level_cache" value="false"/>
<entry key="hibernate.cache.use_query_cache" value="false"/>
</map>
</property>
</bean>
<!--事务管理器配置-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"><value>org.gjt.mm.mysql.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/cc?useUnicode=true&characterEncoding=UTF-8</value></property>
<property name="username"> <value>root</value> </property>
<property name="password" value="cc"/>
</bean>
</beans>
配置完成后,可以去运行Client测试一下了,当然数据库和表需要先准备好
也可以在<jpa:repositories>下面添加filter,形如:
<repositories base-package="com.acme.repositories">
<context:exclude-filter type="regex" expression=".*SomeRepository" />
</repositories>
第二章:JpaRepository基本功能
JpaRepository的基本功能示范
具体的看代码演示
其中:Pageable接口的实现类是PageRequest,Page接口的实现类是PageImpl。
示例如下:
Page<UserModel> p = ur.findAll(new PageRequest(0,2,new Sort(new Order(Direction.
DESC,"uuid"))));
System. out.println("list="+p.getContent());
第三章:JpaRepository的查询
直接在接口中定义查询方法,如果是符合规范的,可以不用写实现,目前支持的关键字写法如下: