jpa中文手册(jpa 文档)
通常所说的jpa指的是啥
1. JPA概念
Java persistence API的简称,中文名是Java持久层API,
是JDK5.0注解或XML描述对象-关系表的映射关系,
并将运行期的实体对象持久化到数据库中。
(对象持久化:是将内存中的对象保存到可永久保存的存储设备中的一种技术)
2. JPA出现的原因
1.简化现有JavaEE和JavaSE应用的对象持久化的开发工作;
2.Sun希望整合ORM技术,实现在持久化领域的统一应用;
3. JPA提供的技术
1.ORM映射元数据
JPA支持XML和JDK5.0注解两种元数据的形式,元数据描述对象和表之间的映射关系框架据此将实体对象持久化到数据库表中;(元数据:是指用来描述数据的数据,就是描述代码间关系的数据,如:hibernate是用hbm文件。JDK5.0出来后,java语言中就有了四种类型(TYPE),即类(class)、枚举(enum)、接口(interface)和注解(@interface),它们是处在同一级别的。java就是通过注解来表示元数据的。)
2.JPA的API
用来操作实体对象,执行crud操作,框架在底层替我们完成所有的事情,开发者从繁琐的JDBC 和 SQL代码中解脱;
3.查询语言
通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合;
JPA实用手册,即看即用
修改查询
@Modifying
@Query("update User u set u.firstname = ?1 where u.lastname = ?2")
int setFixedFirstnameFor(String firstname, String lastname);
使用 Sort 和 JpaSort
public interface UserRepository extends JpaRepository {
? @Query("select u from User u where u.lastname like ?1%")
? List findByAndSort(String lastname, Sort sort);
? @Query("select u.id, LENGTH(u.firstname) as fn_len from User u where u.lastname like ?1%")
? List findByAsArrayAndSort(String lastname, Sort sort);
}
repo.findByAndSort("lannister", new Sort("firstname"));? ? ? ? ? ?
repo.findByAndSort("stark", new Sort("LENGTH(firstname)"));? ? ? ?
repo.findByAndSort("targaryen", JpaSort.unsafe("LENGTH(firstname)"));
repo.findByAsArrayAndSort("bolton", new Sort("fn_len"));? ?
使用已命名参数
public interface UserRepository extends JpaRepository {
? @Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname")
? User findByLastnameOrFirstname(@Param("lastname") String lastname,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Param("firstname") String firstname);
}? ?
原生SQL分页
public interface UserRepository extends JpaRepository {
? @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
? ? countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
? ? nativeQuery = true)
? Page findByLastname(String lastname, Pageable pageable);
}
Sort sort =newSort(Sort.Direction.DESC,"createTime");//创建时间降序排序Pageable pageable =newPageRequest(pageNumber,pageSize,sort);
使用原生SQL
public interface UserRepository extends JpaRepository {
? @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
? User findByEmailAddress(String emailAddress);
}
为了消除不确定性,可以在方法名内使用下划线“_”手动定义隔断点。
List findByAddress_ZipCode(ZipCode zipCode);
查询方法建立
distinct flag
ignoring case
order by
public interface PersonRepository extends Repository {
? List findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
? // Enables the distinct flag for the query? List findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
? List findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);
? // Enabling ignoring case for an individual property? List findByLastnameIgnoreCase(String lastname);
? // Enabling ignoring case for all suitable properties? List findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);
? // Enabling static ORDER BY for a query? List findByLastnameOrderByFirstnameAsc(String lastname);
? List findByLastnameOrderByFirstnameDesc(String lastname);
}
异步查询结果
@Async
Future findByFirstname(String firstname);? ? ? ? ? ?
@Async
CompletableFuture findOneByFirstname(String firstname);
@Async
ListenableFuture findOneByLastname(String lastname);
Like模糊查询
@Query(value = "select name,author,price from Book b where b.name like %:name%")
List findByNameMatch(@Param("name") String name);
In 查询
@Query(value = "select * from trade$seek_purchase_offer where sp_id in (:spIds) and of_enuu = :enUu", nativeQuery = true)
? ? List getSeekPurchaseOfferList(@Param("spIds") List spIds, @Param("enUu") Long enUu);
MappedSuperClass:
映射为非实体父类,该实体父类不会生成对应的数据表
@OneToOne
@Entity
@Table(name = "costume_all_id")
public class AllId extends AbstractEntity {
? ? private static final long serialVersionUID = 1L;
? ? @OneToOne(cascade = CascadeType.ALL)
? ? @JoinColumn(name = "costume_member_fk")
? ? private Member member;// 用户表外键
}
@OneToMany和@ManyToOne
@Entity
@Table(name = "costume_organization")
public class Organization extends AbstractEntity {
? ? private static final long serialVersionUID = 1L;
? ? @Column(nullable = false, length = 50)
? ? private String name; // 组织名称
? ? @OneToMany(mappedBy = "organization")
? ? private Set departmentSet; // 部门集合
}
@Entity
@Table(name = "costume_department")
public class Department extends AbstractEntity {
? ? private static final long serialVersionUID = 1L;
? ? @Column(nullable = false, length = 50)
? ? private String name; // 部门名称
? ? @ManyToOne(optional = false)
? ? private Organization organization; // 组织外键
? ? @ManyToMany
? ? private Set memberSet; // 用户表外键
? ? public Organization getOrganization() {
? ? ? ? return organization;
? ? }
? ? @JsonBackReference
? ? public void setOrganization(Organization organization) {
? ? ? ? this.organization = organization;
? ? }
}
@ManyToMany
Entity
@Table(name = "costume_member")
public class Member extends AbstractEntity {
? ? private static final long serialVersionUID = 1L;
? ? @Column(nullable = false, length = 20)
? ? private String name;
? ? @ManyToMany
? ? @JoinTable(joinColumns = { @JoinColumn(name = "member_id") }, inverseJoinColumns = {
? ? ? ? ? ? @JoinColumn(name = "department_id") }) //被控方表字段名
? ? private Set departmentSet; // 部门表外键
? ? public Set getDepartmentSet() {
? ? ? ? return departmentSet;
? ? }
? ? @JsonBackReference
? ? public void setDepartmentSet(Set departmentSet)
? ? {
? ? ? ? this.departmentSet = departmentSet;
? ? }
}
HQL通过旅店名称查询旅店以及城市的所有信息 直接返回实体类
/**
* 关联查询
*
* @return
*/
@Query(value = "select new pers.zpw.domain.CityHohel(t1.name AS cityName,t2.name AS hotelName) from? TCity t1 left? join THotel t2 on t1.id=t2.city where t2.name =:name")
List findCityAndHotelByHQLResultObj(@Param("name") String name);
@Data
public class CityHohel {
? ? ? ? private String cityName;
? ? ? ? private String hotelName;
? ? ? ? public CityHohel(String cityName, String hotelName) {
? ? ? ? ? ? this.cityName = cityName;
? ? ? ? ? ? this.hotelName = hotelName;
? ? ? ? }
}
实例2
@Entity?
@Table(name="orders")?
public class Order {?
? ? private String orderid;?
? ? private Float amount = 0f;?
? ? private Set items = new HashSet();?
? ? @Id?
? ? @Column(length = 12)?
? ? public String getOrderid() {?
? ? ? ? return orderid;?
? ? }?
? ? public void setOrderid(String orderid) {?
? ? ? ? this.orderid = orderid;?
? ? }?
? ? @Column(nullable = false)?
? ? public Float getAmount() {?
? ? ? ? return amount;?
? ? }?
? ? public void setAmount(Float amount) {?
? ? ? ? this.amount = amount;?
? ? }?
@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE },mappedBy ="order") //这里配置关系,并且确定关系维护端和被维护端。mappBy表示关系被维护端,只有关系端有权去更新外键。这里还有注意OneToMany默认的加载方式是赖加载。当看到设置关系中最后一个单词是Many,那么该加载默认为懒加载?
? ? public Set getItems() {?
? ? ? ? return items;?
? ? }?
? ? public void setItems(Set items) {?
? ? ? ? this.items = items;?
? ? }?
? ? ? ? /**?
? ? ? ? ? *该方法用于向order中加order项?
? ? ? ? ? /*?
? ? public void addOrderItem(OrderItem orderItem){?
? ? ? ? orderItem.setOrder(this);//用关系维护端来维护关系?
? ? ? ? this.items.add(orderItem);?
? ? }?
}
@Entity?
public class OrderItem {?
? ? private Integer id;?
? ? private String productName;?
? ? private Float sellPrice = 0f;?
? ? private Order order;?
? ? @Id?
? ? @GeneratedValue?
? ? public Integer getId() {?
? ? ? ? return id;?
? ? }?
? ? public void setId(Integer id) {?
? ? ? ? this.id = id;?
? ? }?
? ? @Column(length = 40, nullable = false)?
? ? public String getProductName() {?
? ? ? ? return productName;?
? ? }?
? ? public void setProductName(String productName) {?
? ? ? ? this.productName = productName;?
? ? }?
? ? @Column(nullable = false)?
? ? public Float getSellPrice() {?
? ? ? ? return sellPrice;?
? ? }?
? ? public void setSellPrice(Float sellPrice) {?
? ? ? ? this.sellPrice = sellPrice;?
? ? }?
? ? @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH }, optional = true)?
? ? @JoinColumn(name="order_id")//这里设置JoinColum设置了外键的名字,并且orderItem是关系维护端?
? ? public Order getOrder() {?
? ? ? ? return order;?
? ? }?
? ? public void setOrder(Order order) {?
? ? ? ? this.order = order;?
? ? }?
}?
缓存
??
????org.springframework.boot??
????spring-boot-starter-cache??
@Configuration??
@EnableCaching??
public?class?CacheConfig?{??
}??
@Cacheable
Spring 在执行 @Cacheable 标注的方法前先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,执行该方法并将方法返回值放进缓存。
参数: value缓存名、 key缓存键值、 condition满足缓存条件、unless否决缓存条件
@Cacheable(value?=?"user",?key?=?"#id")??
public?User?findById(final?Long?id)?{??
????System.out.println("cache?miss,?invoke?find?by?id,?id:"?+?id);??
????for?(User?user?:?users)?{??
????????if?(user.getId().equals(id))?{??
????????????return?user;??
????????}??
????}??
????return?null;??
}??
@CachePut
和 @Cacheable 类似,但会把方法的返回值放入缓存中, 主要用于数据新增和修改方法。
@CachePut(value?=?"user",?key?=?"#user.id")??
public?User?save(User?user)?{??
????users.add(user);??
????return?user;??
}?
@CacheEvict
方法执行成功后会从缓存中移除相应数据。
参数: value缓存名、 key缓存键值、 condition满足缓存条件、 unless否决缓存条件、 allEntries是否移除所有数据(设置为true时会移除所有缓存)
@CacheEvict(value?=?"user",?key?=?"#user.id")?//?移除指定key的数据??
public?User?delete(User?user)?{??
????users.remove(user);??
????return?user;??
}??
@CacheEvict(value?=?"user",?allEntries?=?true)?//?移除所有数据??
public?void?deleteAll()?{??
????users.clear();??
}??
spring.cache.type=none ***设置缓存无效化
集成EhCache
??
????net.sf.ehcache??
????ehcache??
?????????xsi:noNamespaceSchemaLocation="ehcache.xsd"??
??????
src\main\resources/application.properties
spring.cache.ehcache.config=classpath:ehcache.xml
如果想自定义设置一些个性化参数时,通过Java Config形式配置。
@Configuration??
@EnableCaching??
public?class?CacheConfig?{??
????@Bean??
????public?CacheManager?cacheManager()?{??
????????return?new?EhCacheCacheManager(ehCacheCacheManager().getObject());??
????}??
????@Bean??
????public?EhCacheManagerFactoryBean?ehCacheCacheManager()?{??
????????EhCacheManagerFactoryBean?cmfb?=?new?EhCacheManagerFactoryBean();??
????????cmfb.setConfigLocation(new?ClassPathResource("ehcache.xml"));??
????????cmfb.setShared(true);??
????????return?cmfb;??
????}??
}??
组合CacheManager
从多个CacheManager中轮询得到相应的Cache。
@Configuration??
@EnableCaching??
public?class?CacheConfig?{??
????@Bean??
????public?CacheManager?compositeCacheManager(RedisTemplate?redisTemplate)?{??
????????CompositeCacheManager?cacheManager?=?new?CompositeCacheManager(new?ConcurrentMapCacheManager(),?new?SimpleCacheManager());??
????????cacheManager.setFallbackToNoOpCache(false);??
????????cacheManager.afterPropertiesSet();??
????????return?cacheManager;??
????}??

jpa是什么,和hibernate类似?
1、JPA全称Java Persistence API. JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
JPA的主要目标之一就是提供更加简单的编程模型:在JPA框架下创建实体和创建Java 类一样简单,没有任何的约束和限制,只需要使用 javax.persistence.Entity进行注释,JPA的框架和接口也都非常简单,没有太多特别的规则和设计模式的要求,开发者可以很容易地掌握。JPA基于非侵入式原则设计,因此可以很容易地和其它框架或者容器集成。
2、Hibernate是JPA的具体实现。但是Hibernate出现的时间早于JPA。JPA是SUN在持久化框架发展起来后提出的规范。Hibernate从3.2开始,就开始兼容JPA。Hibernate3.2获得了Sun TCK的JPA(JavaPersistence API) 兼容认证。
Hibernate作为JPA的一种实现,jpa的注解已经是hibernate的核心,hibernate只提供了一些补充,而不是两套注解。hibernate对jpa的支持够足量,在使用hibernate注解建议使用jpa。
扩展资料
JPA查询能力
JPA的查询语言是面向对象而非面向数据库的,它以面向对象的自然语法构造查询语句,可以看成是Hibernate HQL的等价物。JPA定义了独特的JPQL(Java Persistence Query Language),JPQL是EJB QL的一种扩展,它是针对实体的一种查询语言,操作对象是实体,而不是关系数据库的表,而且能够支持批量更新和修改、JOIN、GROUP BY、HAVING 等通常只有 SQL 才能够提供的高级查询特性,甚至还能够支持子查询。
高级特性
JPA 中能够支持面向对象的高级特性,如类之间的继承、多态和类之间的复杂关系,这样的支持能够让开发者最大限度的使用面向对象的模型设计企业应用,而不需要自行处理这些特性在关系数据库的持久化。
参考资料来源:百度百科:JPA
java 中文API谁有,百度云分享一下
Android中文版
api手册地址:
Ant最新版
api手册地址:
ASM字节码操作
api手册地址:
Axis2最新版
api手册地址:
Bash脚本
api手册地址:
Bootstrap 3
api手册地址:
Bootstrap 4
api手册地址:
C/C++
api手册地址:
C3P0连接池
api手册地址:
CentOS使用文档
api手册地址:
Commons-Beanutils
api手册地址:
Commons-Fileupload
api手册地址:
Commons-IO最新版
api手册地址:
Commons-Lang最新版
api手册地址:
Commons-Net最新版
api手册地址:
CSS 3
api手册地址:
DBCP连接池
api手册地址:
Dom4j
api手册地址:
dubbo中文文档
api手册地址:
EhCache
api手册地址:
Freemarker
api手册地址:
Go语言
api手册地址:
Hadoop
api手册地址:
Hibernate中文版
api手册地址:
IKAnalyzer中文版
api手册地址:
Java 10
api手册地址:
Java 6
api手册地址:
Java 7
api手册地址:
Java 8中文版
api手册地址:
jqGrid中文版
api手册地址:
Jquery中文版
api手册地址:
Json-lib
api手册地址:
Junit4最新版
api手册地址:
Kryo
api手册地址:
Log4j最新版
api手册地址:
Lucene
api手册地址:
Maven
api手册地址:
Windows MFC中文版
api手册地址:
Mybatis
api手册地址:
MySql中文版
api手册地址:
Netty 3.6
api手册地址:
Nginx中文版
api手册地址:
OpenJPA最新版
api手册地址:
PHP中文版
api手册地址:
POI-apache
api手册地址:
QuickServer
api手册地址:
redis中文参考文档
api手册地址:
Ruby
api手册地址:
Ruby-library
api手册地址:
Ruby on Rails
api手册地址:
Shiro
api手册地址:
Spring最新版
api手册地址:
Spring for Android
api手册地址:
Spring Boot
api手册地址:
Spring Cloud中文文档
api手册地址:
Spring Security
api手册地址:
Spring中文版
api手册地址:
Struts 2最新版
api手册地址:
Taperstry
api手册地址:
TensorFlow中文
api手册地址:
Tomcat
api手册地址:
Ubuntu
api手册地址:
Velocity 1.7
api手册地址:
VelocityTools2.0
api手册地址:
Vue Router中文参考
api手册地址:
vue.js中文文档
api手册地址:
XMLBeans
api手册地址:
Yahoo UI中文版
api手册地址:
Zend Framework中文版
api手册地址:
Zookeeper
api手册地址:
请教在线文档的api
Android中文版
api手册地址:
Ant最新版
api手册地址:
ASM字节码操作
api手册地址:
Axis2最新版
api手册地址:
Bash脚本
api手册地址:
Bootstrap 3
api手册地址:
Bootstrap 4
api手册地址:
C/C++
api手册地址:
C3P0连接池
api手册地址:
CentOS使用文档
api手册地址:
Commons-Beanutils
api手册地址:
Commons-Fileupload
api手册地址:
Commons-IO最新版
api手册地址:
Commons-Lang最新版
api手册地址:
Commons-Net最新版
api手册地址:
CSS 3
api手册地址:
DBCP连接池
api手册地址:
Dom4j
api手册地址:
dubbo中文文档
api手册地址:
EhCache
api手册地址:
Freemarker
api手册地址:
Go语言
api手册地址:
Hadoop
api手册地址:
Hibernate中文版
api手册地址:
IKAnalyzer中文版
api手册地址:
Java 10
api手册地址:
Java 6
api手册地址:
Java 7
api手册地址:
Java 8中文版
api手册地址:
jqGrid中文版
api手册地址:
Jquery中文版
api手册地址:
Json-lib
api手册地址:
Junit4最新版
api手册地址:
Kryo
api手册地址:
Log4j最新版
api手册地址:
Lucene
api手册地址:
Maven
api手册地址:
Windows MFC中文版
api手册地址:
Mybatis
api手册地址:
MySql中文版
api手册地址:
Netty 3.6
api手册地址:
Nginx中文版
api手册地址:
OpenJPA最新版
api手册地址:
PHP中文版
api手册地址:
POI-apache
api手册地址:
QuickServer
api手册地址:
redis中文参考文档
api手册地址:
Ruby
api手册地址:
Ruby-library
api手册地址:
Ruby on Rails
api手册地址:
Shiro
api手册地址:
Spring最新版
api手册地址:
Spring for Android
api手册地址:
Spring Boot
api手册地址:
Spring Cloud中文文档
api手册地址:
Spring Security
api手册地址:
Spring中文版
api手册地址:
Struts 2最新版
api手册地址:
Taperstry
api手册地址:
TensorFlow中文
api手册地址:
Tomcat
api手册地址:
Ubuntu
api手册地址:
Velocity 1.7
api手册地址:
VelocityTools2.0
api手册地址:
Vue Router中文参考
api手册地址:
vue.js中文文档
api手册地址:
XMLBeans
api手册地址:
Yahoo UI中文版
api手册地址:
Zend Framework中文版
api手册地址:
Zookeeper
api手册地址: