概念
1.1 Druid
Druid是阿里巴巴开发的一个连接池,他提供了一个高效、功能强大、可扩展性好的数据库连接池。Druid是一个高效的数据查询系统,主要解决的是对于大量的基于时序的数据进行聚合查询。数据可以实时摄入,进入到Druid后立即可查,同时数据是几乎是不可变。通常是基于时序的事实事件,事实发生后进入Druid,外部系统就可以对该事实进行查询。
shared-nothing架构与lambda架构
1.快速查询(Fast Query) : 部分数据聚合(Partial Aggregate) + 内存化(In-Memory) + 索引(Index)
2.水平拓展能力(Horizontal Scalability):分布式数据(Distributed data)+并行化查询(Parallelizable Query)
3.实时分析(Realtime Analytics):Immutable Past , Append-Only Future
1.2 mybatis-plus
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
参考:mybatis-plus的官方文档
操作步骤
2.1 导入依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency>
|
2.2 添加配置参数
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
| spring: datasource: url: jdbc:mysql://localhost:3306/kevin_db_dev?useUnicode=true&&characterEncoding=utf-8&&useSSL=false username: root password: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver druid: stat-view-servlet: enabled: true url-pattern: /druid/* reset-enable: true login-username: root login-password: 123456 allow: deny: filter: stat: enabled: true log-slow-sql: true slow-sql-millis: 3000 merge-sql: true wall: config: multi-statement-allow: true
|
(更多配置)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| spring: datasource: druid: initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 filters: stat,wall
schema: - classpath:department.sql
|
2.3 搭建测试环境
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.zhu.common.entity.dos;
import com.baomidou.mybatisplus.annotation.TableName; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
@Data @AllArgsConstructor @NoArgsConstructor @TableName("tb_student") public class Student { private Integer id; private String name; private Integer age; }
|
1 2 3 4 5 6 7 8 9 10
| package com.zhu.common.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.zhu.common.entity.dos.Student; import org.apache.ibatis.annotations.Mapper;
@Mapper public interface StudentMapper extends BaseMapper<Student> { }
|
1 2 3 4 5 6 7
| package com.zhu.common.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.zhu.common.entity.dos.Student;
public interface StudentService extends IService<Student> { }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.zhu.common.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.zhu.common.entity.dos.Student; import com.zhu.common.mapper.StudentMapper; import com.zhu.common.service.StudentService; import org.springframework.stereotype.Service;
@Service public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService { }
|
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
| package com.zhu.common;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.zhu.common.entity.dos.Student; import com.zhu.common.service.StudentService; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
@Slf4j @RunWith(SpringRunner.class) @SpringBootTest(classes = CommonApplication.class) public class StudentTest {
@Autowired private StudentService studentService;
@Test public void testInsert(){ Student stu=new Student(); stu.setName("张三"); stu.setAge(18); log.info(String.valueOf(studentService.save(stu))); }
@Test public void testQuery(){ log.info(studentService.getOne( Wrappers.<Student>lambdaQuery() .eq(Student::getName,"张三")) .toString()); } }
|
插入成功:
查询成功: