1.什么是es
是开源高可扩展的分布式的全文检索引擎,可以近乎实时存储和检索数据
特性:
2.es的原理
根据内容进行分词,然后建立反向(倒排)索引,根据倒排索引进行搜索,速度快.
3.和mysql数据库相比
关系型数据库(mysql) | 非关系型数据库ES |
---|---|
数据库Database | 索引Index |
表table | 类型type |
数据列column | 字段field |
约束schema | 映射mapping |
1.去官网Elasticsearch压缩包,解压缩包,进入bin,双击elasticsearch.bat运行.。
修改config/elasticsearch.yml,增加2句话
http.cors.enabled: true
http.cors.allow-origin: "*"
2.在springboot项目pom文件中引入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
3.在接口数据到层中继承接口 ElasticsearchRepository,自动关系映射
4.controller控制层,dao层继承ElasticsearchRepository接口后,可直接调用其自带的一些数据访问方法,实现增删改查。
import com.tjetc.dao.EmployeeDao;
import com.tjetc.domain.Employee;
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
@GetMapping("/add")
public String add(){
return "add";
}
@PostMapping("/add")
public String add(Employee employee, MultipartFile photo){
if (photo!=null && photo.getSize()>0){
File file = new File("C:/upload/");
if (!file.exists()){
file.mkdir();
}
String filename = photo.getOriginalFilename();
File file1 = new File(file, filename);
try {
photo.transferTo(file1);
employee.setPhotopath("upload/"+filename);
} catch (IOException e) {
e.printStackTrace();
}
}
employeeDao.save(employee);
System.out.println("添加成功");
return "redirect:/employee/list";
}
@RequestMapping("/list")
private String listByName(@RequestParam(defaultValue = "")String name,
@RequestParam(defaultValue = "0")Integer pageNum,
@RequestParam(defaultValue = "3")Integer pageSize, Model model){
Pageable pageable= PageRequest.of(pageNum,pageSize);
List<Employee> list1 = employeeDao.findByName(name, pageable);
System.out.println("list哈哈哈啊:"+list1);
Page<Employee> page = employeeDao.findAll(pageable);
List<Employee> list = page.getContent();
System.out.println("page的个数:"+page.getTotalPages());
System.out.println("getNumber:"+page.getNumber());
System.out.println("getSize:"+page.getSize());
System.out.println("getNumberOfElements:"+page.getNumberOfElements());
Iterable<Employee> all = employeeDao.findAll();
/*List<Employee> list = (List)employeeDao.findAll();*/
for (Employee employee : list) {
System.out.println("employee = " + employee);
}
/* List<Product> list = productDao.findByProduct(name, pageable);
System.out.println("list的值:"+list);*/
System.out.println("page = " + page);
model.addAttribute("list",list);
model.addAttribute("page",page);
model.addAttribute("name",name);
return "list";
}
@RequestMapping("/findById")
public String findById(Integer id,Model model){
Employee employee = employeeDao.findById(id).get();
System.out.println("employee = " + employee);
model.addAttribute("e",employee);
return "update";
}
@PostMapping("/update")
public String update(Employee employee, MultipartFile photo){
if (photo!=null && photo.getSize()>0){
File file = new File("C:/upload/");
if (!file.exists()){
file.mkdir();
}
String filename = photo.getOriginalFilename();
File file1 = new File(file, filename);
try {
photo.transferTo(file1);
employee.setPhotopath("upload/"+filename);
} catch (IOException e) {
e.printStackTrace();
}
}
employeeDao.save(employee);
System.out.println("修改成功");
return "redirect:/employee/list";
}
@RequestMapping("/del")
public String del(Integer id){
employeeDao.deleteById(id);
System.out.println("删除成功");
return "redirect:/employee/list";
}
}
5.dao数据层
import com.tjetc.domain.Employee;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface EmployeeDao extends ElasticsearchRepository<Employee,Integer> {
Page<Employee> findAll(Pageable pageable);
List<Employee> findByName(String name, Pageable pageable);
}
6.domain实体类对象属性定义格式
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "employee")
public class Employee {
@Id
@Field(type = FieldType.Integer,store = true,index = false)
private Integer id;
@Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_max_word")
private String name;
@Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_max_word")
private Double price;
@Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_max_word")
private String photopath;
public Employee(Integer id, String name, Double price, String photopath) {
this.id = id;
this.name = name;
this.price = price;
this.photopath = photopath;
}
public Employee() {
}