springboot整合mybatis加elasticsearch实例

elasticsearch简单运用

创建一个springboot工程,引入依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

版本根据自己的实际情况引入
application.yml文件配置如下:

spring:
  datasource:
    #driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: 123456
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 127.0.0.1:9300
mybatis:
  type-aliases-package: com.lsl.elasticsearch.pojo

启动类上加扫描的dao包路径
在这里插入图片描述
以上工程创建完毕

创建Sku对象

public class Sku {
    private Long id;
    private Long spuId;
    private String title;
    private String images;
    private Long price;
    private String indexes;
    private String ownSpec;
    private Boolean enable;
    private Date createTime;
    private Date lastUpdateTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getSpuId() {
        return spuId;
    }

    public void setSpuId(Long spuId) {
        this.spuId = spuId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }

    public Long getPrice() {
        return price;
    }

    public void setPrice(Long price) {
        this.price = price;
    }

    public String getIndexes() {
        return indexes;
    }

    public void setIndexes(String indexes) {
        this.indexes = indexes;
    }

    public String getOwnSpec() {
        return ownSpec;
    }

    public void setOwnSpec(String ownSpec) {
        this.ownSpec = ownSpec;
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    @Override
    public String toString() {
        return "Sku{" +
                "id=" + id +
                ", spuId=" + spuId +
                ", title='" + title + '\'' +
                ", images='" + images + '\'' +
                ", price=" + price +
                ", indexes='" + indexes + '\'' +
                ", ownSpec='" + ownSpec + '\'' +
                ", enable=" + enable +
                ", createTime=" + createTime +
                ", lastUpdateTime=" + lastUpdateTime +
                '}';
    }
}

创建SkuService编写查询所有sku的方法

 @Autowired
    private SkuDao skuDao;
    public List<Sku> queryAllSku(){
        List<Sku> goodsList= skuDao.queryAllSku();
        return goodsList;
    }

创建SkuDao

  @Select("select * from goods")
    List<Sku> queryAllSku();

创建Goods对象,这个对象用来创建索引

@Document(indexName = "test",type = "goods",shards = 1,replicas = 0)
public class Goods {
    @Id
    private Long id;
    private Long spuId;
    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String title;
    @Field(type = FieldType.Keyword,index = false)
    private String images;
    private Long price;
    @Field(type = FieldType.Keyword)
    private String indexes;
    @Field(type = FieldType.Keyword)
    private String ownSpec;
    private Boolean enable;
    private Date createTime;
    private Date lastUpdateTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getSpuId() {
        return spuId;
    }

    public void setSpuId(Long spuId) {
        this.spuId = spuId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }

    public Long getPrice() {
        return price;
    }

    public void setPrice(Long price) {
        this.price = price;
    }

    public String getIndexes() {
        return indexes;
    }

    public void setIndexes(String indexes) {
        this.indexes = indexes;
    }

    public String getOwnSpec() {
        return ownSpec;
    }

    public void setOwnSpec(String ownSpec) {
        this.ownSpec = ownSpec;
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", spuId=" + spuId +
                ", title='" + title + '\'' +
                ", images='" + images + '\'' +
                ", price=" + price +
                ", indexes='" + indexes + '\'' +
                ", ownSpec='" + ownSpec + '\'' +
                ", enable=" + enable +
                ", createTime=" + createTime +
                ", lastUpdateTime=" + lastUpdateTime +
                '}';
    }
}

创建GoodsDao

public interface GoodsDao extends ElasticsearchRepository<Goods,Integer> {
}

只需继承ElasticsearchRepository类即可,第一个参数是哪一个对象,第二个参数是id的类型

在SkuService中添加buildGoods方法,将Sku对象转换成Goods对象

public Goods buildGoods(Sku sku){
        Goods goods=new Goods();
        goods.setId(sku.getId());
        goods.setSpuId(sku.getSpuId());
        goods.setTitle(sku.getTitle());
        goods.setPrice(sku.getPrice());
        goods.setOwnSpec(sku.getOwnSpec());
        goods.setImages(sku.getImages());
        goods.setEnable(sku.getEnable());
        goods.setIndexes(sku.getIndexes());
        goods.setCreateTime(sku.getCreateTime());
        goods.setLastUpdateTime(sku.getLastUpdateTime());
        return goods;
    }

在测试类中编写方法将所有数据导入索引库

@Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private SkuService skuService;
    @Autowired
    private GoodsDao goodsDao;
    @Test
    public void contextLoads() {
        elasticsearchTemplate.createIndex(Goods.class);
        elasticsearchTemplate.putMapping(Goods.class);
        List<Sku> skus = skuService.queryAllSku();
        List<Goods> goodsList=skus.stream().map(sku -> {
            try {
                return skuService.buildGoods((Sku)sku);
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }).collect(Collectors.toList());
        goodsDao.saveAll(goodsList);
    }

查看索引库中的数据
在这里插入图片描述
我们发现在索引名称为test,type为goods中已经导入了我们在数据库中的sku数据

编写查询方法

    @Test
    public void matchQuery(){
        int page=0;
        int size=5;
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        queryBuilder.withQuery(QueryBuilders.matchQuery("title","小米华为vivo"));
        queryBuilder.withPageable(PageRequest.of(page,size));
        queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
        Page<Goods> goods = goodsDao.search(queryBuilder.build());
        for (Goods good : goods) {
            System.out.println(good);
        }
        System.out.println(goods.getTotalElements());
    }

在这里插入图片描述
只要title中包含小米或者华为或者vivo的都会被查出来,因为我们设置了title字段分词,这里显示5条因为分页每页5条数据
各位在玩的时候可以自己编写简单的类测试,不用把类的字段写的这么多

评论 4 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页

打赏作者

阿斯兰z

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值