Druid + MyBatis-Plus + MySQL
♦数据源:DruidDataSource
♦持久化技术:MyBatis-Plus / MyBatis
♦数据库: MySQL
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root
hikari:
maximum-pool-size: 50
使用JdbcTemplate需要导入
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
代码
@Test
void test() {
String sql = "select * from tbl_book where id = 1";
List<Book> qurey = jdbcTemplate.query(sql, new RowMapper<Book>() {
@Override
public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
Book temp = new Book();
temp.setId(rs.getInt("id"));
temp.setName(rs.getString("name"));
temp.setType(rs.getString("type"));
temp.setDescription(rs.getString("description"));
return temp;
}
});
System.out.println(qurey);
}
配置
spring:
jdbc:
template:
query-timeout: -1 #查询超时时间
max-rows: 500 #最大行数
fetch-size: -1 #批处理数量
server:
port: 80
spring:
h2:
console:
enabled: true
path: /h2
♦ 访问用户名sa,默认密码123456
create table tbl_book (id int,name varchar,type varchar,description varchar)
server:
port: 80
spring:
h2:
console:
enabled: true
path: /h2
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:~/test
username: sa
password: 123456
server:
port: 80
spring:
h2:
console:
enabled: flase
path: /h2
server:
port: 80
spring:
h2:
console:
enabled: true
path: /h2
datasource:
# driver-class-name: org.h2.Driver
url: jdbc:h2:~/test
username: sa
password: 123456
内存级
NoSQL数据库持久化
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
spring:
redis:
host: localhost
port: 6379
♦ 主机:localhost(默认)
♦ 端口:6379(默认)
@SpringBootTest
class Springboot16RedisApplicationTests {
@Test
void set(@Autowired RedisTemplate redisTemplate) {
ValueOperations ops = redisTemplate.opsForValue();
ops.set("age", 41);
}
@Test
void get(@Autowired RedisTemplate redisTemplate) {
ValueOperations ops = redisTemplate.opsForValue();
Object age = ops.get("age");
System.out.println(age);
}
@Test
void hset(@Autowired RedisTemplate redisTemplate) {
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("info", "b", "bb");
}
@Test
void hget(@Autowired RedisTemplate redisTemplate) {
HashOperations hashOperations = redisTemplate.opsForHash();
Object val = hashOperations.get("info", "b");
System.out.println(val);
}
}
@SpringBootTest
public class StringRedisTemplateTest {
@Test
void set(@Autowired StringRedisTemplate stringRedisTemplate) {
ValueOperations ops = stringRedisTemplate.opsForValue();
ops.set("testKey","testKey");
}
@Test
void get(@Autowired StringRedisTemplate stringRedisTemplate) {
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
String name = ops.get("testKey");
System.out.println(name);
}
}
客户端选择:jedis
导入依赖
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
dependency>
配置客户端:
spring:
redis:
host: localhost # 127.0.0.1
port: 6379
client-type: jedis
lettcus与jedis区别
♦ 存储位置:数据库
♦ 特征:永久性存储,修改频度极低
♦ 存储位置:数据库、Mongdb
♦ 特征:永久性存储与临时存储相结合、修改频度较高
♦ 存储位置:数据库、Mongdb
♦ 特征:永久性存储与临时存储相结合、修改频度较高
♦ 存储位置:Mongdb
♦ 特征:临时存储,修改频度飞速
db.集合名称.insert/save/insertOne(文档)
db.集合名称.remove(条件)
db.集合名称.update(条件,{操作种类:{文档}})
♦ 查询全部: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域
♦ 基本格式: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
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>
spring:
data:
mongodb:
uri: mongodb://localhost/itheima
@SpringBootTest
class Springboot17MongodbApplicationTests {
@Autowired
private MongoTemplate mongoTemplate;
@Test
void contextLoads() {
Book book = new Book();
book.setId(2 );
book.setName("spring1");
book.setType("spring1");
book.setDescription("spring1");
mongoTemplate.save(book);
}
@Test
void find() {
List<Book> all = mongoTemplate.findAll(Book.class);
System.out.println(all);
}
}
运行 elasticsearch.bat
运行失败修改bin目录下elasticsearch-env.bat的JDK路径
安装ik分词器解压到plugins目录下
PUT http:/localhost:9200/books
GET http:/localhost:9200/books
DELETE http:/localhost:9200/books
{
"mappings" : {
"properties":{
"id":{
"type":"keyword"
},
"name":{
"type":"text",
"analyzer":"ik_max_word",
"copy_to":"all"
},
"type":{
"type":"keyword"
},
"description":{
"type":"text",
"analyzer":"ik_max_word" ,
"copy_to":"all"
},
"all":{
"type":"text",
"analyzer":"ik_max_word"
}
}
}
}
POST http:/localhost:9200/books/_doc #使用系统生成的id
POST http:/localhost:9200/books/_create/1 #使用指定id
POST http:/localhost:9200/books/_doc/1 #使用指定id,不存在创建,存在更新(版本递增)
{
"name" : "springboot",
"type" : "springboot",
"description" : "springboot"
}
GET http:/localhost:9200/books/_doc/1 #查询单个文档
GET http:/localhost:9200/books/_search #查询全部文档
Get http:/localhost:9200/books/_search?q=name:springboot
DELETE http:/localhost:9200/books/_doc/1
PUT http:/localhost:9200/books/_doc/1
{
"name" : "springboot",
"type" : "springboot",
"description" : "springboot";
}
PUT http:/localhost:9200/books/_update/1
{
"doc" : {
"name" : "springboot"
}
}
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>spring-boot-start-data-elasticsearchartifactId>
dependency>
spring:
elasticsearch:
=rest:
uris: http://localhost:9200
@SpringBootTest
class Springboot18EsApplicationTests{
@Autowired
private ElasticsearchRestTemplate template;
}
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
dependency>
@Test
void testCreateClient() throws IOException {
HttpHost host = HttpHost.create("http://localhost:9200");
RestClientBuilder builder = RestClient.builder(host);
RestHighLevelClient = new RestHighLevelClient(builder);
//客户端操作
CreateIndexRequest request = new CreateIndexRequest("books");
//获取操作索引的客户端对象,调用创建索引操作
client.indices().create(request, RequestOptions.DEFAULT);
//关闭客户端
client.close();
}
@SpringBootTest
class Springboot18EsApplicationTests {
@Autowired
private BookDao bookDao;
@Autowired
RestHighLevelClient client;
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
@Test
void testCreateByIk() throws IOException {
HttpHost host = HttpHost.create("http://localhost:9200");
RestClientBuilder builder = RestClient.builder(host);
RestClientBuilder client = new RestHighLevelClient(builder);
//客户端操作
CreateIndexRequest request = new CreateIndexRequest("books");
//设置要执行的操作
String json = " ";
//设置请求中的参数,参数类型json数据
request.source(json, XContentType.JSON);
//获取操作索引的客户端对象,调用创建索引操作
client.indices().create(request, RequestOptions.DEFAULT);
//关闭客户端
client.close();
}
@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> 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);
}
client.bulk(bulkRequest, RequestOptions.DEFAULT);
}
//按id查询
@Test
void testGet() 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 builder = new SearchSourceBuilder();
builder.query(QueryBuilders.termQuery("name", "java"));
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
Book book = JSON.parseObject(sourceAsString, Book.class);
System.out.println(book);
}
}