- 浏览: 131981 次
文章分类
- 全部博客 (149)
- Java (41)
- 设计模式 (23)
- php (2)
- C++ (2)
- Linux (6)
- C (1)
- JavaEE (27)
- javascript (2)
- uplodify (1)
- Mybatis (2)
- Maven (4)
- Lucene (3)
- commons (1)
- Spring (7)
- SQL (3)
- mahout (1)
- MySQL (2)
- extjs4.2.1 (2)
- ubuntu (1)
- hadoop (1)
- hibernate (1)
- Spring JPA (2)
- JPA 2.0 (1)
- express (1)
- redis (2)
- angularjs (1)
- git (1)
- Python (1)
- logging (2)
最新评论
-
xlaohe1:
controller返回的是一个POJO对象,然后用@Resp ...
Spring MVC 4.X ResponseBody 日期类型Json 处理 -
TRAMP_ZZY:
能帮到你,我也很高兴。
Spring MVC 4.X ResponseBody 日期类型Json 处理 -
jobzjc:
第一段有帮到我。如果是非对象,Object方式传递的时候,第一 ...
Spring MVC 4.X ResponseBody 日期类型Json 处理 -
TRAMP_ZZY:
dingran 写道为什么,我怎么就没找到System > ...
Ubuntu 12.04 设置 IBus 开机启动 -
dingran:
为什么,我怎么就没找到System >> Pref ...
Ubuntu 12.04 设置 IBus 开机启动
[url][/url]
1. Sping Data JPA 创建查找的顺序
a. CREATE attempts to construct a store-specific query from the query method name. The general approach is to remove a given set of well-known prefixes from the method name and parse the rest of the method. Read more about query construction in the section called “Query creation”.
b. USE_DECLARED_QUERY tries to find a declared query and will throw an exception in case it can't find one. The query can be defined by an annotation somewhere or declared by other means. Consult the documentation of the specific store to find available options for that store. If the repository infrastructure does not find a declared query for the method at bootstrap time, it fails.
c. CREATE_IF_NOT_FOUND (default) combines CREATE and USE_DECLARED_QUERY. It looks up a declared query first, and if no declared query is found, it creates a custom method name-based query. This is the default lookup strategy and thus will be used if you do not configure anything explicitly. It allows quick query definition by method names but also custom-tuning of these queries by introducing declared queries as needed.
2. 查询创建机制
(1) 有用的前缀 find…By, read…By, query…By, count…By, and get…By,同时可以在查询语句前面加上 Distinct 来创建语句。
(2) 查询方法的创建
http://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/index.html
1. Sping Data JPA 创建查找的顺序
a. CREATE attempts to construct a store-specific query from the query method name. The general approach is to remove a given set of well-known prefixes from the method name and parse the rest of the method. Read more about query construction in the section called “Query creation”.
b. USE_DECLARED_QUERY tries to find a declared query and will throw an exception in case it can't find one. The query can be defined by an annotation somewhere or declared by other means. Consult the documentation of the specific store to find available options for that store. If the repository infrastructure does not find a declared query for the method at bootstrap time, it fails.
c. CREATE_IF_NOT_FOUND (default) combines CREATE and USE_DECLARED_QUERY. It looks up a declared query first, and if no declared query is found, it creates a custom method name-based query. This is the default lookup strategy and thus will be used if you do not configure anything explicitly. It allows quick query definition by method names but also custom-tuning of these queries by introducing declared queries as needed.
2. 查询创建机制
(1) 有用的前缀 find…By, read…By, query…By, count…By, and get…By,同时可以在查询语句前面加上 Distinct 来创建语句。
(2) 查询方法的创建
@Repository public class UserDaoImpl implements UserDao { @PersistenceContext private EntityManager entityManager; @Override public User findByName(String name) { TypedQuery<User> query = this.entityManager.createQuery("select u from User u where u.name = :name", User.class); query.setParameter("name", name); return query.getSingleResult(); } @Override public List<User> findAll() { TypedQuery<User> query = this.entityManager.createQuery("select u from User u", User.class); return query.getResultList(); } @Override public void saveOrUpdate(User user) { this.entityManager.persist(user); } } @NoRepositoryBean public interface AbstractRepository<T extends AbstractEntity> extends Repository<T, Long> { @Query("select t from #{#entityName} t where t.id = ?1") List<T> findAllById(Long id); } @MappedSuperclass public class AbstractEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; /** * Returns the identifier of the entity. * * @return the id */ public Long getId() { return id; } /* * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (this.id == null || obj == null || !(this.getClass().equals(obj.getClass()))) { return false; } AbstractEntity that = (AbstractEntity) obj; return this.id.equals(that.getId()); } /* * (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { return id == null ? 0 : id.hashCode(); } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } }
public interface UserRepository extends Repository<User, Integer>, UserRepositoryCustom { List<User> findByNameAndEmail(String name, String email); // Enable the distinct flag for the query List<User> findDistinctUserByNameOrEmail(String name, String email); List<User> findUserDistinctUserByNameOrEmail(String name, String email); // Enabling ignoring case for an individual property List<User> findByNameIgnoreCase(String name); // Enabling static ORDER BY for a query List<User> findByNameAndEmailAllIgnoreCase(String name, String email); // Enabling static ORDER BY for a query @Lock(LockModeType.READ) List<User> findByNameOrderByUserIdAsc(String name); List<User> findByNameOrderByUserIdDesc(String name); Page<User> findByNameLike(String name, Pageable pageable); List<User> findByNameLike(String name, Sort sort); // User @Query @Query("select u from User u where u.email = ?1") User findByEmail(String email); @Query("select u from User u where u.name like %?1") List<User> findByNameEndsWith(String name); //User @Query to execute Navtie Query @Query(value = "select * from user where email = ?0", nativeQuery = true) User findByEmailAddress(String email); // Modify @Modifying(clearAutomatically = true) @Query("update User u set u.name = ?1 where u.id = ?2") int setFixedNameFor(String name, Integer id); }
/* * 选用CrudRepository 接口是为了暴露方法,因为默认的SimpleJpaRepository 已经实现了很多方法 * 必须暴露出来,才能调用 */ public interface ProductRepository extends CrudRepository<Product, Long>{ public abstract Page<Product> findByDescriptionContaining(String description, Pageable pageable); @Query("select p from Product p where p.attributes[?1] = ?2") public abstract List<Product> findByArrtibuteAndValue(String attribute, String value); public abstract Long countByName(String name); public List<Product> findByName(String name); }
public interface DocumentRepository extends JpaRepository<Document, Integer>, CrudRepository<Document, Integer>{ List<Document> findByDocName(String docName); @Query(value="select d from Document d where d.docType=:docType") public Document findByDocType(@Param("docType") Byte docType); public List<Document> findByUploadAuthorLike(String uploadAuthor); @Query("select d from Document d where d.author=:author") public List<Document> findByAuthorLike(String author); public List<Document> findByDocTypeBetween(Byte begin, Byte end); }
@Repository @Transactional(readOnly = true) public class JpaCustomerDaoImpl implements CustomerDao { @PersistenceContext private EntityManager em; @Override @Transactional public Customer save(Customer customer) { if (customer.getId() != null) { em.persist(customer); return customer; } else { return em.merge(customer); } } @Override public Customer findByEmailAddress(EmailAddress emailAddress) { String sql = " select c from Customer c where c.emailAddress = :emailAddress"; TypedQuery<Customer> query = em.createQuery(sql, Customer.class); query.setParameter("emailAddress", emailAddress); return query.getSingleResult(); } }
http://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/index.html
发表评论
-
ServletContainerInitializer 初始化器
2016-08-24 14:29 5000概述 为了实现不通过web ... -
Tomcat 7.X 配置https
2014-11-10 11:47 446http://tomcat.apache.org/t ... -
Spring 4.0.3+JPA(hibernate 4.3.5Final 实现)+Maven 配置文件入门实例
2014-06-27 09:42 3214本案例是基于Spring 实现的JPA实例。JPA为SUN公 ... -
Spring 4.0.3+Hibernate4.3.5+Maven 配置文件
2014-06-21 09:43 2247<?xml version="1.0&qu ... -
JavaEE Filter和Listener 分析
2014-02-18 14:18 7361. Filter 使程序可以改变 ... -
Servlet 3.0 文件上传新特性
2014-02-15 21:55 459Servlet 3.0 实现了文件上传的功能,通过注解的方式指 ... -
ServletContext 与ServletConfig剖析
2014-02-15 20:44 5921. ServletContext 是一个全局的储存信息的空间 ... -
JavaMail Spring Mail支持
2014-02-15 15:54 6931. 普通的JavaMail 发送和接受邮件 public ... -
Jdbc 数据库连接池简易实现和JdbcUtils
2014-01-20 16:42 631public class MyDataSource { ... -
Java反射将Jdbc查询结果封装为对象
2014-01-19 10:24 1246public class ORMTest { pu ... -
Spring JdbcTemplate CRUD 操作
2014-01-11 12:12 872/** * Project Name:cjxy ... -
JavaEE 下载文件中文乱码兼容多浏览器
2013-12-11 11:02 468public static String encodeF ... -
Spring 3.2.* MVC通过Ajax获取JSON数据报406错误
2013-09-12 12:13 586Spring 3.2.* MVC通过Ajax获取JSON数据报 ... -
Spring SqlQuery 使用
2013-09-11 17:52 641/** * Project Name:webblog ... -
JavaEE pager-taglib 分页插件(2)
2013-08-12 17:56 374/** * Project Name:TestPag ... -
JavaEE pager-taglib 分页插件(1)
2013-08-12 16:47 1064pg:pager 这个标签用来 ... -
Web 安全与 过滤器
2013-06-03 16:51 6891. Servlet 安全的四大要 ... -
JSP学习笔记二
2013-06-03 15:36 7351. JSP 标准动作获取 Jav ... -
JSP学习笔记一
2013-05-24 16:26 7171. JSP 最终还是会变成一个完整的Servlet 在W ... -
Servlet学习笔记(二)
2013-05-23 17:30 6641. Servlet 的初始化参数 配置: < ...
相关推荐
spring data jpa 实例源码 spring data jpa 实例源码 spring data jpa 实例源码
《你真的了解Jpa吗?》案例代码,内有SpringBootJPA的入门例子,包括增删改查一级实体关联关系处理。
'SpringDataJPA从入门到精通'分为12章 内容包括整体认识JPA、JPA基础查询方法、定义查询方法、注解式查询方法、@Entity实例里面常用注解详解、JpaRepository扩展详解、JPA的MVC扩展REST支持、DataSource的配置、乐观...
spring Data家族给我们提供了一个现成的dao层框架,这里面有不同的项目,如Spring Data JPA, Spring Data Neo4j and Spring Data MongoDB,他们的共同特点是他们给我们提供了框架代码,不再需要我们自己去实现了。
基础框架,可满足开发需要
2017 spring data jpa+spring4.2+springmvc+hibernate4.3 maven环境intellij idea增删改查实例
用了spring data Jpa做的增删改查登录分页查询的简单实例
Spring+Spring MVC+SpringData JPA整合完成增删改查,翻页实例.zip
springDataJpa入门项目实例,基本都方法都很全,本人编写,大家一起交流学习.
spring boot,持久层的使用实例,其中使用了JPA 、Hibernate、Spring Data、Spring Data JPA、JdbcTemplate
SpringMVC +Hibernate JPA+Spring-data-jpa实例 是入门的实例。
主要介绍了Spring Boot整合Spring Data Jpa代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
idea工具spring整合jpa工程实例 1.已成功测试 2.根据自己用的数据库或者工具去修改部分配置即可
- eclipselink:展示了如何在Spring Boot和Eclipselink中使用Spring Data JPA的示例项目。 - example:包含了各种示例包,展示了使用Spring Data JPA的不同级别。可以查看simple包以获取最基本的设置。 - ...
本篇文章主要介绍了Spring Data JPA调用存储过程实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
本篇文章主要介绍了Spring Data JPA实现分页Pageable的实例代码,具有一定的参考价值,有兴趣的可以了解一下
实现spring-data-jpa的增删改查方法,该项目是maven项目,需要maven支持
主要介绍了Spring Data JPA例子代码[基于Spring Boot、Mysql],小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧