1.引入相关依赖
//提供getset 让代码更加简洁 org.springframework.boot spring-boot-starter-data-elasticsearch2.3.4.RELEASE //mybatis 提供分页 org.projectlombok lomboktrue com.github.pagehelper pagehelper5.3.0
2.yml配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/nsbdcms?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
data:
elasticsearch:
cluster-name: es-Cluster
cluster-nodes: 192.168.126.128:9200
repositories:
enabled: true
es:
url: 192.168.126.128:9200
index: sougoulog
#mybatis plus 设置
mybatis-plus:
type-aliases-package: com.example.jsoupdome.domin
mapper-locations: classpath:mapper
@Configuration
public class Client {
@Value("${es.url}")
private String esUrl;
@Bean
RestHighLevelClient configRestHighLevelClient() throws Exception {
String[] esUrlArr = esUrl.split(",");
List httpHosts = new ArrayList<>();
for(String es : esUrlArr){
String[] esUrlPort = es.split(":");
httpHosts.add(new HttpHost(esUrlPort[0], Integer.parseInt(esUrlPort[1]), "http"));
}
return new RestHighLevelClient(
RestClient.builder(httpHosts.toArray(new HttpHost[0]))
);
}
3.创建实体类对应于数据库表 一一对应列表信息
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.document;
import java.util.Date;
@Data
@TableName(value = "t_cms_content_news")
@document(indexName = "content")
public class TCmsContentNews {
@TableId(value = "content_id",type = IdType.INPUT)
private Long contentId;
@ApiModelProperty(value="内容")
private String content;
@ApiModelProperty(value="附件")
private String fujian;
@ApiModelProperty(value="来源")
private String laiyuan;
private String val;
@TableField("create_time")
private Date createTime;
@TableField("update_time")
private Date updateTime;
}
4.ContentEsRepository接口 继承 ElasticsearchRepository 这个类,提供操作es api 直接用就可以
import com.example.jsoupdome.domin.TCmsContentNews; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface ContentEsRepository extends ElasticsearchRepository{ }
5.然后就三层架构了
controller
import com.example.jsoupdome.service.ContentEsServce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/update")
public class ContentEsController {
@Autowired
private ContentEsServce contentEsServce;
@RequestMapping(value = "/importEs",method = RequestMethod.POST)
public String importEsByContent(){
this.contentEsServce.saveEsByContent();
return "success" ;
}
}
ContentEsServce接口
public interface ContentEsServce {
void saveEsByContent();
}
ContentEsServceImpl实现类
import com.example.jsoupdome.dao.ContentMapper;
import com.example.jsoupdome.repository.ContentEsRepository;
import com.example.jsoupdome.domin.TCmsContentNews;
import com.example.jsoupdome.service.ContentEsServce;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.linkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Service
public class ContentEsServceImpl implements ContentEsServce {
@Autowired
private ContentEsRepository contentEsRepository;
@Autowired
private ContentMapper contentMapper;
public static ThreadPoolExecutor es = new ThreadPoolExecutor(50, 100, 0L, TimeUnit.SECONDS,new linkedBlockingQueue());
@Override
public void saveEsByContent() {
//查询全部数据
List content = this.contentMapper.selectList(null);
//获取总数
int total = content.size();
//定义每页个数 --前台传来
final int pagesize = 100;
//求出当每页为pagesize 时候,可以分几页,就循环几次 ,分页插入
int pagenum = (int) Math.ceil(total / pagesize);
System.out.println("一共有"+pagenum+"页");
//分页导入
for (int i = 1; i <= pagenum; i++) {
//分页对象 mybatis 提供的
PageHelper.startPage(i,pagesize);
PageInfo tcmFilePageInfo = new PageInfo(content);
System.out.println("正在导入第" + i + "页数据");
if(tcmFilePageInfo.getSize()>0){
es.submit(()->{
contentEsRepository.saveAll(tcmFilePageInfo.getList());
});
}
}
System.out.println("导入完毕!");
}
}
dao层
import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.example.jsoupdome.domin.TCmsContentNews; public interface ContentMapper extends baseMapper{ }
然后 post 请求一下 http://localhost:8888/update/importEs
我们打开head 插件查询数据
说明插入成功 了



