时间:2021-05-20
0.版本选择
我这里选择了5.6.x,记得如果spring-boot-starter-parent是1.x可以选择2.x版本的elasticsearch,版本要对应,不然会有莫名其妙的问题
1.安装ElasticSearch
https://.example.demo.repository.GoodsRepository;import io.swagger.annotations.Api;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiOperation;import org.elasticsearch.index.query.QueryBuilder;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Pageable;import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;import org.springframework.data.elasticsearch.core.query.SearchQuery;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*; import java.util.List;import java.util.Optional; import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery; @Controller@Api(value = "HelloWorldController|一个用来测试swagger注解的控制器",tags = "HelloWorldController", description = "HelloWorldController")public class HelloWorldController { @Autowired private GoodsRepository goodsRepository; @ResponseBody @RequestMapping(value = "/hello", method = RequestMethod.GET) @ApiOperation(value = "根据用户编号获取用户姓名", notes = "test: 仅1和2有正确返回") @ApiImplicitParam(paramType="query", name = "userNumber", value = "用户编号", required = true, dataType = "Integer") public String index(@RequestParam Integer userNumber){ if(userNumber == 1){ return "小白"; } else if(userNumber == 2){ return "小红"; } else{ return "未知"; } } @ResponseBody @RequestMapping(value = "/save", method = RequestMethod.POST) @ApiOperation(value = "新增商品") public String save(){ GoodsInfo goodsInfo = new GoodsInfo(System.currentTimeMillis(), "商品" + System.currentTimeMillis(), "这是一个测试商品"); goodsRepository.save(goodsInfo); return "success"; } @ResponseBody @RequestMapping(value = "/delete", method = RequestMethod.POST) @ApiOperation(value = "删除商品") @ApiImplicitParam(paramType="query", name = "id", value = "商品id", required = true, dataType = "long") public String delete(@RequestParam(required = true) long id){ goodsRepository.deleteById(id); return "success"; } @ResponseBody @RequestMapping(value = "/update", method = RequestMethod.POST) @ApiOperation(value = "更新商品") @ApiImplicitParam(paramType="query", name = "id", value = "商品id", required = true, dataType = "long") public String update(@RequestParam(required = true) long id, @RequestParam(required = false) String name, @RequestParam(required = false) String description){ Optional<GoodsInfo> goodsInfo = goodsRepository.findById(id); if(goodsInfo.isPresent()){ if(name != null){ goodsInfo.get().setName(name); } if(description != null){ goodsInfo.get().setDescription(description); } goodsRepository.save(goodsInfo.get()); return "success"; }else{ return "no find"; } } @ResponseBody @RequestMapping(value = "/getOne", method = RequestMethod.GET) @ApiOperation(value = "得到一个商品") @ApiImplicitParam(paramType="query", name = "id", value = "商品id", required = true, dataType = "long") public Optional<GoodsInfo> getOne(@RequestParam(required = true) long id){ Optional<GoodsInfo> goodsInfo = goodsRepository.findById(id); return goodsInfo; } private SearchQuery getEntitySearchQuery(int pageNumber, String searchContent){ Pageable pageable = PageRequest.of(pageNumber, 20); SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(queryStringQuery(searchContent)).withPageable(pageable).build(); return searchQuery; } @ResponseBody @RequestMapping(value = "/search", method = RequestMethod.GET) @ApiOperation(value = "搜索商品") public List<GoodsInfo> search(@RequestParam(required = true) Integer pageNumber, @RequestParam(required = true) String query){ SearchQuery searchQuery = getEntitySearchQuery(pageNumber, query); Page<GoodsInfo> goodsPage = goodsRepository.search(searchQuery); return goodsPage.getContent(); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Springboot2.1.X整合Elasticsearch最新版的一处问题新版本的Springboot2的spring-boot-starter-data-e
本文介绍了springboot的maven配置依赖详解,分享给大家,具体如下:我们通过引用spring-boot-starter-parent,添加spring
一、使用mybatis-spring-boot-starter1、添加依赖org.mybatis.spring.bootmybatis-spring-boot-
1.什么是spring-boot-devtoolsspring-boot-devtools是spring-boot项目开发时的一个热部署工具,安装了spring
一)spring-boot-starter命名规则自动配置模块命名规则:xxx-spring-boot,如:aspectlog-spring-boot启动器命名