Solr工具类
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 27 TbItemExample example=new TbItemExample(); 28 Criteria criteria = example.createCriteria(); 29 criteria.andStatusEqualTo("1"); 30 List<TbItem> list = itemMapper.selectByExample(example); 31 32 for(TbItem item:list){ 33 System.out.println(item.getTitle()); 34 } 35 System.out.println("开始导入数据...."); 36 37 solrTemplate.saveBeans(list); 38 solrTemplate.commit(); 39 System.out.println("导入数据完成...."); 40 } 41 42 public static void main(String[] args) { 43 44 ApplicationContext content=new ClassPathXmlApplicationContext("classpath*:spring/applicationContext*.xml"); 45 SolrUtil solrUtil= (SolrUtil) content.getBean("solrUtil"); 46 solrUtil.importData(); 47 48 } 49 50 }