SpringBoot集成ElasticSearch对API的实际应用封装(七)

第一步.添加配置文件

在resources创建elasticsearch.yml配置文件并添加一下配置:

#elasticsearch集群名称,默认是elasticsearch
spring.data.elasticsearch.cluster.name=wali

#节点的地址,注意api模式下的端口号是9300,千万不要写成9200等
spring.date.elasticsearch.cluster-nodes=127.0.0.1:9300

#是否开启本地存储
spring.data.elasticsearch.repositories.enable=true

第二步.创建实体类并配置

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.util.Date;
/**
 * Created by ${ligh} on 2018/12/28 下午5:19
 */
 
@Document(indexName = "people",type = "person",shards = 5,replicas = 1,refreshInterval = "1")
public class People {

    @Id
    private String id;
    private String name;
    private String password;
    private Integer age;
//    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birth;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

第三步.创建Dao层,并设置为组件

import com.ligh.domain.People;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
/**
 * Created by ${ligh} on 2018/12/28 下午5:21
 */
 
@Component
public interface PeopleDao extends ElasticsearchRepository<People,String>{
}

第四步.创建结果返回状态和返回信息

创建返回信息类:

/**
 * Created by ${ligh} on 2018/12/28 下午4:26
 */
public class RespInfo {

    private Object content;
    private Integer status;
    private String message;

    public Object getContent() {
        return content;
    }
    public void setContent(Object content) {
        this.content = content;
    }
    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

创建返回状态类:

/**
 * Created by ${ligh} on 2018/12/28 下午4:28
 */
public class InfoCode {

    public static Integer SUCCESS=200;

    public static Integer ERROR = 500;

    //其他状态...
}

第五步.Controller类中进行接口调用

import com.alibaba.fastjson.JSON;
import com.ligh.dao.PeopleDao;
import com.ligh.domain.People;
import com.ligh.entity.InfoCode;
import com.ligh.entity.RespInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by ${ligh} on 2018/12/28 下午5:23
 */
@RequestMapping("people")
@RestController
public class PeopleController {

    @Autowired
    private PeopleDao peopleDao;

    /**
     *  添加方法也是修改方法
     *
     * @param people
     * @return
     */
    @RequestMapping("add")
    public String add(@RequestBody People people){
        RespInfo respInfo = new RespInfo();
        Date date = new Date();
        people.setBirth(date);
        People save = peopleDao.save(people);
        if(save != null){
            respInfo.setMessage("存储成功!");
            respInfo.setStatus(InfoCode.SUCCESS);
        }else {
            respInfo.setMessage("存储失败!");
            respInfo.setStatus(InfoCode.ERROR);
        }
        return JSON.toJSONString(respInfo);
    }

    /**
     *  删除方法
     *
     * @param people
     * @return
     */
    @RequestMapping("delete")
    public String delete(@RequestBody(required = false) People people){
        peopleDao.delete(people);
        return "success";
    }

    /**
     *  通过id查询
     *
     * @param people
     * @return
     */
    @RequestMapping("query")
    public String update(@RequestBody People people){
        RespInfo respInfo = new RespInfo();
        //查询全部
        Iterable<People> all = peopleDao.findAll();
        Map map = new ConcurrentHashMap();
        map.put("all",all);
        //根据指定参数进行查询
        Optional<People> list = peopleDao.findById(people.getId());
        if(list.isPresent()){
            respInfo.setStatus(InfoCode.SUCCESS);
            map.put("list",list);
            respInfo.setContent(map);
        }else {
            respInfo.setMessage("没有数据!");
            respInfo.setStatus(InfoCode.ERROR);
        }
        return JSON.toJSONString(respInfo);
    }
}

以上就完成了springboot实际开发中的使用。

猜你喜欢

转载自blog.csdn.net/ligh_sqh/article/details/85333738