数据库数据一键上传到solr索引库工具类
@Component
public class SolrUtil {
@Autowired
private TbItemMapper itemMapper;
@Autowired
private SolrTemplate solrTemplate;
public void importItemList(){
TbItemExample example = new TbItemExample();
TbItemExample.Criteria criteria = example.createCriteria();
criteria.andStatusEqualTo("1");//审核通过的商品才能存入solr索引库
List<TbItem> itemList = itemMapper.selectByExample(example);
System.out.println("--商品列表--");
for (TbItem item : itemList) {
System.out.println(item.getId()+"::"+
item.getTitle()+"::"+item.getBrand()+"::"+
item.getPrice()+"::"+item.getCategory());
Map specMap = JSON.parseObject(item.getSpec(), Map.class);
item.setSpecMap(specMap);
}
System.out.println("--结束--");
solrTemplate.saveBeans(itemList);
solrTemplate.commit();
}
public static void main(String[] args) {
//不仅要加载本项目下的配置文件, 还要加载添加的jar summer-dao下面的配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext
("classpath*:spring/applicationContext*.xml");
SolrUtil solrUtil = context.getBean("solrUtil", SolrUtil.class);
solrUtil.importItemList();
}
}