1 import java.util.List;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.context.ApplicationContext;
5 import org.springframework.context.support.ClassPathXmlApplicationContext;
6 import org.springframework.data.solr.core.SolrTemplate;
7 import org.springframework.stereotype.Component;
8
9 import com.pinyougou.mapper.TbItemMapper;
10 import com.pinyougou.pojo.TbItem;
11 import com.pinyougou.pojo.TbItemExample;
12 import com.pinyougou.pojo.TbItemExample.Criteria;
13
14 @Component
15 public class SolrUtil {
16
17 @Autowired
18 private TbItemMapper itemMapper;
19
20 @Autowired
21 private SolrTemplate solrTemplate;
22
23 //导入数据方法
24 public void importData(){
25 //查询数据
26 TbItemExample example=new TbItemExample();
27 Criteria criteria = example.createCriteria();
28 criteria.andStatusEqualTo("1");
29 List<TbItem> list = itemMapper.selectByExample(example);
30
31 for(TbItem item:list){
32 System.out.println(item.getTitle());
33 }
34 System.out.println("开始导入数据....");
35
36 solrTemplate.saveBeans(list);
37 solrTemplate.commit();
38 System.out.println("导入数据完成....");
39 }
40
41 public static void main(String[] args) {
42
43 ApplicationContext content=new ClassPathXmlApplicationContext("classpath*:spring/applicationContext*.xml");
44 SolrUtil solrUtil= (SolrUtil) content.getBean("solrUtil");
45 solrUtil.importData();
46
47 }
48
49 }