简体   繁体   English

在春季开始使用JPA

[英]start using jpa in spring

Im really dont get it. 我真的不明白。 I download STS Spring Tool Suite, and i want to use JPA in my MVC app. 我下载了STS Spring Tool Suite,并且想在我的MVC应用程序中使用JPA。 I have to add some JAR's? 我必须添加一些JAR吗? where can i find that JAR's, how do i know what JAR's are? 我在哪里可以找到JAR,我怎么知道JAR是什么? what i have to add to my pom.xml file? 我必须添加到pom.xml文件中的内容是什么?

I already checked many sites but anyone tell exactly how to make it work. 我已经检查了许多站点,但是没有人确切说明如何使它工作。 Its like the assume you know some steps that i really dont know what steps are. 它就像假设您知道一些步骤,我真的不知道什么步骤。

Then, if i create more than one repository for differents objects, they do the transactions to the same database? 然后,如果我为不同对象创建多个存储库,那么它们将事务存储到同一数据库吗?

在pom.xml中使用它

<dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency>

I recommend visiting http://mvnrepository.com . 我建议访问http://mvnrepository.com I have used that in the past to find out what I need to add to the POM. 过去,我曾用它来找出我需要添加到POM中的内容。 Also, IDEs like IntelliJ and Eclipse make this really easy by providing arch-types, etc. For example, to get spring go here: http://mvnrepository.com/artifact/org.springframework/spring-context/4.1.0.RELEASE . 此外,像IntelliJ和Eclipse这样的IDE通过提供arch-types等也使此操作变得非常容易。例如,要获取spring,请访问: http : //mvnrepository.com/artifact/org.springframework/spring-context/4.1.0。发布

Here is something to get your started. 这是一些入门的信息。 Just add these dependencies and you'll get jpa along with spring. 只需添加这些依赖项,您就会在Spring中获得jpa。 Removing the persistence this is what you would need to make a simple spring mvc web server. 除去持久性,这就是制作简单的Spring MVC Web服务器所需要的。 With the persistence, you'll have to set things up. 有了持久性,您就必须进行设置。

   <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>

There are lots of example on the net. 网上有很多例子。 I'm sure if you searched you could find something. 我敢肯定,如果您搜索过,就能找到一些东西。 Anyway I have a project here you can use to get started: 无论如何,我在这里有一个项目可以用来入门:

https://github.com/alanhay/spring-data-jpa-bootstrap https://github.com/alanhay/spring-data-jpa-bootstrap

This is a minimal project that has everything you need including some unit tests that will run against an in-memory HSQLDB database. 这是一个最小的项目,包含您需要的所有内容,包括一些将针对内存中的HSQLDB数据库运行的单元测试。

You can download the ZIP and import to Eclipse/STS or check it out as a git project. 您可以下载ZIP并将其导入Eclipse / STS或将其作为git项目签出。 For the ZIP: 对于ZIP:

  • download, unzip and then in Eclipse/STS select Import > Existing Maven Project > Select the unzipped folder. 下载,解压缩,然后在Eclipse / STS中选择导入>现有Maven项目>选择解压缩的文件夹。

  • Once imported right click on the project and run mvn > install (because the project uses QueryDSL requires a full Maven build rather than Eclipse incremental build but there is a plugin you can setup). 导入后,右键单击该项目并运行mvn> install(因为该项目使用QueryDSL,因此需要完整的Maven构建而不是Eclipse增量构建,但是可以安装一个插件)。

  • Open the test class UserRepositoryTest and run. 打开测试类UserRepositoryTest并运行。

Also, further to the comments above, the persistence-api is just a specification. 另外,除了上面的评论,persistence-api只是一个规范。 You also need to add dependency for an implementation eg Hibernate, OpenJPA, EclipseLink. 您还需要为实现添加依赖,例如Hibernate,OpenJPA,EclipseLink。 My example project uses Hibernate. 我的示例项目使用Hibernate。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM