目录html
PostGreSQL是一个功能强大的开源对象关系数据库管理系统(ORDBMS),号称世界上最早进的开源关系型数据库
通过长达15年以上的积极开发和不断改进,PostGreSQL已在可靠性、稳定性、数据一致性等得到了很大的提高。
对比时下最流行的 MySQL 来讲,PostGreSQL 拥有更灵活,更高度兼容标准的一些特性。
此外,PostGreSQL基于MIT开源协议,其开放性极高,这也是其成为各个云计算大T 主要的RDS数据库的根本缘由。git
从DBEngine的排名上看,PostGreSQL排名第四,且保持着高速的增加趋势,很是值得关注。
这篇文章,以整合SpringBoot 为例,讲解如何在常规的 Web项目中使用 PostGreSQL。spring
JPA 是指 Java Persistence API,即 Java 的持久化规范,一开始是做为 JSR-220 的一部分。
JPA 的提出,主要是为了简化 Java EE 和 Java SE 应用开发工做,统一当时的一些不一样的 ORM 技术。
通常来讲,规范只是定义了一套运做的规则,也就是接口,而像咱们所熟知的Hibernate 则是 JPA 的一个实现(Provider)。sql
JPA 定义了什么,大体有:数据库
要体验 JPA 的魅力,能够从Spring Data JPA 开始。apache
SpringDataJPA 是 SpringFramework 对 JPA 的一套封装,主要呢,仍是为了简化数据持久层的开发。
好比:api
基本上,SpringDataJPA 几乎已经成为 Java Web 持久层的必选组件。更多一些细节能够参考官方文档:缓存
https://docs.spring.io/spring-data/jpa/docs/1.11.0.RELEASE/reference/htmltomcat
接下来的篇幅,将演示 JPA 与 PostGreSQL 的整合实例。springboot
这里假定你已经安装好数据库,并已经建立好一个 SpringBoot 项目,接下来需添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>${spring-boot.version}</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
经过spring-boot-stater-data-jpa,能够间接引入 spring-data-jpa的配套版本;
为了使用 PostGreSQL,则须要引入 org.postgresql.postgresql 驱动包。
编辑 application.properties,以下:
## 数据源配置 (DataSourceAutoConfiguration & DataSourceProperties) spring.datasource.url=jdbc:postgresql://localhost:5432/appdb spring.datasource.username=appuser spring.datasource.password=appuser # Hibernate 原语 spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect # DDL 级别 (create, create-drop, validate, update) spring.jpa.hibernate.ddl-auto = update
其中,spring.jpa.hibernate.ddl-auto 指定为 update,这样框架会自动帮咱们建立或更新表结构。
咱们以书籍信息来做为实例,一本书会有标题、类型、做者等属性,对应于表的各个字段。
这里为了演示多对一的关联,咱们还会定义一个Author(做者信息)实体,书籍和实体经过一个外键(author_id)关联
Book 类
@Entity @Table(name = "book") public class Book extends AuditModel{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @NotBlank @Size(min = 1, max = 50) private String type; @NotBlank @Size(min = 3, max = 100) private String title; @Column(columnDefinition = "text") private String description; @Column(name = "fav_count") private int favCount; @ManyToOne(fetch = FetchType.LAZY, optional = false) @JoinColumn(name = "author_id", nullable = false) private Author author; //省略 get/set
这里,咱们用了一系列的注解,好比@Table、@Column分别对应了数据库的表、列。
@GeneratedValue 用于指定ID主键的生成方式,GenerationType.IDENTITY 指采用数据库原生的自增方式,
对应到 PostGreSQL则会自动采用 BigSerial 作自增类型(匹配Long 类型)
@ManyToOne 描述了一个多对一的关系,这里声明了其关联的"做者“实体,LAZY 方式指的是当执行属性访问时才真正去数据库查询数据;
@JoinColumn 在这里配合使用,用于指定其关联的一个外键。
Book 实体的属性:
属性 | 描述 |
---|---|
id | 书籍编号 |
type | 书籍分类 |
title | 书籍标题 |
description | 书籍描述 |
favCount | 收藏数 |
author | 做者 |
Author信息
@Entity @Table(name = "author") public class Author extends AuditModel{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @NotBlank @Size(min = 1, max = 100) private String name; @Size(max = 400) private String hometown;
审计模型
注意到两个实体都继承了AuditModel这个类,这个基础类实现了"审计"的功能。
审计,是指对数据的建立、变动等生命周期进行审阅的一种机制,
一般审计的属性包括 建立时间、修改时间、建立人、修改人等信息
AuditModel的定义以下所示:
@MappedSuperclass @EntityListeners(AuditingEntityListener.class) public abstract class AuditModel implements Serializable { @Temporal(TemporalType.TIMESTAMP) @Column(name = "created_at", nullable = false, updatable = false) @CreatedDate private Date createdAt; @Temporal(TemporalType.TIMESTAMP) @Column(name = "updated_at", nullable = false) @LastModifiedDate private Date updatedAt;
上面的审计实体包含了 createAt、updateAt 两个日期类型字段,@CreatedDate、@LastModifiedDate分别对应了各自的语义,仍是比较容易理解的。
@Temporal 则用于声明日期类型对应的格式,如TIMESTAMP会对应 yyyy-MM-dd HH:mm:ss的格式,而这个也会被体现到DDL中。
@MappedSuperClass 是必须的,目的是为了让子类定义的表能拥有继承的字段(列)
审计功能的“魔力”在于,添加了这些继承字段以后,对象在建立、更新时会自动刷新这几个字段,这些是由框架完成的,应用并不须要关心。
为了让审计功能生效,须要为AuditModel 添加 @EntityListeners(AuditingEntityListener.class)声明,同时还应该为SpringBoot 应用声明启用审计:
@EnableJpaAuditing @SpringBootApplication public class BootJpa { ...
持久层基本是继承于 JpaRepository或CrudRepository的接口。
以下面的代码:
***AuthorRepository
@Repository public interface AuthorRepository extends JpaRepository<Author, Long> { }
*** BookRepository ***
@Repository public interface BookRepository extends JpaRepository<Book, Long>{ List<Book> findByType(String type, Pageable request); @Transactional @Modifying @Query("update Book b set b.favCount = b.favCount + ?2 where b.id = ?1") int incrFavCount(Long id, int fav); }
findByType 实现的是按照 类型(type) 进行查询,这个方法将会被自动转换为一个JPQL查询语句。
并且,SpringDataJPA 已经能够支持大部分经常使用场景,能够参考这里
incrFavCount 实现了收藏数的变动,除了使用 @Query 声明了一个update 语句以外,@Modify用于标记这是一个“产生变动的查询”,用于通知EntityManager及时清除缓存。
@Transactional 在这里是必须的,不然会提示 TransactionRequiredException这样莫名其妙的错误。
Service 的实现相对简单,仅仅是调用持久层实现数据操做。
@Service public class BookService { @Autowired private BookRepository bookRepository; @Autowired private AuthorRepository authorRepository; /** * 建立做者信息 * * @param name * @param hometown * @return */ public Author createAuthor(String name, String hometown) { if (StringUtils.isEmpty(name)) { return null; } Author author = new Author(); author.setName(name); author.setHometown(hometown); return authorRepository.save(author); } /** * 建立书籍信息 * * @param author * @param type * @param title * @param description * @return */ public Book createBook(Author author, String type, String title, String description) { if (StringUtils.isEmpty(type) || StringUtils.isEmpty(title) || author == null) { return null; } Book book = new Book(); book.setType(type); book.setTitle(title); book.setDescription(description); book.setAuthor(author); return bookRepository.save(book); } /** * 更新书籍信息 * * @param bookId * @param type * @param title * @param description * @return */ @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, readOnly = false) public boolean updateBook(Long bookId, String type, String title, String description) { if (bookId == null || StringUtils.isEmpty(title)) { return false; } Book book = bookRepository.findOne(bookId); if (book == null) { return false; } book.setType(type); book.setTitle(title); book.setDescription(description); return bookRepository.save(book) != null; } /** * 删除书籍信息 * * @param bookId * @return */ public boolean deleteBook(Long bookId) { if (bookId == null) { return false; } Book book = bookRepository.findOne(bookId); if (book == null) { return false; } bookRepository.delete(book); return true; } /** * 根据编号查询 * * @param bookId * @return */ public Book getBook(Long bookId) { if (bookId == null) { return null; } return bookRepository.findOne(bookId); } /** * 增长收藏数 * * @return */ public boolean incrFav(Long bookId, int fav) { if (bookId == null || fav <= 0) { return false; } return bookRepository.incrFavCount(bookId, fav) > 0; } /** * 获取分类下书籍,按收藏数排序 * * @param type * @return */ public List<Book> listTopFav(String type, int max) { if (StringUtils.isEmpty(type) || max <= 0) { return Collections.emptyList(); } // 按投票数倒序排序 Sort sort = new Sort(Sort.Direction.DESC, "favCount"); PageRequest request = new PageRequest(0, max, sort); return bookRepository.findByType(type, request); } }
前面的部分已经完成了基础的CRUD操做,但在正式的项目中每每会须要一些定制作法,下面作几点介绍。
使用 findByxxx 这样的方法映射已经能够知足大多数的场景,但若是是一些"不肯定"的查询条件呢?
咱们知道,JPA 定义了一套的API来帮助咱们实现灵活的查询,经过EntityManager 能够实现各类灵活的组合查询。
那么在 Spring Data JPA 框架中该如何实现呢?
首先建立一个自定义查询的接口:
public interface BookRepositoryCustom { public PageResult<Book> search(String type, String title, boolean hasFav, Pageable pageable); }
接下来让 BookRepository 继承于该接口:
@Repository public interface BookRepository extends JpaRepository<Book, Long>, BookRepositoryCustom { ...
最终是 实现这个自定义接口,经过 AOP 的"魔法",框架会将咱们的实现自动嫁接到接口实例上。
具体的实现以下:
public class BookRepositoryImpl implements BookRepositoryCustom { private final EntityManager em; @Autowired public BookRepositoryImpl(JpaContext context) { this.em = context.getEntityManagerByManagedType(Book.class); } @Override public PageResult<Book> search(String type, String title, boolean hasFav, Pageable pageable) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<Book> root = cq.from(Book.class); List<Predicate> conds = new ArrayList<>(); //按类型检索 if (!StringUtils.isEmpty(type)) { conds.add(cb.equal(root.get("type").as(String.class ), type)); } //标题模糊搜索 if (!StringUtils.isEmpty(title)) { conds.add(cb.like(root.get("title").as(String.class ), "%" + title + "%")); } //必须被收藏过 if (hasFav) { conds.add(cb.gt(root.get("favCount").as(Integer.class ), 0)); } //count 数量 cq.select(cb.count(root)).where(conds.toArray(new Predicate[0])); Long count = (Long) em.createQuery(cq).getSingleResult(); if (count <= 0) { return PageResult.empty(); } //list 列表 cq.select(root).where(conds.toArray(new Predicate[0])); //获取排序 List<Order> orders = toOrders(pageable, cb, root); if (!CollectionUtils.isEmpty(orders)) { cq.orderBy(orders); } TypedQuery<Book> typedQuery = em.createQuery(cq); //设置分页 typedQuery.setFirstResult(pageable.getOffset()); typedQuery.setMaxResults(pageable.getPageSize()); List<Book> list = typedQuery.getResultList(); return PageResult.of(count, list); } private List<Order> toOrders(Pageable pageable, CriteriaBuilder cb, Root<?> root) { List<Order> orders = new ArrayList<>(); if (pageable.getSort() != null) { for (Sort.Order o : pageable.getSort()) { if (o.isAscending()) { orders.add(cb.asc(root.get(o.getProperty()))); } else { orders.add(cb.desc(root.get(o.getProperty()))); } } } return orders; } }
聚合功能能够用 SQL 实现,但经过JPA 的 Criteria API 会更加简单。
与实现自定义查询的方法同样,也是经过EntityManager来完成操做:
public List<Tuple> groupCount(){ CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<Book> root = cq.from(Book.class); Path<String> typePath = root.get("type"); //查询type/count(*)/sum(favCount) cq.select(cb.tuple(typePath,cb.count(root).alias("count"), cb.sum(root.get("favCount")))); //按type分组 cq.groupBy(typePath); //按数量排序 cq.orderBy(cb.desc(cb.literal("count"))); //查询出元祖 TypedQuery<Tuple> typedQuery = em.createQuery(cq); return typedQuery.getResultList(); }
上面的代码中,会按书籍的分组统计数量,且按数量降序返回。
等价于下面的SQL:
···
select type, count(*) as count , sum(fav_count) from book
group by type order by count;
···
视图的操做与表基本是相同的,只是视图通常是只读的(没有更新操做)。
执行下面的语句能够建立一个视图:
create view v_author_book as select b.id, b.title, a.name as author_name, a.hometown as author_hometown, b.created_at from author a, book b where a.id = b.author_id;
在代码中使用@Table来进行映射:
@Entity @Table(name = "v_author_book") public class AuthorBookView { @Id private Long id; private String title; @Column(name = "author_name") private String authorName; @Column(name = "author_hometown") private String authorHometown; @Column(name = "created_at") private Date createdAt;
建立一个相应的Repository:
@Repository public interface AuthorBookViewRepository extends JpaRepository<AuthorBookView, Long> { }
这样就能够进行读写了。
在生产环境中通常须要配置合适的链接池大小,以及超时参数等等。
这些须要经过对数据源(DataSource)进行配置来实现,DataSource也是一个抽象定义,默认状况下SpringBoot 1.x会使用Tomcat的链接池。
以Tomcat的链接池为例,配置以下:
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource # 初始链接数 spring.datasource.tomcat.initial-size=15 # 获取链接最大等待时长(ms) spring.datasource.tomcat.max-wait=20000 # 最大链接数 spring.datasource.tomcat.max-active=50 # 最大空闲链接 spring.datasource.tomcat.max-idle=20 # 最小空闲链接 spring.datasource.tomcat.min-idle=15 # 是否自动提交事务 spring.datasource.tomcat.default-auto-commit=true
从这里能够找到一些详尽的参数
SpringBoot 默认状况下会为咱们开启事务的支持,引入 spring-starter-data-jpa 的组件将会默认使用 JpaTransactionManager 用于事务管理。
在业务代码中使用@Transactional 能够声明一个事务,以下:
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false, rollbackFor = Exception.class) public boolean updateBook(Long bookId, String type, String title, String description) { ...
为了演示事务的使用,上面的代码指定了几个关键属性,包括:
选项 | 描述 |
---|---|
REQUIRED | 使用已存在的事务,若是没有则建立一个。 |
MANDATORY | 若是存在事务则加入,若是没有事务则报错。 |
REQUIRES_NEW | 建立一个事务,若是已存在事务会将其挂起。 |
NOT_SUPPORTED | 以非事务方式运行,若是当前存在事务,则将其挂起。 |
NEVER | 以非事务方式运行,若是当前存在事务,则抛出异常。 |
NESTED | 建立一个事务,若是已存在事务,新事务将嵌套执行。 |
级别 | 描述 |
---|---|
DEFAULT | 默认值,使用底层数据库的默认隔离级别。大部分等于READ_COMMITTED |
READ_UNCOMMITTED | 未提交读,一个事务能够读取另外一个事务修改但尚未提交的数据。不能防止脏读和不可重复读。 |
READ_COMMITTED | 已提交读,一个事务只能读取另外一个事务已经提交的数据。能够防止脏读,大多数状况下的推荐值。 |
REPEATABLE_READ | 可重复读,一个事务在整个过程当中能够屡次重复执行某个查询,而且每次返回的记录都相同。能够防止脏读和不可重复读。 |
SERIALIZABLE | 串行读,全部的事务依次逐个执行,这样事务之间就彻底不可能产生干扰,能够防止脏读、不可重复读以及幻读。性能低。 |
readOnly
指示当前事务是否为只读事务,默认为false
rollbackFor
指示当捕获什么类型的异常时会进行回滚,默认状况下产生 RuntimeException 和 Error 都会进行回滚(受检异常除外)
参考文档
https://www.baeldung.com/spring-boot-tomcat-connection-pool
https://www.baeldung.com/transaction-configuration-with-jpa-and-spring
https://www.callicoder.com/spring-boot-jpa-hibernate-postgresql-restful-crud-api-example/
https://docs.spring.io/spring-data/jpa/docs/1.11.0.RELEASE/reference/html/#projections
https://www.cnblogs.com/yueshutong/p/9409295.html
本篇文章描述了一个完整的 SpringBoot + JPA + PostGreSQL 开发案例,一些作法可供你们借鉴使用。
因为 JPA 帮咱们简化许多了数据库的开发工做,使得咱们在使用数据库时并不须要了解过多的数据库的特性。
所以,本文也适用于整合其余的关系型数据库。
前面也已经提到过,PostGreSQL因为其开源许可的开放性受到了云计算大T的青睐,相信将来前景可期。在接下来将会更多的关注该数据库的发展。
欢迎继续关注"美码师的补习系列-springboot篇" ,期待更多精彩内容^-^