导读
前二天,写了一篇ElasticSearch7.8.1从入门到精通的(点我直达),但是还没有整合到SpringBoot中,下面演示将ElasticSearch和mysql整合到Spring Boot中,附演示源码。
项目介绍
模仿NBA网站
网址地址:点我直达
接口开发
将数据库数据导入到ElasticSearch
通过姓名查找球员
通过国家或者球队查询球员
通过姓名字母查找球员
项目搭建
SpringBoot整合ElasticSearch和Mysql
数据库数据
将百度云盘里的sql,在mysql上运行即可
链接: https://pan.baidu.com/s/1MJaJy8isfVnPha00tlS8_w 密码: u3dg
项目结构
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.2.RELEASE
com.cyb
yb_search
0.0.1-SNAPSHOT
yb_search
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.8.1
org.elasticsearch
elasticsearch
7.8.1
com.alibaba
fastjson
1.2.73
com.alibaba
druid
1.1.23
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.3
mysql
mysql-connector-java
8.0.21
mysql
mysql-connector-java
8.0.21
org.apache.commons
commons-lang3
3.11
org.springframework.boot
spring-boot-starter-test
test
junit
junit
4.12
test
org.springframework.boot
spring-boot-maven-plugin
注意
我本地安装的ElasticSearch版本是7.8.1,引入pom的ES依赖的话,最好版本一致,否则可能出现版本兼容问题!!!!!!!!!!!
application.properties
# 端口号
server.port=8083
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/nba?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
# 连接池
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 表明使用Druid连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#初始化时建立物理连接的个数。
spring.datasource.druid.initial-size=5
#最大连接池数量
spring.datasource.druid.max-active=20
#最小连接池数量
spring.datasource.druid.min-idle=5
#获取连接时最大等待时间,单位毫秒
spring.datasource.druid.max-wait=3000
#是否缓存preparedStatement,也就是PSCache,PSCache对支持游标的数据库性能提升巨大,比如说oracle,在mysql下建议关闭。
spring.datasource.druid.pool-prepared-statements=false
#要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100
spring.datasource.druid.max-open-prepared-statements= -1
#配置检测可以关闭的空闲连接间隔时间
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 配置连接在池中的最小生存时间
spring.datasource.druid.min-evictable-idle-time-millis= 300000
spring.datasource.druid.max-evictable-idle-time-millis= 400000
# 日志相关
logging.level.root: info
logging.level.com.cyb.search: debug
# ElasticSearch配置
elasticsearch.host:192.168.199.170
elasticsearch.port=9200
EsConfig.java(ES配置类)
packagecom.cyb.search.config;importorg.apache.http.HttpHost;importorg.elasticsearch.client.RestClient;importorg.elasticsearch.client.RestHighLevelClient;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;/*** @ClassName:EsConfig
* @Description:ES配置文件
* @Author:chenyb
* @Date:2020/8/10 11:25 下午
* @Versiion:1.0*/@Configuration//获取application.properties或application.yml获取里面的参数值
@ConfigurationProperties(prefix = "elasticsearch")public classEsConfig {privateString host;privateInteger port;//初始化RestHighLevelClient
@Bean(destroyMethod = "close")publicRestHighLevelClient client(){return newRestHighLevelClient(RestClient.builder(new HttpHost(host,port,"http")
));
}publicString getHost() {returnhost;
}public voidsetHost(String host) {this.host =host;
}publicInteger getPort() {returnport;
}public voidsetPort(Integer port) {this.port =port;
}
}
NBAPlayerDao.java
packagecom.cyb.search.dao;importcom.cyb.search.model.NBAPlayer;importorg.apache.ibatis.annotations.Mapper;importorg.apache.ibatis.annotations.Select;importjava.util.List;
@Mapperpublic interfaceNBAPlayerDao {
@Select("select * from nba_player")
ListselectAll();
}
NBAPlayer.java(实体类)
packagecom.cyb.search.model;/*** @ClassName:NBAPlayer
* @Description:TODO
* @Author:chenyb
* @Date:2020/8/10 11:39 下午
* @Versiion:1.0*/
public classNBAPlayer {privateInteger id;privateString countryEn;privateString country;privateString code;privateString displayAffiliation;privateString displayName;privateInteger draft;privateString schoolType;privateString weight;privateInteger playYear;privateString jerseyNo;privateLong birthDay;privateString birthDayStr;privateString displayNameEn;privateString position;privateDouble heightValue;privateString playerId;privateString teamCity;privateString teamCityEn;privateString teamName;privateString teamNameEn;privateString teamConference;privateString teamConferenceEn;privateInteger age;publicString getCountry() {returncountry;
}public voidsetCountry(String country) {this.country =country;
}publicString getBirthDayStr() {returnbirthDayStr;
}public voidsetBirthDayStr(String birthDayStr) {this.birthDayStr =birthDayStr;
}publicInteger getAge() {returnage;
}public voidsetAge(Integer age) {this.age =age;
}publicString getCode() {returncode;
}public voidsetCode(String code) {this.code =code;
}publicString getDisplayAffiliation() {returndisplayAffiliation;
}public voidsetDisplayAffiliation(String displayAffiliation) {this.displayAffiliation =displayAffiliation;
}publicString getDisplayName() {returndisplayName;
}public voidsetDisplayName(String displayName) {this.displayName =displayName;
}publicInteger getDraft() {returndraft;
}public voidsetDraft(Integer draft) {this.draft =draft;
}publicString getSchoolType() {returnschoolType;
}public voidsetSchoolType(String schoolType) {this.schoolType =schoolType;
}publicString getWeight() {returnweight;
}public voidsetWeight(String weight) {this.weight =weight;
}publicInteger getPlayYear() {returnplayYear;
}public voidsetPlayYear(Integer playYear) {this.playYear =playYear;
}publicString getCountryEn() {returncountryEn;
}public voidsetCountryEn(String countryEn) {this.countryEn =countryEn;
}publicString getTeamCityEn() {returnteamCityEn;
}public voidsetTeamCityEn(String teamCityEn) {this.teamCityEn =teamCityEn;
}publicString getTeamNameEn() {returnteamNameEn;
}public voidsetTeamNameEn(String teamNameEn) {this.teamNameEn =teamNameEn;
}publicString getTeamConference() {returnteamConference;
}public voidsetTeamConference(String teamConference) {this.teamConference =teamConference;
}publicString getTeamConferenceEn() {returnteamConferenceEn;
}public voidsetTeamConferenceEn(String teamConferenceEn) {this.teamConferenceEn =teamConferenceEn;
}publicString getJerseyNo() {returnjerseyNo;
}public voidsetJerseyNo(String jerseyNo) {this.jerseyNo =jerseyNo;
}publicLong getBirthDay() {returnbirthDay;
}public voidsetBirthDay(Long birthDay) {this.birthDay =birthDay;
}publicString getDisplayNameEn() {returndisplayNameEn;
}public voidsetDisplayNameEn(String displayNameEn) {this.displayNameEn =displayNameEn;
}publicString getPosition() {returnposition;
}public voidsetPosition(String position) {this.position =position;
}publicDouble getHeightValue() {returnheightValue;
}public voidsetHeightValue(Double heightValue) {this.heightValue =heightValue;
}publicString getPlayerId() {returnplayerId;
}public voidsetPlayerId(String playerId) {this.playerId =playerId;
}publicString getTeamCity() {returnteamCity;
}public voidsetTeamCity(String teamCity) {this.teamCity =teamCity;
}publicString getTeamName() {returnteamName;
}public voidsetTeamName(String teamName) {this.teamName =teamName;
}publicInteger getId() {returnid;
}public voidsetId(Integer id) {this.id =id;
}
}
NBAPlayerService.java(接口)
packagecom.cyb.search.service;importcom.cyb.search.model.NBAPlayer;importjava.io.IOException;public interfaceNBAPlayerService {boolean addPlayer(NBAPlayer player,String id) throwsIOException;
}
NBAPlayerServiceImpl.java
packagecom.cyb.search.service.impl;importcom.alibaba.fastjson.JSONObject;importcom.cyb.search.model.NBAPlayer;importcom.cyb.search.service.NBAPlayerService;importorg.elasticsearch.action.index.IndexRequest;importorg.elasticsearch.action.index.IndexResponse;importorg.elasticsearch.client.RequestOptions;importorg.elasticsearch.client.RestHighLevelClient;importorg.springframework.cglib.beans.BeanMap;importorg.springframework.stereotype.Service;importjavax.annotation.Resource;importjava.io.IOException;importjava.util.HashMap;importjava.util.Map;/*** @ClassName:NBAPlayerServiceImpl
* @Description:TODO
* @Author:chenyb
* @Date:2020/8/11 10:09 下午
* @Versiion:1.0*/@Servicepublic class NBAPlayerServiceImpl implementsNBAPlayerService {
@ResourceprivateRestHighLevelClient client;/*** 添加
*@paramplayer 实体类
*@paramid 编号
*@return*@throwsIOException*/@Overridepublic boolean addPlayer(NBAPlayer player, String id) throwsIOException {
IndexRequest request=new IndexRequest("nba_latest").id(id).source(beanToMap(player));
IndexResponse response=client.index(request, RequestOptions.DEFAULT);
System.out.println(JSONObject.toJSON(response));return false;
}/*** 对象转map
*@parambean
*@param
*@return
*/
public static MapbeanToMap(T bean){
Map map=new HashMap<>();if (bean!=null){
BeanMap beanMap=BeanMap.create(bean);for(Object key:beanMap.keySet()){if (beanMap.get(key)!=null){
map.put(key+"",beanMap.get(key));
}
}
}returnmap;
}
}
基础功能实现
往ES中插入一条数据
查看数据库数据
根据ID查ES
单元测试
修改
单元测试
删除
单元测试
将数据库中的数据导入ES
通过名字查找球员
测试
通过国家或球队查找球员
测试
通过字母查球员
测试
项目源码下载
链接: https://pan.baidu.com/s/1QJ8wvjg7TPqGSP-68qpSIQ 密码: d26m