博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring-data-jpa 入门资料
阅读量:6572 次
发布时间:2019-06-24

本文共 7219 字,大约阅读时间需要 24 分钟。

hot3.png

spring-data-jpa 入门资料

  • maven环境安装 
org.springframework.data
spring-data-jpa
1.4.3.RELEASE
org.hibernate.javax.persistence
hibernate-jpa-2.1-api
1.0.0.Draft-16
org.hibernate
hibernate-entitymanager
4.2.7.SP1
spring-data-jpa下载jar会自动依赖包,包括spring framework框架包。
1.4.3.RELEASE依赖包是spring framework的版本是3.14
具体如下:
 
除此之外,需要手动选择jpa支持的厂商,我这里选择是hibernate。hibernate-jpa-2.1-api是指的jpa的2.1规范,hibernate-entitymanager是jpa的具体实现。
加入数据库驱动包及spring test
com.h2database
h2
1.3.174
org.springframework
spring-test
3.1.4.RELEASE
test

最终,pom文件如下:

org.springframework.data
spring-data-jpa
1.4.3.RELEASE
org.hibernate.javax.persistence
hibernate-jpa-2.1-api
1.0.0.Draft-16
org.hibernate
hibernate-entitymanager
4.2.7.SP1
com.h2database
h2
1.3.174
org.springframework
spring-test
3.1.4.RELEASE
test

  • 配置 
   在src/main/resources/META-INF目录下,新建persistence.xml,内容如下:

在src/main/resources,新建infrastructure.xml,内容如下:
在src/main/resources,新建log4j.xml,内容如下:
  • 开发 
    User类:
package org.springframework.data.jpa.example.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.NamedQuery; import org.springframework.data.jpa.domain.AbstractPersistable; /**  * Sample user class.  *   * @author Oliver Gierke  * @author Thomas Darimont  */ @Entity @NamedQuery(name = "User.findByTheUsersName", query = "from User u where u.username = ?1") public class User extends AbstractPersistable
{ private static final long serialVersionUID = -2952735933715107252L; @Column(unique = true) private String username; private String firstname; private String lastname; public User() { this(null); } /** * Creates a new user instance. */ public User(Long id) { this.setId(id); } /** * Returns the username. * * @return */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the firstname */ public String getFirstname() { return firstname; } /** * @param firstname the firstname to set */ public void setFirstname(String firstname) { this.firstname = firstname; } /** * @return the lastname */ public String getLastname() { return lastname; } /** * @param lastname the lastname to set */ public void setLastname(String lastname) { this.lastname = lastname; } }

SimpleUserRepository类:

package org.springframework.data.jpa.example.repository.simple; import java.util.List; import org.springframework.data.jpa.example.domain.User; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; /**  * Simple repository interface for {@link User} instances. The interface is used to declare so called query methods,  * methods to retrieve single entities or collections of them.  *   * @author Oliver Gierke  * @author Thomas Darimont  */ public interface SimpleUserRepository extends CrudRepository
{ /** * Find the user with the given username. This method will be translated into a query using the * {@link javax.persistence.NamedQuery} annotation at the {@link User} class. * * @param lastname * @return */ User findByTheUsersName(String username); /** * Find all users with the given lastname. This method will be translated into a query by constructing it directly * from the method name as there is no other query declared. * * @param lastname * @return */ List
findByLastname(String lastname); /** * Returns all users with the given firstname. This method will be translated into a query using the one declared in * the {@link Query} annotation declared one. * * @param firstname * @return */ @Query("select u from User u where u.firstname = ?") List
findByFirstname(String firstname); /** * Returns all users with the given name as first- or lastname. Makes use of the {@link Param} annotation to use named * parameters in queries. This makes the query to method relation much more refactoring safe as the order of the * method parameters is completely irrelevant. * * @param name * @return */ @Query("select u from User u where u.firstname = :name or u.lastname = :name") List
findByFirstnameOrLastname(@Param("name") String name); }

加入spring配置:

  • 测试 
AbstractSimpleUserRepositoryTests类:
package org.springframework.data.jpa.example.repository.simple; import static org.junit.Assert.*; import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.example.domain.User; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; /**  * Intergration test showing the basic usage of {@link SimpleUserRepository}.  *   * @author Oliver Gierke  * @author Thomas Darimont  */ @RunWith(SpringJUnit4ClassRunner.class) //@Transactional public abstract class AbstractSimpleUserRepositoryTests { @Autowired SimpleUserRepository repository; User user; @Before public void setUp() { user = new User(); user.setUsername("foobar"); user.setFirstname("firstname"); user.setLastname("lastname"); } @Test public void findSavedUserById() {     user = repository.save(user); user = repository.findByTheUsersName("foobar"); // assertEquals(user, repository.findOne(user.getId())); } @Test public void findSavedUserByLastname() throws Exception { // user = repository.save(user); List
users = repository.findByLastname("lastname"); // assertNotNull(users); // assertTrue(users.contains(user)); } @Test public void findByFirstnameOrLastname() throws Exception { // user = repository.save(user); List
users = repository.findByFirstnameOrLastname("lastname"); // assertTrue(users.contains(user)); } }

XmlConfigSimpleUserRepositoryTests类:
package org.springframework.data.jpa.example.repository.simple; import org.springframework.test.context.ContextConfiguration; /**  * @author Thomas Darimont  */ @ContextConfiguration(locations = "/simple-repository-context.xml") public class XmlConfigSimpleUserRepositoryTests extends AbstractSimpleUserRepositoryTests {}
  • 结果 
 

转载于:https://my.oschina.net/u/1177247/blog/185613

你可能感兴趣的文章
基于 jQuery & CSS3 实现智能提示输入框光标位置
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
ThreadLocal分析
查看>>
mysql优化:连接数
查看>>
github的使用(git shell )
查看>>
如何优化js代码(1)——字符串的拼接
查看>>
PHP 时间操作 / 跳转问题
查看>>
Windows 2012 R2 FSMO角色相关小记录
查看>>
2017年6月12日笔记
查看>>
(小蚂蚁站长吧)网站优化做好这八步你就是seo第一
查看>>
使用流的方式往页面前台输出图片
查看>>
java核心技术反射
查看>>
我的友情链接
查看>>
Maven创建新的依赖项目
查看>>
2015年10月26日作业
查看>>
LAMP,安装脚本
查看>>
面向对象题目
查看>>
Java异常总结
查看>>
DHCP
查看>>