
{"id":3052,"date":"2021-02-12T13:25:35","date_gmt":"2021-02-12T05:25:35","guid":{"rendered":"http:\/\/127.0.0.2\/?p=3052"},"modified":"2021-02-12T13:25:35","modified_gmt":"2021-02-12T05:25:35","slug":"2020java%e5%be%ae%e6%9c%8d%e5%8a%a1%e6%9e%b6%e6%9e%84%e4%b9%8bspringboot%e6%95%b4%e5%90%88mybatis%e5%92%8celasticsearch","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=3052","title":{"rendered":"2020java\u5fae\u670d\u52a1\u67b6\u6784\u4e4bSpringBoot\u6574\u5408mybatis\u548cElasticSearch"},"content":{"rendered":"<h2>SpringBoot\u6574\u5408mybatis\u548cElasticSearch<\/h2>\n<p><strong>1.\u521b\u5efa\u5ba2\u6237\u6a21\u5757\u548c\u641c\u7d22\u6a21\u5757<\/strong><\/p>\n<p><strong>2.\u51c6\u5907\u5ba2\u6237\u6a21\u5757\u7684\u9759\u6001\u8d44\u6e90(\u9875\u9762\uff0c\u5b9e\u4f53\u7c7b\uff0c\u6570\u636e\u5e93)<\/strong><\/p>\n<p><strong>3.\u51c6\u5907\u641c\u7d22\u6a21\u5757\u7684\u8d44\u6e90(\u5728ES\u4e2d\u521b\u5efa\u5ba2\u6237\u6a21\u5757\u7684\u7d22\u5f15)<\/strong><\/p>\n<p><strong>3.1 \u5bfc\u5165ES\u7684\u4f9d\u8d56<\/strong><\/p>\n<pre>&lt;dependency&gt;\r\n    &lt;groupId&gt;org.elasticsearch&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;elasticsearch&lt;\/artifactId&gt;\r\n    &lt;version&gt;6.5.4&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n&lt;dependency&gt;\r\n    &lt;groupId&gt;org.elasticsearch.client&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;elasticsearch-rest-high-level-client&lt;\/artifactId&gt;\r\n    &lt;version&gt;6.5.4&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p><strong>3.2 \u7f16\u5199\u8fde\u63a5ES\u7684config\u914d\u7f6e\u7c7b(\u641c\u7d22\u6a21\u5757)<\/strong><!--more--><\/p>\n<pre>elasticsearch:\r\n  port: 9200\r\n  host: 192.168.2.123<\/pre>\n<p><strong>3.2.1 \u7136\u540e\u521b\u5efaconfig\u5305<\/strong><\/p>\n<pre>@Configuration\r\npublic class ElasticSearchConfig {\r\n    @Value(\"${elasticsearch.host}\")\r\n    private String host;\r\n\r\n    @Value(\"${elasticsearch.port}\")\r\n    private Integer port;\r\n\r\n    @Bean\r\n    public RestHighLevelClient client(){\r\n        RestClientBuilder clientBuilder= RestClient.builder(new HttpHost(host,port));\r\n\r\n        \/\/ \u521b\u5efa RestHighLevelClient\r\n        return new RestHighLevelClient(clientBuilder);\r\n    }\r\n}<\/pre>\n<p><strong>3.3 \u5bfc\u5165\u5b9e\u4f53\u7c7b\u5e76\u5b9e\u73b0\u5e8f\u5217\u5316\u63a5\u53e3(\u5ba2\u6237\u6a21\u5757\u548c\u641c\u7d22\u6a21\u5757)<\/strong><\/p>\n<p><strong>3.4 \u521b\u5efa\u7d22\u5f15\u548c\u6d4b\u8bd5\u6570\u636e<\/strong><\/p>\n<pre>@SpringBootTest\r\nclass ElasticSearchTests{\r\n    @Autowired\r\n    private RestHighLevelClient client;\r\n\r\n    String index=\"mvc-users\";\r\n    String type=\"users\";\r\n\r\n    @Test\r\n    public void createIndex() throws Exception {\r\n        \/\/1. \u51c6\u5907\u7d22\u5f15\u7684 settings\r\n        Settings.Builder settings = Settings.builder().\r\n                put(\"number_of_replicas\", 1)\r\n                .put(\"number_of_shards\",3);\r\n        \/\/2. \u51c6\u5907\u7d22\u5f15\u7684\u7ed3\u6784 mappings\r\n        XContentBuilder mappings = JsonXContent.contentBuilder()\r\n                .startObject()\r\n                .startObject(\"properties\")\r\n                .startObject(\"username\")\r\n                .field(\"type\", \"text\")\r\n                .endObject()\r\n                .startObject(\"identity\")\r\n                .field(\"type\", \"text\")\r\n                .endObject()\r\n                .startObject(\"mobile\")\r\n                .field(\"type\", \"keyword\")\r\n                .endObject()\r\n                .endObject()\r\n                .endObject();\r\n        \/\/ 3. \u5c06 settings \u548c mappings \u5c01\u88c5\u5230\u4e00\u4e2aRequest\u5bf9\u8c61\r\n        CreateIndexRequest request = new CreateIndexRequest(index)\r\n                .settings(settings)\r\n                .mapping(type,mappings);\r\n        \/\/4. \u901a\u8fc7 client \u5bf9\u8c61\u53bb\u8fde\u63a5 ES \u5e76\u6267\u884c\u521b\u5efa\u7d22\u5f15\r\n        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);\r\n        \/\/ 5. \u8f93\u51fa\r\n        System.out.println(\"response:\"+response.toString());\r\n    }\r\n    @Test\r\n    public void bulkCreateDoc() throws Exception {\r\n        Users u1=new Users(1,\"admin\",\"admin\",\"340801\",\"\u7537\",\"133\");\r\n        Users u2=new Users(2,\"admin2\",\"admin2\",\"340802\",\"\u7537\",\"134\");\r\n        Users u3=new Users(3,\"admin3\",\"admin3\",\"340803\",\"\u5973\",\"135\");\r\n        Users u4=new Users(4,\"admin4\",\"admin4\",\"340804\",\"\u7537\",\"136\");\r\n        Users u5=new Users(5,\"admin5\",\"admin5\",\"340805\",\"\u5973\",\"137\");\r\n\r\n        ObjectMapper mapper = new ObjectMapper();\r\n\r\n        String json1 = mapper.writeValueAsString(u1);\r\n        String json2 = mapper.writeValueAsString(u2);\r\n        String json3 = mapper.writeValueAsString(u3);\r\n        String json4 = mapper.writeValueAsString(u4);\r\n        String json5 = mapper.writeValueAsString(u5);\r\n\r\n        \/\/2. \u521b\u5efaRequest\uff0c\u5c06\u51c6\u5907\u597d\u7684\u6570\u636e\u5c01\u88c5\u8fdb\u53bb\r\n        BulkRequest request = new BulkRequest();\r\n        request.add(new IndexRequest(index,type,u1.getId().toString()).source(json1, XContentType.JSON));\r\n        request.add(new IndexRequest(index,type,u2.getId().toString()).source(json2,XContentType.JSON));\r\n        request.add(new IndexRequest(index,type,u3.getId().toString()).source(json3,XContentType.JSON));\r\n        request.add(new IndexRequest(index,type,u4.getId().toString()).source(json4,XContentType.JSON));\r\n        request.add(new IndexRequest(index,type,u5.getId().toString()).source(json5,XContentType.JSON));\r\n\r\n        \/\/3. \u7528client\u6267\u884c\r\n        BulkResponse resp = client.bulk(request, RequestOptions.DEFAULT);\r\n\r\n        \/\/4. \u8f93\u51fa\u7ed3\u679c\r\n        System.out.println(resp.toString());\r\n    }\r\n}<\/pre>\n<p><strong>5.\u5c06\u5ba2\u6237\u6a21\u5757\u8fdb\u884c \u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u548cmybatis\u7684\u914d\u7f6e<\/strong><\/p>\n<pre># tomcat \u4fe1\u606f\r\nserver:\r\n  port: 80\r\n# \u8fde\u63a5\u6570\u636e\u5e93\u4fe1\u606f\r\nspring:\r\n  datasource:\r\n    url: jdbc:mysql:\/\/\/springboot?serverTimezone=UTC\r\n    driver-class-name: com.mysql.cj.jdbc.Driver\r\n    username: root\r\n    password: root\r\n    type: com.alibaba.druid.pool.DruidDataSource\r\n# mybatis\u7684\u914d\u7f6e\r\nmybatis:\r\n  mapper-locations: classpath:mapper\/*.xml\r\n  type-aliases-package: com.evshou.admin.demo.entity\r\n  configuration:\r\n    map-underscore-to-camel-case: true<\/pre>\n<p><strong>5.1 \u5728\u5ba2\u6237\u6a21\u5757\u521b\u5efamapper\u5305\u4ee5\u53ca\u7528\u6237\u6a21\u5757\u7684mapper\u63a5\u53e3<\/strong><\/p>\n<p><strong>5.2 \u5728\u542f\u52a8\u7c7b\u6dfb\u52a0@MapperScan(basePackages = &#8220;mapper\u5305&#8221;)<\/strong><\/p>\n<p><strong>5.3 \u5728resources\u76ee\u5f55\u521b\u5efamapper\u76ee\u5f55\u4ee5\u53ca\u6620\u5c04\u6587\u4ef6xml\uff0c\u5fc5\u987b\u4e0e\u63a5\u53e3\u540d\u4e00\u81f4<\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?&gt;\r\n&lt;!DOCTYPE mapper PUBLIC \"-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN\"\r\n        \"http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd\" &gt;\r\n&lt;mapper namespace=\"com.evshou.admin.openapi.usermedel.mapper.UsersMapper\"&gt;\r\n\r\n&lt;\/mapper&gt;<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2021\/02\/12\/1.png\" alt=\"\" width=\"680\" \/><\/p>\n<p><strong>6. \u4eceES\u4e2d\u67e5\u8be2\u5ba2\u6237\u4fe1\u606f\uff0c\u5e76\u901a\u8fc7\u7528\u6237\u6a21\u5757\u5c55\u793a\u5728\u9875\u9762\u4e2d<\/strong><\/p>\n<p><strong>6.1 \u5728\u641c\u7d22\u6a21\u5757\u521b\u5efaservice\u5305\u5e76\u521b\u5efaUsersService\u63a5\u53e3<\/strong><\/p>\n<pre>String searchUsersByQuery(Map&lt;String,Object&gt; param) throws IOException;<\/pre>\n<p><strong>6.2 \u521b\u5efaimpl\u5305\u5e76\u4e14\u5b9e\u73b0UsersService\u63a5\u53e3\uff0c\u7136\u540e\u6dfb\u52a0\u6ce8\u89e3 @Service<\/strong><\/p>\n<p><strong>6.2.1 \u521b\u5efa vo\u5305\u5e76\u4e14\u521b\u5efa LayUITableVO&lt;T&gt; \u5b9e\u4f53\u7c7b<\/strong><\/p>\n<pre>@Data\r\npublic class LayUITableVO&lt;T&gt; {\r\n    private Integer code=0;\r\n    private String msg=\"\";\r\n    private Long count;\r\n    private List&lt;T&gt; data;\r\n}<\/pre>\n<p><strong>6.2.2 \u5bfc\u5165\u4f9d\u8d56<\/strong><\/p>\n<pre>&lt;dependency&gt;\r\n    &lt;groupId&gt;commons-beanutils&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;commons-beanutils&lt;\/artifactId&gt;\r\n    &lt;version&gt;1.9.3&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<pre>&lt;dependency&gt;\r\n    &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;jackson-core&lt;\/artifactId&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p><strong>6.2.3 \u63a5\u53e3\u5b9e\u73b0\u7c7b<\/strong><\/p>\n<pre>@Service\r\npublic class UsersServiceImpl implements UsersService {\r\n    @Autowired\r\n    private RestHighLevelClient client;\r\n    String index=\"openapi_user\";\r\n    String type=\"user\";\r\n\r\n    @Override\r\n    public String searchUsersByQuery(Map&lt;String, Object&gt; param) throws IOException {\r\n        \/\/ 1. SearchRequest\r\n        SearchRequest request=new SearchRequest(index);\r\n        request.types(type);\r\n        \/\/ 2. \u5c01\u88c5\u6267\u884c\u6761\u4ef6\r\n        SearchSourceBuilder source=new SearchSourceBuilder();\r\n        Object name = param.get(\"name\");\r\n        if(!StringUtils.isEmpty(name)){\r\n            source.query(QueryBuilders.termQuery(\"username\",name));\r\n        }\r\n        Object state = param.get(\"state\");\r\n        if (state != null) {\r\n            source.query(QueryBuilders.termQuery(\"state\",state));\r\n        }\r\n        Integer page = (Integer) param.get(\"page\");\r\n        Integer limit = (Integer) param.get(\"limit\");\r\n        source.from((page-1)*limit);\r\n        source.size(limit);\r\n        request.source(source);\r\n        \/\/ 3. \u6267\u884c\u67e5\u8be2\r\n        SearchResponse response = client.search(request, RequestOptions.DEFAULT);\r\n        \/\/ 4. \u5c01\u88c5\u6570\u636e\r\n        LayUITableVO&lt;Users&gt; vo=new LayUITableVO&lt;&gt;();\r\n        vo.setCount(response.getHits().getTotalHits());\r\n        List&lt;Users&gt; data=new ArrayList&lt;&gt;();\r\n        for (SearchHit hit : response.getHits().getHits()) {\r\n            Users users=new Users();\r\n            try {\r\n                BeanUtils.populate(users,hit.getSourceAsMap());\r\n            } catch (Exception e) {\r\n                e.printStackTrace();\r\n            }\r\n            data.add(users);\r\n        }\r\n        vo.setData(data);\r\n        \/\/ 5. \u8fd4\u56de\u6570\u636e\r\n        return JSON.toJSON(vo);\r\n    }\r\n}<\/pre>\n<p><strong>6.2.4 \u5728\u63a5\u53e3\u65b9\u6cd5\u53f3\u952e goto test<\/strong><\/p>\n<pre>@SpringBootTest\r\nclass UsersServiceTest {\r\n    @Autowired\r\n    private UsersService usersService;\r\n\r\n    @Test\r\n    void searchUsersByQuery() throws IOException {\r\n        Map&lt;String,Object&gt; param=new HashMap&lt;&gt;();\r\n        param.put(\"page\",1);\r\n        param.put(\"limit\",10);\r\n        String s = usersService.searchUsersByQuery(param);\r\n        System.out.println(s);\r\n    }\r\n}<\/pre>\n<p><strong>6.2.5 \u521b\u5efacontroller\u5305\u5e76\u4e14\u521b\u5efaUsersController<\/strong><\/p>\n<pre>@RestController\r\n@RequestMapping(\"\/search\/users\")\r\npublic class UsersController {\r\n    @Autowired\r\n    private UsersService service;\r\n\r\n    @PostMapping(value = \"\/table\",produces = \"application\/json;charset=utf-8\")\r\n    public String table(@RequestBody Map&lt;String,Object&gt; param) throws Exception{\r\n        \/\/ \u8c03\u7528 service \u67e5\u8be2\u6570\u636e, \u76f4\u63a5\u8fd4\u56de\r\n        return service.searchUsersByQuery(param);\r\n    }\r\n}<\/pre>\n<p><strong>6.3 \u5ba2\u6237\u6a21\u5757<\/strong><\/p>\n<p><strong>6.3.1 \u521b\u5efaservice\u5305\u4ee5\u53ca\u63a5\u53e3<\/strong><\/p>\n<pre>public interface UsersService {\r\n    String findUsersByQuery(Map&lt;String,Object&gt; parameter);\r\n}<\/pre>\n<p><strong>6.3.2 \u521b\u5efaservice.impl\u5305\u4ee5\u53ca\u63a5\u53e3\u5b9e\u73b0\u7c7b<\/strong><\/p>\n<pre>@Service\r\npublic class UsersServiceImpl implements UsersService {\r\n    @Autowired\r\n    private RestTemplate restTemplate;\r\n    @Override\r\n    public String findUsersByQuery(Map&lt;String, Object&gt; parameter) {\r\n        \/\/ 1.\u51c6\u5907\u5934\u4fe1\u606f\u548c\u8bf7\u6c42\u53c2\u6570\r\n        String json = JSON.toJSON(parameter);\r\n        HttpHeaders httpHeaders=new HttpHeaders();\r\n        httpHeaders.setContentType(MediaType.parseMediaType(\"application\/json;charset=utf-8\"));\r\n        HttpEntity&lt;String&gt; entity=new HttpEntity&lt;&gt;(json,httpHeaders);\r\n        \/\/ 2. \u4f7f\u7528 RestTemplate \u8c03\u7528\u641c\u7d22\u6a21\u5757\r\n        return restTemplate.postForObject(\"http:\/\/localhost:8080\/search\/users\/table\", entity, String.class);\r\n    }\r\n}<\/pre>\n<p><strong>6.3.3 \u521b\u5efaconfig\u5305\u5e76\u4e14\u521b\u5efa RestTemplateConfig \u7c7b<\/strong><\/p>\n<pre>@Configuration\r\npublic class RestTemplateConfig {\r\n\r\n    @Bean\r\n    public RestTemplate restTemplate(){\r\n        return new RestTemplate();\r\n    }\r\n}<\/pre>\n<p><strong>6.3.4 \u5c06\u641c\u7d22\u6a21\u5757\u7684utils\u590d\u5236\u5230\u5ba2\u6237\u6a21\u5757\u4e2d<\/strong><\/p>\n<p><strong>6.3.5 \u5728\u63a5\u53e3\u53f3\u952e goto test<\/strong><\/p>\n<pre>@SpringBootTest\r\nclass UsersServiceTest {\r\n    @Autowired\r\n    private UsersService service;\r\n\r\n    @Test\r\n    void findUsersByQuery() {\r\n        Map&lt;String,Object&gt; parameter=new HashMap&lt;&gt;();\r\n        parameter.put(\"page\",1);\r\n        parameter.put(\"limit\",10);\r\n        parameter.put(\"name\",\"admin\");\r\n        String result = service.findUsersByQuery(parameter);\r\n        System.out.println(result);\r\n    }\r\n}<\/pre>\n<p><strong>6.4 \u5728 \u5ba2\u6237\u6a21\u5757 \u521b\u5efa controller\u5305\u5e76\u4e14\u521b\u5efaUsersController \u7c7b<\/strong><\/p>\n<pre>@RestController\r\n@RequestMapping(\"\/search\/users\")\r\npublic class UsersController {\r\n    @Autowired\r\n    private UsersService service;\r\n    @GetMapping(value = \"\/table\",produces = \"application\/json;charset=utf-8\")\r\n    public String table(@RequestParam(defaultValue = \"1\") Integer page,\r\n                        @RequestParam(defaultValue = \"10\") Integer limit,\r\n                        String name,Integer state){\r\n        \/\/ 1. \u5c01\u88c5\u53c2\u6570\r\n        Map&lt;String,Object&gt; parameter=new HashMap&lt;&gt;();\r\n        parameter.put(\"page\",page);\r\n        parameter.put(\"limit\",limit);\r\n        parameter.put(\"name\",name);\r\n        parameter.put(\"state\",state);\r\n        \/\/ 2. \u8c03\u7528 service \u67e5\u8be2\u6570\u636e\uff0c\u7136\u540e\u8fd4\u56de\u6570\u636e\r\n        return service.findUsersByQuery(parameter);\r\n    }\r\n}<\/pre>\n<p><strong>6.5 \u641c\u7d22\u6a21\u5757\u8fdb\u884c\u6dfb\u52a0\u7528\u6237\u4fe1\u606f<\/strong><\/p>\n<p><strong>6.5.1 \u5728\u641c\u7d22\u6a21\u5757\u7684service\u63a5\u53e3\u6dfb\u52a0 \u6dfb\u52a0\u7528\u6237\u65b9\u6cd5<\/strong><\/p>\n<pre>void addUsers(Users users) throws IOException;<\/pre>\n<p><strong>6.5.2 \u5728\u641c\u7d22\u6a21\u5757\u7684service\u5b9e\u73b0\u7c7b<\/strong><\/p>\n<p>\u5728\u7c7b\u7684\u4e0a\u65b9\u6dfb\u52a0\u6ce8\u89e3 @Slf4j<\/p>\n<pre>@Override\r\npublic void addUsers(Users users) throws IOException {\r\n    \/\/ 1. \u521b\u5efa indexRequest\r\n    IndexRequest request=new IndexRequest(index,type,users.getId()+\"\");\r\n    \/\/ 2. \u5c01\u88c5\u6570\u636e\r\n    request.source(JSON.toJSON(users), XContentType.JSON);\r\n    \/\/ 3. \u6267\u884c\u6dfb\u52a0\r\n    IndexResponse response = client.index(request, RequestOptions.DEFAULT);\r\n    \/\/ 4. \u5224\u65ad\u6dfb\u52a0\u662f\u5426\u6210\u529f(\u5931\u8d25\uff0c\u629b\u51fa\u5f02\u5e38)\r\n    if(!\"CREATED\".equalsIgnoreCase(response.getResult().toString())){\r\n        log.error(\"[\u5411ES\u6dfb\u52a0\u7528\u6237\u4fe1\u606f\u5931\u8d25] index={},type={},users={}\",index,type,users);\r\n        throw new RuntimeException(\"[\u5411ES\u6dfb\u52a0\u7528\u6237\u4fe1\u606f\u5931\u8d25]\");\r\n    }\r\n}<\/pre>\n<p><strong>6.5.3 \u5728\u6d4b\u8bd5\u7c7b\u6dfb\u52a0\u65b9\u6cd5<\/strong><\/p>\n<pre>@Test\r\nvoid addUsers() throws IOException {\r\n    Users users=new Users(6,\"admin6\",\"admin6\",\"166\",\"166\",\"62220166\",996.6,1);\r\n    service.addUsers(users);\r\n}<\/pre>\n<p>6.5.4 \u5728\u641c\u7d22\u6a21\u5757\u7684controller\u7c7b\u6dfb\u52a0<\/p>\n<pre>@PostMapping(value = \"\/add\")\r\npublic void add(@RequestBody Users users) throws IOException {\r\n    service.addUsers(users);\r\n}<\/pre>\n<p><strong>6.6 \u7528\u6237\u6a21\u5757\u6a21\u5757\u8fdb\u884c\u6dfb\u52a0\u7528\u6237\u4fe1\u606f<\/strong><\/p>\n<p><strong>6.6.1 \u7528\u6237\u6a21\u5757service\u63a5\u53e3<\/strong><\/p>\n<pre>void add(Users users);<\/pre>\n<p><strong>6.6.2 \u7528\u6237\u6a21\u5757service\u5b9e\u73b0\u7c7b<\/strong><\/p>\n<pre>@Service\r\n@Slf4j\r\npublic class UsersServiceImpl implements UsersService {\r\n    @Autowired\r\n    private UsersMapper mapper;\r\n    @Override\r\n    <strong>@Transactional<\/strong>\r\n    public void add(Users users) {\r\n        \/\/ 1. \u8c03\u7528 mapper \u6dfb\u52a0\u6570\u636e\u5230 mysql\r\n        Integer add = mapper.add(users);\r\n        \/\/ 2. \u5224\u65ad\u6dfb\u52a0\u662f\u5426\u6210\u529f\r\n        if(add!=1){\r\n            log.error(\"[\u6dfb\u52a0\u7528\u6237\u4fe1\u606f\u5931\u8d25] users={}\",users);\r\n            throw new RuntimeException(\"[\u6dfb\u52a0\u7528\u6237\u4fe1\u606f\u5931\u8d25]\");\r\n        }\r\n        \/\/ 3. \u8c03\u7528\u641c\u7d22\u6a21\u5757\uff0c\u6dfb\u52a0\u6570\u636e\u5230 ES\r\n        \/\/ 1.\u51c6\u5907\u5934\u4fe1\u606f\u548c\u8bf7\u6c42\u53c2\u6570\r\n        String json = JSON.toJSON(users);\r\n        HttpHeaders httpHeaders=new HttpHeaders();\r\n        httpHeaders.setContentType(MediaType.parseMediaType(\"application\/json;charset=utf-8\"));\r\n        HttpEntity&lt;String&gt; entity=new HttpEntity&lt;&gt;(json,httpHeaders);\r\n        \/\/ 2. \u4f7f\u7528 RestTemplate \u8c03\u7528\u641c\u7d22\u6a21\u5757\r\n        restTemplate.postForObject(\"http:\/\/localhost:8080\/search\/users\/add\", entity, String.class);\r\n    }\r\n}<\/pre>\n<p><strong>6.6.3 \u7528\u6237\u6a21\u5757mapper<\/strong><\/p>\n<pre>Integer add(Users users);<\/pre>\n<p><strong>6.6.4 \u7528\u6237\u6a21\u5757mapper\u6620\u5c04<\/strong><\/p>\n<pre>&lt;insert id=\"add\" parameterType=\"Users\" useGeneratedKeys=\"true\" keyProperty=\"id\"&gt;\r\n    insert into users values(null,#{username},#{password},#{mobile},#{identity},#{account},#{money},#{state})\r\n&lt;\/insert&gt;<\/pre>\n<p><strong>6.6.5 \u7528\u6237\u6a21\u5757mapper\u6d4b\u8bd5\u7c7b<\/strong><\/p>\n<pre>@SpringBootTest\r\nclass UsersMapperTest {\r\n    @Autowired\r\n    private UsersMapper mapper;\r\n\r\n    @Test\r\n    void add() {\r\n        Users users=new Users(null,\"admin12\",\"admin12\",\"1712\",\"1712\",\"62221712\",121212.12,1);\r\n        Integer add = mapper.add(users);\r\n        System.out.println(add+\"\\t\"+users);\r\n    }\r\n}<\/pre>\n<p><strong>6.6.6 \u7528\u6237\u6a21\u5757\u7684service\u6d4b\u8bd5<\/strong><\/p>\n<pre>@Test\r\nvoid add(){\r\n    Users users=new Users(null,\"admin16\",\"admin16\",\"1716\",\"1716\",\"62221716\",16.16,1);\r\n    service.add(users);\r\n}<\/pre>\n<p><strong>6.7 \u7528\u6237\u6a21\u5757\u7684controller<\/strong><\/p>\n<p><strong>6.7.1 \u521b\u5efa vo \u5305\u5e76\u4e14\u521b\u5efa ResultVO \u7c7b<\/strong><\/p>\n<pre>@Data\r\n@AllArgsConstructor\r\n@NoArgsConstructor\r\npublic class ResultVO {\r\n    private Boolean status;\r\n    private String message;\r\n    private Object result;\r\n    public ResultVO(Boolean status, String message) {\r\n        this.status = status;\r\n        this.message = message;\r\n    }\r\n}<\/pre>\n<p><strong>6.7.2 controller<\/strong><\/p>\n<pre>@PostMapping(\"\/add\")\r\npublic ResultVO add(Users users){\r\n    try {\r\n        \/\/ 1. \u8c03\u7528 service \u6267\u884c\u6dfb\u52a0\r\n        service.add(users);\r\n        \/\/ 2. \u8fd4\u56dejson\r\n        return new ResultVO(true,\"\u6dfb\u52a0\u6210\u529f\");\r\n    } catch (RuntimeException e) {\r\n        e.printStackTrace();\r\n        return new ResultVO(false,e.getMessage());\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SpringBoot\u6574\u5408mybatis\u548cElasticSearch 1.\u521b\u5efa\u5ba2\u6237\u6a21\u5757\u548c\u641c\u7d22\u6a21\u5757 2.\u51c6\u5907\u5ba2\u6237\u6a21 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-3052","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/3052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3052"}],"version-history":[{"count":11,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/3052\/revisions"}],"predecessor-version":[{"id":3064,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/3052\/revisions\/3064"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}