---
title: "elasticsearch，spring boot，mybatis项目小结"
author: "rainbow_小春"
date: "2017-10-25T14:19:00Z"
tags: []
categories: ["研发管理"]
slug: "elasticsearch-spring-boot-mybatis"
draft: false
lastmod: "2017-10-25T19:12:27Z"
id: 2397574
url: "https://719607746.iteye.com/blog/2397574"
---

最近接手一个活，用springboot整合mybatis获取一批数据，上传到elastic search建索引。小小研究了一下，操作不多，没有很难。

很好的教程

[http://www.sojson.com/blog/81.html](http://www.sojson.com/blog/81.html)

例

[https://yq.aliyun.com/articles/70054](https://yq.aliyun.com/articles/70054)

 

遇到比较典型的错：

[错1](https://stackoverflow.com/questions/8367312/serializing-with-jackson-json-getting-no-serializer-found)

[https://stackoverflow.com/questions/8367312/serializing-with-jackson-json-getting-no-serializer-found](https://stackoverflow.com/questions/8367312/serializing-with-jackson-json-getting-no-serializer-found)

No serializer found for class elasticSearch.LogModel and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL\_ON\_EMPTY\_BEANS)

大意是模板类没有set方法，故报错。用对象转json的时候报的。

 

错2

[http://blog.csdn.net/z69183787/article/details/22269131](http://blog.csdn.net/z69183787/article/details/22269131)

Jackson2需要引3个maven包

&lt;!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --&gt;

&lt;dependency&gt;

&lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;

&lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;

&lt;version&gt;2.9.0.pr3&lt;/version&gt;

&lt;/dependency&gt;

&lt;!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --&gt;

&lt;dependency&gt;

&lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;

&lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;

&lt;version&gt;2.9.0.pr3&lt;/version&gt;

&lt;/dependency&gt;

&lt;!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --&gt;

&lt;dependency&gt;

&lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;

&lt;artifactId&gt;jackson-core&lt;/artifactId&gt;

&lt;version&gt;2.9.0.pr3&lt;/version&gt;

 

 

NoNodeAvailableException\[None of the configured nodes are available: \[{#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9201}]]

这个是因为配置文件中没有配，网上有文章写创建conf，真是乱讲。（后来又报了一次这个错，是因为client连接关闭了还调用连接，spring的@autowire标签初始化类只调用一次构造方法，我创建连接的代码写构造方法里了，

当心client关闭后再次访问，会报找不着节点的错误。）

1. $ vi config/elasticsearch.yml
2. #cluster name
3. cluster.name: sojson-application
4. #节点名称
5. node.name: node-1
6. #绑定IP和端口
7. network.host:123.88.88.88
8. http.port:9200

 

 

还有在程序中创建连接时，端口写9300

client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.0.106"), 9300));

9300端口： ES节点之间通讯使用

9200端口： ES节点 和 外部 通讯使用

我们这里是添加节点，故使用9300端口。

 

启动elastic search报错，用这个命令启动。

failed to delete temp file /Users/lasia/Software/elasticsearch-2.1.0/data/gafisperson/nodes/0/indices/person/1/translog/translog-4113158842387570706.tlog

java.nio.file.NoSuchFileException: /Users/lasia/Software/elasticsearch-2.1.0/data/gafisperson/nodes/0/indices/person/1/translog/translog-4113158842387570706.tlog

 

./elasticsearch -Des.insecure.allow.root=true -d

后台启动

 

做这个项目时还用到了springboot mybatis,操作数据库。

很简单，配置mapping.xml，再在类中添加方法就可以了。需要注意添加方法时sql语句的通配参数需要转换一下。

long minSeq(@Param("name")String name);

 

&lt;select id="minSeq" resultType="long"&gt;

select seq from monad\_load\_config where name = #{name} and deletag = '1'

&lt;/select&gt;

而且，map配置文件中有大于号小于号的时候，需要注意转义。

&lt; &gt;

&lt;!\[CDATA\[]]&gt;

发现数据库查询的时候，要分根据seq分组查询，必须先进行排序，再切割（rownum&lt;10）,不然只会排切割的区间···还好发现的早，不然得丢好多数据···

SELECT * from (select lower(p.personid) as personid, p.fingerrepeatno as fingerrepeatno

from gafis\_person p left join sys\_depart d on p.gather\_org\_code = d.code left join code\_ly ly on p.data\_sources = ly.code ) m

WHERE m.seq &gt;= 1 and ROWNUM &lt;=10 ; 对勾

 

select lower(p.personid) as personid, p.fingerrepeatno as fingerrepeatno

from gafis\_person p left join sys\_depart d on p.gather\_org\_code = d.code left join code\_ly ly on p.data\_sources = ly.code

WHERE p.seq &gt;= 1 and ROWNUM &lt;=10; ❌

 

不久后又增加功能开放端口，删除整个索引，用标签@RequestMapping("/delete")来实现，在类上和方法上分别加这个标签。参数增加：@RequestParam("indexName") String indexName

再访问，[http://127.0.0.1:8099/elastic/delete?indexName=sss](http://127.0.0.1:8099/elastic/delete?indexName=sss)

但要注意，标签内的字符串更改后不rebuild可能会认不出来。

 

～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～

 

由于访问接口后返回404页面，这一点需要修改。

参考资料[http://jinnianshilongnian.iteye.com/blog/1997192](http://jinnianshilongnian.iteye.com/blog/1997192)

将标签@Service改为@RestController

传参标签由@requestparam改为public String DeleteElastic(@PathVariable("deleteName") String deleteName) {配合

@RequestMapping("/delete/{deleteName}")使用

 

 @PathVariable和@RequestParam，分别是从路径里面去获取变量，也就是把路径当做变量，后者是从请求里面获取参数。 

 

/Springmvc/user/page.do?pageSize=3&pageNow=2 

 

pageSize和pageNow应该是属于参数而不是路径，所以应该添加@RequestParam的注解。 

 

如果做成如下URL，则可以使用@PathVariable 

 

/Springmvc/user/page/2/3.do 

 

～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～

 

同时还需要注意@Controller和@RestController的区别

@RestController=@[Controller+@Reponsebody](mailto:Controller+@Reponsebody)

只写@Controller会映射到页面，加@Responsebody会直接当作返回结构体来处理，不会对应到页面。

[http://blog.csdn.net/gg12365gg/article/details/51345601](http://blog.csdn.net/gg12365gg/article/details/51345601)

,下面[spring](http://lib.csdn.net/base/javaee)程序的代码片段可以直接return回数据，不然需要的是html格式的view。

\[java] view plain copy

 print?

@RestController  

public class HelloController {  

  

    @RequestMapping("/")  

    public String index() {  

        return "Greetings from Spring Boot!";  //由于是@RestController，因此直接返回数据，而不是view  

    }  

  

}  

 

@Service和@Controller区别

 

@Service用于标注业务层组件

@Controller用于标注控制层组件（如struts中的action）