1.Redis
1.Redis是一款key-value存储结构的内存结构的内存级NoSQL数据库
支持多种数据存储
支持持久化
支持集群
2.服务器的启动命令:
redis-server.exe redis.windows.conf
3.客户端启动命令
redis-cli.exe
4.SpringBoot整合Redis
1.导入相应的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.做相应的配置
spring: redis: host: localhost port: 6379 client-type: jedis
主机:localhost
默认端口:6379
5.RedisTemplate提供各种数据库存储类型的接口
@SpringBootTest class Springboot16RedisApplicationTests { @Resource private RedisTemplate redisTemplate; @Test void set() { //选择操作的数据类型 调用set方法 ValueOperations ops = redisTemplate.opsForValue(); ops.set(1,"hello"); } @Test void get() { //选择操作的数据类型 调用set方法 ValueOperations ops = redisTemplate.opsForValue(); Object o = ops.get(1); System.out.println(o); } @Test //通过添加两个键和一个值 void hset() { HashOperations hashOperations = redisTemplate.opsForHash(); hashOperations.put("key1","a","aaa"); } @Test void hget() { //选择操作的数据类型 调用set方法 HashOperations hashOperations = redisTemplate.opsForHash(); Object o = hashOperations.get("key1", "a"); System.out.println(o); } }
6.jedis和redis的区别:
1.jedis连接Redis服务器是直连模式当多线程模式下使用jedis会存在线程安全问题,解决方案可以通过配置连接池使每个连接专用,这样整体性能就大受影响。
2.lettcus基于Netty框架进行与Redis服务器连接,底层设计中采用StatefulRedisConnection。 StatefulRedisConnection自身是线程安全的,可以保障并发访问安全问题,所以一个连接可以被多线程复用。当然lettcus也支持多连接实例一起工作。
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
spring: redis: host: localhost port: 6379 client-type: jedis
2.Mongodb
MongoDB是一个开源、高性能、无模式的文档型数据库,实际上是指动态类型的模式 ,而不是RDBMS(SQL)数据库中可用的静态类型的模式。
1.导入相应的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
2.配置客户端
spring: data: mongodb: uri: mongodb://localhost/lin
3.客户端读写
@SpringBootTest class Springboot17MonogodbApplicationTests { @Resource private MongoTemplate mongoTemplate; @Test void contextLoads() { Book book = new Book(); book.setId(2); book.setType("111"); book.setDescription("111"); book.setName("11111"); mongoTemplate.save(book); } @Test void findAll(){ List<Book> all = mongoTemplate.findAll(Book.class); System.out.println(all); } }
4.MongoDB的CRUD
1.基础查询
查询全部:db.集合.find(); 查第一条:db.集合.findOne() 查询指定数量文档:db.集合.find().limit(10) //查10条文档 跳过指定数量文档:db.集合.find().skip(20) //跳过20条文档 统计:db.集合.count() 排序:db.集合.sort({age:1}) //按age升序排序 投影:db.集合名称.find(条件,{name:1,age:1}) //仅保留name与age域
2.条件查询
基本格式:db.集合.find({条件}) 模糊查询:db.集合.find({域名:/正则表达式/}) //等同SQL中的like,比like强大,可以执行正则所有规则 条件比较运算:db.集合.find({域名:{$gt:值}}) //等同SQL中的数值比较操作,例如:name>18 包含查询:db.集合.find({域名:{$in:[值1,值2]}}) //等同于SQL中的in 条件连接查询:db.集合.find({$and:[{条件1},{条件2}]}) //等同于SQL中的and、or
3.ElasticSearch(ES)
ElasticSearch是一个分布式搜索全文搜素引擎
1.elasticsearch的运行elasticsearch.bat
2.创建、查询、删除索引
3.创建索引并指定规则
{ "mappings":{ "properties":{ "id":{ "type":"keyword" }, "name":{ "type":"text", "analyzer":"ik_max_word", "copy_to":"all" }, "description":{ "type":"text", "analyzer":"ik_max_word", "copy_to":"all" }, "all":{ "type":"text", "analyzer":"ik_max_word" }, "type":{ "type":"text" } } } }
4.创建文档
5.查询文档
6.删除文档
7.条件查询
8.修改文档(全量修改)
PUT http://localhost:9200/books/_doc/1
{ "name":"springboot", "type":"springboot", "description":"springboot" }
9.修改文档(修改部分)
POST http://localhost:9200/books/_update/1
{ "doc":{ "name":"springboot" } }
10.springboot整合es
1.导入相应的依赖
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency>
2.配置
spring: datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true username: root password: "123456" # elasticsearch: # uris: http://localhost:9200 mybatis-plus: global-config: db-config: table-prefix: tbl_ id-type: auto configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.相关操作
@SpringBootTest class Springboot18EsApplicationTests { @Resource private BookDao bookDao; private RestHighLevelClient client; //测试启动时执行的方法 @BeforeEach void setUp() { //创建客户端 HttpHost host=HttpHost.create("http://localhost:9200"); RestClientBuilder builder= RestClient.builder(host); client = new RestHighLevelClient(builder); } //执行完测试方法之后执行的方法 @AfterEach void tearDown() throws IOException { client.close(); } @Test void createTestIndex() throws IOException { CreateIndexRequest request=new CreateIndexRequest("books"); client.indices().create(request, RequestOptions.DEFAULT); } // @Test void testCreateIndexByIK() 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" + " },\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" + " \"type\":{\n" + " \"type\":\"text\"\n" + " }\n" + "\n" + " }\n" + " }\n" + "}"; //设置请求中的参数 request.source(json, XContentType.JSON); client.indices().create(request, RequestOptions.DEFAULT); } //添加文档 @Test void creatDoc() throws IOException { Book book = bookDao.selectById(2); 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 creatDocAll() throws IOException { List<Book> bookList = bookDao.selectList(null); //创建一个批处理的容器 BulkRequest bulkRequest = new BulkRequest(); for (Book book : bookList) { IndexRequest request=new IndexRequest("books").id(book.getId().toString()); String json= JSON.toJSONString(book); request.source(json,XContentType.JSON); bulkRequest.add(request); } /* IndexRequest request=new IndexRequest("books").id(book.getId().toString()); String json= JSON.toJSONString(book); request.source(json,XContentType.JSON);*/ client.bulk(bulkRequest,RequestOptions.DEFAULT); } //按id查询文档 @Test void findById() throws IOException { GetRequest request=new GetRequest("books","2"); GetResponse res = client.get(request, RequestOptions.DEFAULT); String sourceAsString = res.getSourceAsString(); System.out.println(sourceAsString); } //按条件查询 @Test void findByCondition() throws IOException { SearchRequest request=new SearchRequest("books"); //添加查询条件 SearchSourceBuilder builder=new SearchSourceBuilder(); //all中包含了name 和 description SearchSourceBuilder query = builder.query(QueryBuilders.termQuery("all", "spring")); request.source(builder); SearchResponse search = client.search(request, RequestOptions.DEFAULT); //拿到所有的命中结果 SearchHits hits = search.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); System.out.println(sourceAsString); } } }