转载

Spring-Data的来历

预计阅读时间: 20 分钟

Spring Data 在保持Spring一致方式上,提供了模板代码和尽可能的让你垂直同各种NoSQL仓库进行交互。根据你的需要,你可以通过定义Spring Data接口的硬性编码风格,为你整个项目开发一种持久化逻辑。

从Spring Data域对象和仓库开始说起,我们要学习两种实现Spring queries(数据查询)方式:1.命名约束 2.使用QueryDSL(一种在编译时确保类型安全的查询验证)

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>    <groupId>com.geekcap.javaworld</groupId>   <artifactId>spring-data-example</artifactId>   <version>1.0-SNAPSHOT</version>   <packaging>jar</packaging>    <name>spring-data-example</name>   <url>http://maven.apache.org</url>      <properties>         <spring.version>3.2.1.RELEASE</spring.version>         <spring.data.mongodb.version>1.2.0.RELEASE</spring.data.mongodb.version>         <java.version>1.6</java.version>         <querydsl.version>2.9.0</querydsl.version>     </properties>    <dependencies>        <!-- Spring -->       <dependency>           <groupId>org.springframework</groupId>           <artifactId>spring-core</artifactId>           <version>${spring.version}</version>           <exclusions>               <exclusion>                   <groupId>commons-logging</groupId>                   <artifactId>commons-logging</artifactId>               </exclusion>           </exclusions>       </dependency>       <dependency>           <groupId>org.springframework</groupId>           <artifactId>spring-beans</artifactId>           <version>${spring.version}</version>       </dependency>       <dependency>           <groupId>org.springframework</groupId>           <artifactId>spring-context</artifactId>           <version>${spring.version}</version>       </dependency>       <dependency>           <groupId>org.springframework</groupId>           <artifactId>spring-aop</artifactId>           <version>${spring.version}</version>       </dependency>        <!-- Spring & Spring Data MongoDB -->       <dependency>           <groupId>org.springframework.data</groupId>           <artifactId>spring-data-mongodb</artifactId>           <version>${spring.data.mongodb.version}</version>       </dependency>       <dependency>           <groupId>org.springframework</groupId>           <artifactId>spring-tx</artifactId>           <version>${spring.version}</version>       </dependency>       <dependency>           <groupId>org.springframework</groupId>           <artifactId>spring-expression</artifactId>           <version>${spring.version}</version>       </dependency>        <!-- Querydsl -->       <dependency>           <groupId>com.mysema.querydsl</groupId>           <artifactId>querydsl-mongodb</artifactId>           <version>${querydsl.version}</version>       </dependency>        <!-- Test -->       <dependency>           <groupId>junit</groupId>           <artifactId>junit</artifactId>           <version>4.11</version>           <scope>test</scope>       </dependency>       <dependency>           <groupId>org.springframework</groupId>           <artifactId>spring-test</artifactId>           <version>${spring.version}</version>           <scope>test</scope>       </dependency>    </dependencies>      <build>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <configuration>                     <source>${java.version}</source>                     <target>${java.version}</target>                 </configuration>             </plugin>              <plugin>                 <groupId>com.mysema.maven</groupId>                 <artifactId>apt-maven-plugin</artifactId>                 <version>1.0.8</version>                 <executions>                     <execution>                         <phase>generate-sources</phase>                         <goals>                             <goal>process</goal>                         </goals>                         <configuration>                             <outputDirectory>target/generated-sources</outputDirectory>                             <processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>                         </configuration>                     </execution>                 </executions>             </plugin>         </plugins>     </build> </project>

applicationContext.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:p="http://www.springframework.org/schema/p"        xmlns:mongo="http://www.springframework.org/schema/data/mongo"        xmlns:context="http://www.springframework.org/schema/context"        xmlns:oxm="http://www.springframework.org/schema/oxm"        xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.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                            http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd">       <!-- Component scan to find all Spring components -->     <context:component-scan base-package="com.geekcap.javaworld.springdata" />      <!-- Load Spring Data Mongo Support -->     <mongo:db-factory id="mongoDbFactory" dbname="SpringDataExample" host="localhost" port="27017"/>      <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">         <constructor-arg ref="mongoDbFactory" />         <property name="writeConcern" value="SAFE" />     </bean>      <!-- Ask Spring Data to scan our domain objects -->     <mongo:mapping-converter id="mongoConverter" base-package="com.geekcap.javaworld.springdata.model" />      <!-- Ask Spring Data to scan our repositories -->     <mongo:repositories base-package="com.geekcap.javaworld.springdata.repository" />  </beans>
原文  http://boke.iflsy.com/archives/1375
正文到此结束
Loading...