1.导入数据到索引库
导入的数据中包含了三个表的内容:
1.1 sql语句
SELECT
a.id,
a.title,
a.sell_point,
a.price,
a.image,
b.`name` category_name,
c.item_desc
FROM
tb_item a
LEFT JOIN tb_item_cat b ON a.cid = b.id
LEFT JOIN tb_item_desc c ON a.id = c.item_id
1.2 dao层(因为是多表查询,所以这里不能使用mybatis逆向工程中的mapper)
需要创建一个mapper接口+mapper映射文件。名称相同且在同一目录下。
itemMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.jingxi.search.mapper.ItemMapper" > <select id="getItemList" resultType="com.jingxi.search.pojo.Item"> SELECT a.id, a.title, a.sell_point, a.price, a.image, b. NAME category_name FROM tb_item a LEFT JOIN tb_item_cat b ON a.cid = b.id </select> </mapper>
1.3 service层
package com.jingxi.search.service; import java.util.List; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.common.SolrInputDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.jingxi.common.pojo.JingXiResult; import com.jingxi.common.util.ExceptionUtil; import com.jingxi.search.mapper.ItemMapper; import com.jingxi.search.pojo.Item; @Service public class ItemServiceImpl implements ItemService { @Autowired private ItemMapper itemMapper; @Autowired private SolrServer solrServer; @Override public JingXiResult importAllItems() { try { //查询商品列表 List<Item> list = itemMapper.getItemList(); //把商品信息写入索引库 for (Item item : list) { //创建一个SolrInputDocument对象 SolrInputDocument document = new SolrInputDocument(); document.setField("id", item.getId()); document.setField("item_title", item.getTitle()); document.setField("item_sell_point", item.getSell_point()); document.setField("item_price", item.getPrice()); document.setField("item_image", item.getImage()); document.setField("item_category_name", item.getCategory_name()); document.setField("item_desc", item.getItem_des()); //写入索引库 solrServer.add(document); } //提交修改 solrServer.commit(); } catch (Exception e) { e.printStackTrace(); return JingXiResult.build(500, ExceptionUtil.getStackTrace(e)); } return JingXiResult.ok(); } }
1.4 controller层
package com.jingxi.search.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.jingxi.common.pojo.JingXiResult; import com.jingxi.search.service.ItemService; @Controller public class ItemController { @Autowired private ItemService itemService; @RequestMapping("/manager/importall") @ResponseBody public JingXiResult imporeAllitem(){ JingXiResult result=itemService.importAllItems(); return result; } }
1.5 item实体类
package com.jingxi.search.pojo; public class Item { private String id; private String title; private String sell_point; private long price; private String image; private String category_name; private String item_des; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSell_point() { return sell_point; } public void setSell_point(String sell_point) { this.sell_point = sell_point; } public long getPrice() { return price; } public void setPrice(long price) { this.price = price; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getCategory_name() { return category_name; } public void setCategory_name(String category_name) { this.category_name = category_name; } public String getItem_des() { return item_des; } public void setItem_des(String item_des) { this.item_des = item_des; } }
至此,导入数据到索引库的服务完成~
2.在jingxi-backend中调用jingxi-reach中的服务
效果展示:
调用服务的链接:
service层:
controller层:
jsp页面:
import-index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <div> <a class="easyui-linkbutton" onclick="importIndex()">导入数据到索引库</a> </div> <script type="text/javascript"> function importIndex(){ $.post("index/import",null,function(data){ if(data.status==200){ $.messager.alert('提示','导入数据到索引库成功'); }else{ $.messager.alert('提示','导入数据到索引库失败'); } }); } </script>