Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d46bca

Browse files
committed
Add nosql section
1 parent b930448 commit 0d46bca

File tree

2 files changed

+206
-5
lines changed

2 files changed

+206
-5
lines changed

SUMMARY.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
- [20.5.2、远程更新](pages/using-spring-boot.md#using-boot-devtools-remote-update)
8080
- [21、打包生产应用](pages/using-spring-boot.md#using-boot-packaging-for-production)
8181
- [21、下一步](pages/using-spring-boot.md#using-boot-whats-next)
82-
- [四、Spring Boot 功能](pages/spring-boot-features.md#boot-features)
82+
- [四、Spring Boot 特性](pages/spring-boot-features.md#boot-features)
8383
- [23、SpringApplication](pages/spring-boot-features.md#boot-features-spring-application)
8484
- [23.1、启动失败](pages/spring-boot-features.md#boot-features-startup-failure)
8585
- [23.2、自定义 banner](pages/spring-boot-features.md#boot-features-banner)
@@ -138,5 +138,17 @@
138138
- [30.3.1、实体类](pages/spring-boot-features.md#boot-features-jpa-and-spring-data)
139139
- [30.3.2、Spring Data JPA 资源库](pages/spring-boot-features.md#boot-features-spring-data-jpa-repositories)
140140
- [30.3.3、创建和删除 JPA 数据库](pages/spring-boot-features.md#boot-features-creating-and-dropping-jpa-databases)
141+
- [30.3.4、在视图中打开 EntityManager](pages/spring-boot-features.md#boot-features-jpa-in-web-environment)
142+
- [30.4、Spring Data JDBC](pages/spring-boot-features.md#boot-features-data-jdbc)
143+
- [30.5、使用 H2 的 Web 控制台](pages/spring-boot-features.md#boot-features-sql-h2-console)
144+
- [30.5.1、更改 H2 控制台的路径](pages/spring-boot-features.md#boot-features-sql-h2-console-custom-path)
145+
- [30.6、使用 jOOQ](pages/spring-boot-features.md#boot-features-jooq)
146+
- [30.6.1、代码生成](pages/spring-boot-features.md#_code_generation)
147+
- [30.6.2、使用 DSLContext](pages/spring-boot-features.md#_using_dslcontext)
148+
- [30.6.3、jOOQ SQL 方言](pages/spring-boot-features.md#_jooq_sql_dialect)
149+
- [30.6.4、自定义 jOOQ](pages/spring-boot-features.md#_customizing_jooq)
150+
- [31、使用 NoSQL 技术](pages/spring-boot-features.md#boot-features-nosql)
151+
- [31.1、Redis](pages/spring-boot-features.md#boot-features-redis)
152+
- [31.1.1、连接 Redis](pages/spring-boot-features.md#boot-features-connecting-to-redis)
141153

142154

pages/spring-boot-features.md

Lines changed: 193 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<a id="boot-features"></a>
22

3-
# 四、Spring Boot 功能
3+
# 四、Spring Boot 特性
44

55
本部分将介绍 Spring Boot 相关的细节内容。在这里,您可以学习到可能需要使用和自定义的主要功能。您如果还没有做好充分准备,可能需要阅读[第二部分:入门](#getting-started)[第三部分:使用 Spring Boot](#using-boot),以便打下前期基础。
66

@@ -1316,7 +1316,7 @@ spring.datasource.driver-class-name=com.mysql.jdbc.Driver
13161316
13171317
**注意**
13181318

1319-
对于要创建的池 `DataSource`,我们需要能够验证有效的 `Driver` 类是否可用,因此我们在使用之前进行检查。例如,如果您设置了 `spring.datasource.driver-class-name=com.mysql.jdbc.Driver`,那么该类必须可加载。
1319+
> 对于要创建的池 `DataSource`,我们需要能够验证有效的 `Driver` 类是否可用,因此我们在使用之前进行检查。例如,如果您设置了 `spring.datasource.driver-class-name=com.mysql.jdbc.Driver`,那么该类必须可加载。
13201320
13211321
有关更多支持选项,请参阅 [DataSourceProperties](https://github.com/spring-projects/spring-boot/tree/v2.1.1.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java)。这些都是标准选项,与实际的实现无关。还可以使用各自的前缀(`spring.datasource.hikari.*``spring.datasource.tomcat.*``spring.datasource.dbcp2.*`)微调实现特定的设置。请参考您现在使用的连接池实现的文档来获取更多信息。
13221322

@@ -1388,12 +1388,12 @@ spring.jdbc.template.max-rows=500
13881388
Java Persistence API(Java 持久化 API)是一项标准技术,可让您将对象**映射**到关系数据库。`spring-boot-starter-data-jpa` POM 提供了一个快速起步的方法。它提供了以下关键依赖:
13891389

13901390
- **Hibernate**  ——  最受欢迎的 JPA 实现之一。
1391-
- **Spring Data JPA** ——  可以轻松地实现基于 JPA 的资源库。
1391+
- **Spring Data JPA** ——  可以轻松地实现基于 JPA 的资源库。
13921392
- **Spring ORM**  ——  Spring Framework 的核心 ORM 支持
13931393

13941394
**提示**
13951395

1396-
我们不会在这里介绍太多关于 JPA 或者 [Spring Data](https://projects.spring.io/spring-data/) 的相关内容。您可以在 [spring.io](https://spring.io/) 上查看[使用 JPA 访问数据](https://spring.io/guides/gs/accessing-data-jpa/),获取阅读 [Spring Data JPA](https://projects.spring.io/spring-data-jpa/)[Hibernate](https://hibernate.org/orm/documentation/) 的参考文档。
1396+
> 我们不会在这里介绍太多关于 JPA 或者 [Spring Data](https://projects.spring.io/spring-data/) 的相关内容。您可以在 [spring.io](https://spring.io/) 上查看[使用 JPA 访问数据](https://spring.io/guides/gs/accessing-data-jpa/),获取阅读 [Spring Data JPA](https://projects.spring.io/spring-data-jpa/)[Hibernate](https://hibernate.org/orm/documentation/) 的参考文档。
13971397
13981398
<a id="boot-features-jpa-and-spring-data"></a>
13991399

@@ -1507,4 +1507,193 @@ spring.jpa.properties.hibernate.globally_quoted_identifiers=true
15071507

15081508
默认情况下,DDL 执行(或验证)将延迟到 `ApplicationContext` 启动后。还有一个 `spring.jpa.generate-ddl` 标志,如果 Hibernate 自动配置是激活的,那么它将不会被使用,因为 `ddl-auto` 设置更细粒度。
15091509

1510+
<a id="boot-features-jpa-in-web-environment"></a>
1511+
1512+
#### 30.3.4、在视图中打开 EntityManager
1513+
1514+
如果您正在运行 web 应用程序,Spring Boot 将默认注册 [`OpenEntityManagerInViewInterceptor`](https://docs.spring.io/spring/docs/5.1.3.RELEASE/javadoc-api/org/springframework/orm/jpa/support/OpenEntityManagerInViewInterceptor.html) 用于**在视图中打开 EntityManager** 模式,即运允许在 web 视图中延迟加载。如果您不想开启这个行为,则应在 `application.properties` 中将 `spring.jpa.open-in-view` 设置为 `false`
1515+
1516+
<a id="boot-features-data-jdbc"></a>
1517+
1518+
### 30.4、Spring Data JDBC
1519+
1520+
Spring Data 包含了对 JDBC 资源库的支持,并将自动为 `CrudRepository` 上的方法生成 SQL。对于更高级的查询,它提供了 `@Query` 注解。
1521+
1522+
当 classpath 下存在必要的依赖时,Spring Boot 将自动配置 Spring Data 的 JDBC 资源库。可以通过添加单个 `spring-boot-starter-data-jdbc` 依赖引入到项目中。如有必要,可通过在应用程序中添加 `@EnableJdbcRepositories` 注解或 `JdbcConfiguration` 子类来控制 Spring Data JDBC 的配置。
1523+
1524+
**提示**
1525+
1526+
> 有关 Spring Data JDBC 的完整详细信息,请参阅[参考文档](https://projects.spring.io/spring-data-jdbc/)
1527+
1528+
<a id="boot-features-sql-h2-console"></a>
1529+
1530+
### 30.5、使用 H2 的 Web 控制台
1531+
1532+
[H2 数据库](http://www.h2database.com/)提供了一个[基于浏览器的控制台](http://www.h2database.com/html/quickstart.html#h2_console),Spring Boot 可以为您自动配置。当满足以下条件时,控制台将自动配置:
1533+
1534+
- 您开发的是一个基于 servlet 的 web 应用程序
1535+
- `com.h2database:h2` 在 classpath 上
1536+
- 您使用了 [Spring Boot 的开发者工具](#using-boot-devtools)
1537+
1538+
**提示**
1539+
1540+
> 如果您不使用 Spring Boot 的开发者工具,但仍希望使用 H2 的控制台,则可以通过将 `spring.h2.console.enabled` 属性设置为 `true` 来实现。
1541+
1542+
**注意**
1543+
1544+
> H2 控制台仅用于开发期间,因此应注意确保 `spring.h2.console.enabled` 在生产环境中**没有**设置为 `true`
1545+
1546+
<a id="boot-features-sql-h2-console-custom-path"></a>
1547+
1548+
#### 30.5.1、更改 H2 控制台的路径
1549+
1550+
默认情况下,控制台的路径为 `/h2-console`。你可以使用 `spring.h2.console.path` 属性来自定义控制台的路径。
1551+
1552+
<a id="boot-features-jooq"></a>
1553+
1554+
### 30.6、使用 jOOQ
1555+
1556+
Java 面向对象查询([Java Object Oriented Querying,jOOQ](http://www.jooq.org/))是一款广受欢迎的产品,出自 [Data Geekery](http://www.datageekery.com/),它可以通过数据库生成 Java 代码,并允许您使用流畅 API 来构建类型安全的 SQL 查询。商业版和开源版都可以与 Spring Boot 一起使用。
1557+
1558+
<a id="_code_generation"></a>
1559+
1560+
#### 30.6.1、代码生成
1561+
1562+
要使用 jOOQ 的类型安全查询,您需要从数据库模式生成 Java 类。您可以按照 [jOOQ 用户手册](https://www.jooq.org/doc/3.11.7/manual-single-page/#jooq-in-7-steps-step3)中的说明进行操作。如果您使用了 `jooq-codegen-maven` 插件,并且还使用了 `spring-boot-starter-parent` 父 POM,则可以安全地省略掉插件的 `<version>` 标签。您还可以使用 Spring Boot 定义的版本变量(例如 `h2.version`)来声明插件的数据库依赖。以下是一个示例:
1563+
1564+
```xml
1565+
<plugin>
1566+
<groupId>org.jooq</groupId>
1567+
<artifactId>jooq-codegen-maven</artifactId>
1568+
<executions>
1569+
...
1570+
</executions>
1571+
<dependencies>
1572+
<dependency>
1573+
<groupId>com.h2database</groupId>
1574+
<artifactId>h2</artifactId>
1575+
<version>${h2.version}</version>
1576+
</dependency>
1577+
</dependencies>
1578+
<configuration>
1579+
<jdbc>
1580+
<driver>org.h2.Driver</driver>
1581+
<url>jdbc:h2:~/yourdatabase</url>
1582+
</jdbc>
1583+
<generator>
1584+
...
1585+
</generator>
1586+
</configuration>
1587+
</plugin>
1588+
```
1589+
1590+
<a id="_using_dslcontext"></a>
1591+
1592+
#### 30.6.2、使用 DSLContext
1593+
1594+
jOOQ 提供的流畅 API 是通过 `org.jooq.DSLContext` 接口初始化的。Spring Boot 将自动配置一个 `DSLContext` 作为 Spring Bean,并且将其连接到应用程序的 `DataSource``要使用 DSLContext`,您只需要 `@Autowire` 它:
1595+
1596+
```java
1597+
@Component
1598+
public class JooqExample implements CommandLineRunner {
1599+
1600+
private final DSLContext create;
1601+
1602+
@Autowired
1603+
public JooqExample(DSLContext dslContext) {
1604+
this.create = dslContext;
1605+
}
1606+
1607+
}
1608+
```
1609+
1610+
**提示**
1611+
1612+
> jOOQ 手册建议使用名为 `create` 的变量来保存 `DSLContext`
1613+
1614+
您可以使用 `DSLContext` 构建查询:
1615+
1616+
```java
1617+
public List<GregorianCalendar> authorsBornAfter1980() {
1618+
return this.create.selectFrom(AUTHOR)
1619+
.where(AUTHOR.DATE_OF_BIRTH.greaterThan(new GregorianCalendar(1980, 0, 1)))
1620+
.fetch(AUTHOR.DATE_OF_BIRTH);
1621+
}
1622+
```
1623+
1624+
<a id="_jooq_sql_dialect"></a>
1625+
1626+
#### 30.6.3、jOOQ SQL 方言
1627+
1628+
除非配置了 `spring.jooq.sql-dialect` 属性,否则 Spring Boot 会自动判定用于数据源的 SQL 方言。如果 Spring Boot 无法检测到方言,则使用 `DEFAULT`
1629+
1630+
**注意**
1631+
1632+
> Spring Boot 只能自动配置 jOOQ 开源版本支持的方言。
1633+
1634+
<a id="_customizing_jooq"></a>
1635+
1636+
#### 30.6.4、自定义 jOOQ
1637+
1638+
可通过定义自己的 `@Bean` 来实现更高级的功能,这些自定义将在创建 jOOQ `Configuration` 时使用。您可以为以下 jOOQ 类型定义 bean:
1639+
1640+
- `ConnectionProvider`
1641+
- `ExecutorProvider`
1642+
- `TransactionProvider`
1643+
- `RecordMapperProvider`
1644+
- `RecordUnmapperProvider`
1645+
- `RecordListenerProvider`
1646+
- `ExecuteListenerProvider`
1647+
- `VisitListenerProvider`
1648+
- `TransactionListenerProvider`
1649+
1650+
如果要完全控制 jOOQ 配置,您可以创建自己的 `org.jooq.Configuration` `@Bean`
1651+
1652+
<a id="boot-features-nosql"></a>
1653+
1654+
## 31、使用 NoSQL 技术
1655+
1656+
Spring Data 提供了其他项目,可以帮助您访问各种 NoSQL 技术,包括 [MongoDB](https://projects.spring.io/spring-data-mongodb/)[Neo4J](https://projects.spring.io/spring-data-neo4j/)[Elasticsearch](https://github.com/spring-projects/spring-data-elasticsearch/)[Solr](https://projects.spring.io/spring-data-solr/)[Redis](https://projects.spring.io/spring-data-redis/)[Gemfire](https://projects.spring.io/spring-data-gemfire/)[Cassandra](https://projects.spring.io/spring-data-cassandra/)[Couchbase](https://projects.spring.io/spring-data-couchbase/)[LDAP](https://projects.spring.io/spring-data-ldap/)。Spring Boot 为 Redis、MongoDB、Neo4j、Elasticsearch、Solr、Cassandra、Couchbase 和 LDAP 提供了自动配置。您也可以使用其他项目,但您需要自行配置他们。请参阅 [projects.spring.io/spring-data](https://projects.spring.io/spring-data) 中相应的参考文档。
1657+
1658+
<a id="boot-features-redis"></a>
1659+
1660+
## 31.1、Redis
1661+
1662+
[Redis](http://redis.io/) 是一个集缓存、消息代理和键值存储等丰富功能的数据库。Spring Boot 为 [Lettuce](https://github.com/lettuce-io/lettuce-core/)[Jedis 客户端类库](https://github.com/xetorthio/jedis/)提供了基本自动配置,[Spring Data Redis](https://github.com/spring-projects/spring-data-redis) 为他们提供了上层抽象。
1663+
1664+
使用 `spring-boot-starter-data-redis` starter 可方便地引入相关依赖。默认情况下,它使用 [Lettuce](https://github.com/lettuce-io/lettuce-core/)。该 starter 可处理传统应用程序和响应式应用程序。
1665+
1666+
**提示**
1667+
1668+
> 我们还提供了一个 `spring-boot-starter-data-redis-reactive` starter,以便与其他带有响应式支持的存储保持一致。
1669+
1670+
<a id="boot-features-connecting-to-redis"></a>
1671+
1672+
## 31.1.1、连接 Redis
1673+
1674+
您可以像所有 Spring Bean 一样注入自动配置的 `RedisConnectionFactory``StringRedisTemplate` 或普通的 `RedisTemplate` 实例。默认情况下,实例将尝试在 `localhost:6379` 上连接 Redis 服务器,以下是 bean 示例:
1675+
1676+
```java
1677+
@Component
1678+
public class MyBean {
1679+
1680+
private StringRedisTemplate template;
1681+
1682+
@Autowired
1683+
public MyBean(StringRedisTemplate template) {
1684+
this.template = template;
1685+
}
1686+
1687+
// ...
1688+
1689+
}
1690+
```
1691+
1692+
**提示**
1693+
1694+
> 您还可以注册任意数量个实现了 `LettuceClientConfigurationBuilderCustomizer` 的 bean,以进行更高级的自定义。如果你使用 Jedis,则可以使用 `JedisClientConfigurationBuilderCustomizer`
1695+
1696+
如果您添加了自己的任何一个自动配置类型的 `@Bean`,它将替换默认设置(除了 `RedisTemplate`,由于排除是基于 bean 名称,而 `redisTemplate` 不是它的类型)。默认情况下,如果 `commons-pool2` 在 classpath 上,您将获得一个连接池工厂。
1697+
1698+
15101699
**待续……**

0 commit comments

Comments
 (0)