环境
JAVA8
MYSQL 5.7
ES 7.12.1
步骤
创建测试表student
CREATE TABLE `student` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(64) NULL DEFAULT NULL,
`score` int(32) NULL DEFAULT NULL COMMENT '分数',
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
`update_time` datetime NULL DEFAULT NULL COMMENT '修改时间',
`create_user` bigint(20) NULL DEFAULT NULL COMMENT '创建人',
`update_user` bigint(20) NULL DEFAULT NULL COMMENT '修改人',
`is_deleted` tinyint(1) NULL DEFAULT NULL COMMENT '是否删除',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1791004050214227970 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
项目引入MBP包和EE依赖(EE和MBP官方文档都有提供)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Easy-EE相关包-->
<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>
<dependency>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>2.0.0-beta7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 整合fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.24</version>
</dependency>
<!-- commons-lang整合-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<!-- durid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<!-- mysql连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!--mybatis-plus连接-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
MBP生成工具生成实体类 添加对应注解
/**
* <p>
*
* </p>
*
* @author GBB
* @since 2024-05-16
*/
@Data
@Accessors(chain = true)
@TableName("student") //MYSQL表名
@NoArgsConstructor
@AllArgsConstructor
@IndexName("student") //创建Es索引
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.ASSIGN_ID) //mysql记录Id
@IndexId(type= org.dromara.easyes.annotation.rely.IdType.CUSTOMIZE) //ES中文档Id
private Long id;
/**
* 添加查询高亮 使用IK分词器
*/
@HighLight
@IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)
private String name;
private Integer age;
private Integer score;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
@TableField(fill = FieldFill.INSERT)
private Integer isDeleted;
}
es和mbp都需要扫描mapper文件,建议Mapper层对mapper文件作隔离
ES的mapper文件
public interface StudentEsMapper extends BaseEsMapper<Student> {
}
MBP对应的Mapper文件
public interface StudentMapper extends BaseMapper<Student> {
}
controller层
@RestController
@RequiredArgsConstructor
@RequestMapping("/student")
public class StudentController {
private final StudentEsMapper studentEsMapper;
private final IStudentService studentService;
@GetMapping("/createIndex")
public Boolean createIndex() {
// 初始化-> 创建索引
return studentEsMapper.createIndex();
}
@PostMapping("/add")
public Integer addStudent(@RequestBody Student student){
boolean save = studentService.save(student);
return (save? studentEsMapper.insert(student):-1) ;
}
@GetMapping("/search")
public List<Student> addStudent(){
LambdaEsQueryWrapper<Student> wrapper = new LambdaEsQueryWrapper<>();
wrapper.like(StringUtils.isNotEmpty("张"), Student::getName, "张");
List<Student> students = studentEsMapper.selectList(wrapper);
return students;
}
@PostMapping("/update")
public Integer update(@RequestBody Student student){
boolean b = studentService.updateById(student);
return b?studentEsMapper.updateById(student):-1;
}
进行测试操作
操作前
测试创建索引
GET: localhost:3000/student/createIndex 返回true表示创建完成
测试添加数据
POST: localhost:3000/student/add 返回1表示MYSQL添加和ES同步完成
查看结果
测试修改接口
POST: localhost:3000/student/update 返回1表示完成
查看结果
查询查看EE官方文档,这里不在演示