update StoreDrugIndex set state=set b2c=o2o= where storeId= 查询结构是集合 转为Springboot ES处理
To update the StoreDrugIndex in Spring Boot using Elasticsearch, you can follow the steps below:
- Create a new Java class for the StoreDrugIndex entity. This class should have fields for state, b2c, o2o, and storeId.
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "store_drug_index")
public class StoreDrugIndex {
@Id
private String id;
private String state;
private boolean b2c;
private boolean o2o;
private String storeId;
// Getters and setters
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public boolean isB2c() {
return b2c;
}
public void setB2c(boolean b2c) {
this.b2c = b2c;
}
public boolean isO2o() {
return o2o;
}
public void setO2o(boolean o2o) {
this.o2o = o2o;
}
public String getStoreId() {
return storeId;
}
public void setStoreId(String storeId) {
this.storeId = storeId;
}
}
- Create a new repository interface that extends ElasticsearchRepository for the StoreDrugIndex entity.
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface StoreDrugIndexRepository extends ElasticsearchRepository<StoreDrugIndex, String> {
}
- Create a new service class for updating the StoreDrugIndex.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StoreDrugIndexService {
private final StoreDrugIndexRepository storeDrugIndexRepository;
@Autowired
public StoreDrugIndexService(StoreDrugIndexRepository storeDrugIndexRepository) {
this.storeDrugIndexRepository = storeDrugIndexRepository;
}
public StoreDrugIndex updateStoreDrugIndex(String id, String state, boolean b2c, boolean o2o, String storeId) {
StoreDrugIndex storeDrugIndex = new StoreDrugIndex();
storeDrugIndex.setId(id);
storeDrugIndex.setState(state);
storeDrugIndex.setB2c(b2c);
storeDrugIndex.setO2o(o2o);
storeDrugIndex.setStoreId(storeId);
return storeDrugIndexRepository.save(storeDrugIndex);
}
}
- Use the StoreDrugIndexService in your controller to update the StoreDrugIndex.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/store-drug-index")
public class StoreDrugIndexController {
private final StoreDrugIndexService storeDrugIndexService;
@Autowired
public StoreDrugIndexController(StoreDrugIndexService storeDrugIndexService) {
this.storeDrugIndexService = storeDrugIndexService;
}
@PutMapping("/{id}")
public StoreDrugIndex updateStoreDrugIndex(@PathVariable String id, @RequestBody StoreDrugIndex storeDrugIndex) {
return storeDrugIndexService.updateStoreDrugIndex(id, storeDrugIndex.getState(), storeDrugIndex.isB2c(), storeDrugIndex.isO2o(), storeDrugIndex.getStoreId());
}
}
With these changes, you can now update the StoreDrugIndex by making a PUT request to "/store-drug-index/{id}" with the updated StoreDrugIndex object in the request body.
原文地址: https://www.cveoy.top/t/topic/jfFD 著作权归作者所有。请勿转载和采集!