第三方bean属性绑定
@ConfigurationProperties绑定属性名宽松绑定
@Value注解不支持松散绑定
绑定前缀命名命名规则(仅能使用纯小写字母、数字、下划线作为合法的字符)
常用计量单位 JDK8支持的时间与空间计量单位(ChronoUnit,DataUnit)
bean属性校验 1.开启数据校验有助于系统安全性,J2EE规范中JSR303规范定义了一组有关数据校验相关的API(javax.validation),需要添加JSR303规范坐标
1 2 3 4 5 6 7 <dependency > <groupId > javax.validation</groupId > <artifactId > validation-api</artifactId > <version > 2.0.1.Final</version > </dependency >
2.开启对当前bean的属性注入校验 (@Validated ——对使用后了此注解的类里的字段进行校验)
1 2 @Validated public class ServerConfig {}
3.使用校验API接口需要添加一个Hibernate框架的校验器做实现类,否则会报ERROR,需要添加Hibernate校验框架坐标
1 2 3 4 <dependency > <groupId > org.hibernate.validator</groupId > <artifactId > hibernate-validator</artifactId > </dependency >
1 2 3 4 5 6 7 8 9 10 11 12 Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022 -04 -14 18 :42 :54.022 ERROR 17960 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: The Bean Validation API is on the classpath but no implementation could be found Action: Add an implementation, such as Hibernate Validator, to the classpath
4.设置校验规则,具体对字段进行校验的校验规则在如下包内
1 2 3 4 @Max(value = 8888,message = "最大值不能超过8888") private int port; package javax.validation.constraints;
进制数据转换规则 yaml语法规则
——字面值表达方式
内置数据源 SpringBoot提供了3种内嵌的数据源对象供开发者选择
(1)HikariCP —-默认内置数据源对象 (需要注意HikariCP使用的是通用的url,数据源详细设置并不能使用通用设置)
(2)Tomcat提供DataSource —-HiKariCP不可用的情况下,且在Web环境中,将使用tomcat服务器配置数据源对象
(3)Commons DBCP —-Hik ariCP不可用,tomcat数据源也不可用,将使用dbcp数据源
内置数据库 Springboot提供了3种内嵌数据库供开发者选择,提供开发测试效率
(1)H2
(2)HSQL
(3)Derby
h2使用方法(第一次初始化需要配置数据源)
1 2 3 4 5 6 7 8 9 10 11 12 spring: h2: console: enabled: true path: /h2 # datasource: # url: jdbc:h2:~/test # hikari: # driver-class-name: org.h2.Driver # username: sa # password: 1234
Redis redis是一筐kyey-value存储解结构的内存级NoSQL数据库
——支持多种数据存储格式
——支持持久化
——支持集群
Redis下载(windows)
https://github.com/tporadowski/redis/releases
Redis安装与启动(windows)
——Windows解压安装或一键式安装
——服务端启动命令
1 redis-server.exe redis.windows.conf
客户端启动命令
redis默认端口6379
第一次启动有bug:
——需要在redis-cli.exe执行shutdown
springboot整合redis
——导入依赖
1 2 3 4 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
——springboot配置文件配置redis
1 2 3 4 spring: redis: host: localhost #默认主机 port: 6379 #默认端口
使用RedisTemplat
——测试redis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 @Test void set () { ValueOperations ops = redisTemplate.opsForValue(); ops.set("age" ,20 ); } @Test void get () { ValueOperations ops = redisTemplate.opsForValue(); Object age = ops.get("age" ); System.out.println(age); } @Test void hset () { HashOperations hops = redisTemplate.opsForHash(); hops.put("users" ,"zhangsan" ,"zhangsan" ); } @Test void hget () { HashOperations hops = redisTemplate.opsForHash(); Object users = hops.get("users" , "zhangsan" ); System.out.println(users); }
使用RedisTemplate遇到乱码使用JdkSerializationRedisSerializer来进行序列化
1 2 3 4 5 6 7 8 9 @Autowired(required = false) public void setRedisTemplate (RedisTemplate redisTemplate) { RedisSerializer stringSerializer = new StringRedisSerializer (); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(stringSerializer); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(stringSerializer); this .redisTemplate = redisTemplate; }
Elasticsearch(ES) Elasticsearch是一个分布式全文搜索引擎
——导入依赖
1 2 3 4 <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency>
——使用ES客户端
1 2 3 4 5 6 7 8 9 @Test void testCreateIndex () throws IOException { HttpHost host = HttpHost.create("http://localhost:9200" ); RestClientBuilder builer = RestClient.builder(host); client = new RestHighLevelClient (builer); CreateIndexRequest request = new CreateIndexRequest ("books" ); client.indices().create(request, RequestOptions.DEFAULT); client.close(); }
—— ES实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 package com.example;import com.alibaba.fastjson.JSON;import com.example.dao.BookDao;import com.example.pojo.Book;import org.apache.http.HttpHost;import org.elasticsearch.action.bulk.BulkRequest;import org.elasticsearch.action.get.GetRequest;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.index.IndexRequest;import org.elasticsearch.action.search.SearchRequest;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.client.indices.CreateIndexRequest;import org.elasticsearch.common.xcontent.XContentType;import org.elasticsearch.index.query.QueryBuilder;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.search.SearchHit;import org.elasticsearch.search.SearchHits;import org.elasticsearch.search.builder.SearchSourceBuilder;import org.junit.jupiter.api.AfterEach;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import javax.naming.directory.SearchResult;import java.io.IOException;import java.util.List;@SpringBootTest class SpringbootEsApplicationTests { @Autowired private BookDao bookDao; private RestHighLevelClient client; @BeforeEach void setUp () { HttpHost host = HttpHost.create("http://localhost:9200" ); RestClientBuilder builer = RestClient.builder(host); client = new RestHighLevelClient (builer); } @AfterEach void tearDown () throws IOException { client.close(); } @Test void testCreateIndex () throws IOException { CreateIndexRequest request = new CreateIndexRequest ("books" ); String json = "{\n" + " \"mappings\":{\n" + " \"properties\":{\n" + " \"id\":{\n" + " \"type\":\"keyword\"\n" + " },\n" + " \"name\":{\n" + " \"type\":\"text\",\n" + " \"analyzer\":\"ik_max_word\",\n" + " \"copy_to\":\"all\"\n" + " },\n" + " \"type\":{\n" + " \"type\":\"keyword\"\n" + " },\n" + " \"description\":{\n" + " \"type\":\"text\",\n" + " \"analyzer\":\"ik_max_word\",\n" + " \"copy_to\":\"all\"\n" + " },\n" + " \"all\":{\n" + " \"type\":\"text\",\n" + " \"analyzer\":\"ik_max_word\"\n" + " }\n" + " }\n" + " }\n" + "}" ; request.source(json, XContentType.JSON); client.indices().create(request, RequestOptions.DEFAULT); } @Test void testCreateDoc () throws IOException { Book book = bookDao.selectById(1 ); IndexRequest request = new IndexRequest ("books" ).id(book.getId().toString()); String json = JSON.toJSONString(book); request.source(json,XContentType.JSON); client.index(request,RequestOptions.DEFAULT); } @Test void testCreateDocAll () throws IOException { List<Book> list = bookDao.selectList(null ); BulkRequest bulk = new BulkRequest (); for (Book book : list) { IndexRequest request = new IndexRequest ("books" ).id(book.getId().toString()); String json = JSON.toJSONString(book); request.source(json,XContentType.JSON); bulk.add(request); } client.bulk(bulk,RequestOptions.DEFAULT); } @Test void testGetId () throws IOException { GetRequest request = new GetRequest ("books" ,"1" ); GetResponse response = client.get(request, RequestOptions.DEFAULT); String json = response.getSourceAsString(); System.out.println(json); } @Test void testSearch () throws IOException { SearchRequest request = new SearchRequest ("books" ); SearchSourceBuilder builer = new SearchSourceBuilder (); builer.query(QueryBuilders.termQuery("name" ,"aa" )); request.source(builer); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits hits = response.getHits(); for (SearchHit hit : hits) { String json = hit.getSourceAsString(); Book book = JSON.parseObject(json, Book.class); System.out.println(book.toString()); } }