版本说明
- Springboot 2.7.5
- JDK 17
- Elasticsearch 7.14.0
- Easy-Es 1.1.1
- 《点我进入Easy-Es官网》PS:目前Easy-Es暂不支持SpringBoot3.X
Windows10安装Elasticsearch
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.aoxqwl</groupId>
<artifactId>eedemo</artifactId>
<version>1</version>
<name>eedemo</name>
<description>eedemo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok插件依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- Easy-Es暂不支持SpringBoot3.X,且推荐Elasticsearch版本为7.14.0 -->
<dependency>
<groupId>cn.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>1.1.1</version>
<exclusions>
<exclusion>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.14.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.14.0</version>
</dependency>
</dependencies>
application.yml
easy-es:
enable: true #默认为true,若为false则认为不启用本框架
banner: false # 默认为true 打印banner 若您不期望打印banner,可配置为false
address : 127.0.0.1:9200 # es的连接地址,必须含端口 若为集群,则可以用逗号隔开 例如:127.0.0.1:9200,127.0.0.2:9200
username: elastic #若无 则可省略此行配置
password: WG7WVmuNMtM4GwNYkyWH #若无 则可省略此行配置
# 请求通信超时时间 单位:ms 在平滑模式下,由于要迁移数据,用户可根据数据量大小调整此参数值大小,否则请求容易超时导致索引托管失败,建议您尽量给大不给小
socketTimeout: 5000
global-config:
# 项目是否分布式环境部署,默认为true, 如果是单机运行可填false,将不加分布式锁,效率更高.
distributed: false
Document实体类
import cn.easyes.annotation.HighLight;
import cn.easyes.annotation.IndexField;
import cn.easyes.annotation.IndexId;
import cn.easyes.annotation.IndexName;
import cn.easyes.annotation.rely.FieldType;
import cn.easyes.annotation.rely.IdType;
import lombok.Data;
@Data
@IndexName(value = "document")
public class Document {
/**
* es中的唯一id,如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize,如此id便支持任意数据类型)
*/
@IndexId(type = IdType.NONE)
private String id;
/**
* 文档标题,不指定类型默认被创建为keyword类型,可进行精确查询
*/
@IndexField(value = "title")
private String title;
/**
* 文档内容,指定了类型及存储/查询分词器
*/
@HighLight(mappingField = "highlightContent") //对应下面的highlightContent变量名
@IndexField(value = "content", fieldType = FieldType.TEXT)
private String content;
/**
* 创建时间
*/
@IndexField(value = "create_time", fieldType = FieldType.DATE, dateFormat = "yyyy-MM-dd HH:mm:ss")
private String createTime;
/**
* 不存在的字段
*/
@IndexField(exist = false)
private String notExistsField;
/**
* 地理位置经纬度坐标 例如: "40.13933715136454,116.63441990026217"
*/
@IndexField(fieldType = FieldType.GEO_POINT)
private String location;
/**
* 图形(例如圆心,矩形)
*/
@IndexField(fieldType = FieldType.GEO_SHAPE)
private String geoLocation;
/**
* 自定义字段名称
*/
@IndexField(value = "wu-la")
private String customField;
/**
* 高亮返回值被映射的字段
*/
private String highlightContent;
}
DocumentMapper (官方建议:Easy-Es的Mapper和MyBatis-Plus分开存放)
和MyBatis-Plus差不多都是继承mapper,但是Easy-Es不需要继承Service和ServiceImpl
import cn.easyes.core.conditions.interfaces.BaseEsMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface DocumentMapper extends BaseEsMapper<Document> {
}
DocumentService
人家没有帮我们写,那我们就自己写
public interface DocumentService {
Integer insert(Document document);
Document selectById(String id);
Integer updateById(Document document);
Integer deleteById(String id);
List<Document> selectList();
EsPageInfo<Document> pageQuery(Integer pageIndex, Integer pageSize);
Integer deleteBatchIds(List<String> ids);
}
DocumentServiceImpl
@Service
public class DocumentServiceImpl implements DocumentService {
@Resource
private DocumentMapper documentMapper;
@Override
public Integer insert(Document document) {
return this.documentMapper.insert(document);
}
@Override
public Document selectById(String id) {
return this.documentMapper.selectById(id);
}
@Override
public Integer updateById(Document document) {
return this.documentMapper.updateById(document);
}
@Override
public Integer deleteById(String id) {
return this.documentMapper.deleteById(id);
}
@Override
public List<Document> selectList() {
LambdaEsQueryWrapper<Document> leqw = new LambdaEsQueryWrapper<>();
return this.documentMapper.selectList(leqw);
}
@Override
public EsPageInfo<Document> pageQuery(Integer pageIndex, Integer pageSize) {
LambdaEsQueryWrapper<Document> leqw = new LambdaEsQueryWrapper<>();
return this.documentMapper.pageQuery(leqw, pageIndex, pageSize);
}
@Override
public Integer deleteBatchIds(List<String> ids) {
return this.documentMapper.deleteBatchIds(ids);
}
}
DocumentController
@RestController
@RequestMapping("document")
public class DocumentController {
@Resource
private DocumentService documentService;
/**
* 测试程序是否正常运行
*/
@GetMapping("hello")
public String hello() {
return "hello world!";
}
/**
* 新增
*/
@PostMapping
public Integer insert(@RequestBody Document document) {
return this.documentService.insert(document);
}
/**
* 查询
*/
@GetMapping("{id}")
public Document selectById(@PathVariable String id) {
return this.documentService.selectById(id);
}
/**
* 更新
*/
@PutMapping
public Integer updateById(@RequestBody Document document) {
return this.documentService.updateById(document);
}
/**
* 删除
*/
@DeleteMapping("{id}")
public Integer deleteById(@PathVariable String id) {
return this.documentService.deleteById(id);
}
/**
* 查询列表
*/
@GetMapping
public List<Document> selectList() {
return this.documentService.selectList();
}
/**
* 分页查询
*/
@GetMapping("paging")
public EsPageInfo<Document> pageQuery(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
@RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize) {
return this.documentService.pageQuery(pageIndex, pageSize);
}
/**
* 批量删除
*/
@DeleteMapping
public Integer deleteBatchIds(@RequestBody List<String> ids) {
return this.documentService.deleteBatchIds(ids);
}
}
Application启动类加上扫描EsMapper接口注解
@SpringBootApplication
@EsMapperScan("com.aoxqwl.eedemo.mapper.ee") //还是那句话,建议和MyBatis-Plus的mapper分开存放
public class EedemoApplication {
public static void main(String[] args) {
SpringApplication.run(EedemoApplication.class, args);
}
}
结束语
整体下来和MyBatis-Plus是非常贴合的,如果你的项目中使用到了MyBatis-Plus,那么首选是Easy-Es!同样的Easy-Es目前占卜支持SpringBoot3.X,也是个美中不足吧。但是我想目前大家的生产环境都没上SpringBoot3.X吧!接口那些就自己用Postman测一下吧,都是没有问题的。