`
asia007
  • 浏览: 17108 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
Lucene4.6创建索引和查询 lucene lucene4.6索引创建和搜索例子
package com.lucene.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * lucene简单实例
 * @author songhui
 * @date 2014年1月14日
 */
public class LuceneIndex {
	
	//索引的文件的存放路径 测试时可以在本目录下自行建一些文档,内容自行编辑即可  
	public final static String INDEX_FILE_PATH = "E:\\lucene\\test"; 
	//索引的存放位置
    public final static String INDEX_STORE_PATH = "E:\\lucene\\index"; 
    
    /**
     * 创建索引
     * @author songhui
     * @date 2014年1月14日
     * @param analyzer
     * @throws Exception
     */
    public static void createIndex(Analyzer analyzer) throws Exception{  
    	
    	//通过Directory的创建指定索引存放位置
        Directory dire=FSDirectory.open(new File(INDEX_STORE_PATH));  
        //通过IndexWriterConfig的创建指定索引版本和语言词汇分析器
        IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_46, analyzer); 
        //创建IndexWriter,它的作用是用来写索引文件
        IndexWriter iw=new IndexWriter(dire, iwc);  
        LuceneIndex.addDoc(iw);  
        iw.close();  
    }  
    
    /**
     * 动态添加Document  
     * @author songhui
     * @date 2014年1月14日
     * @param iw
     * @throws Exception
     */
    public static void addDoc(IndexWriter iw)  throws Exception{  
    	
        File[] files=new File(INDEX_FILE_PATH).listFiles(); 
        Document doc=null;
        for (File file : files) {  
        	//通过创建Document指定要索引的文档
            doc=new Document();  
            String content=LuceneIndex.getContent(file);  
            String name=file.getName();  
            String path=file.getAbsolutePath();  
            //向Document文档中添加Field信息,不同类型的信息用不同类型的Field来表示
            doc.add(new TextField("content", content, Store.YES));  
            doc.add(new TextField("name", name, Store.YES));  
            doc.add(new TextField("path", path,Store.YES));  
            System.out.println(name+"==="+content+"==="+path);  
            //将Document添加到IndexWriter中并且提交
            iw.addDocument(doc);  
            iw.commit();  
        }  
    }  
    
   /**
    * 获取文本内容
    * @author songhui
    * @date 2014年1月14日
    * @param file
    * @return
    * @throws Exception
    */
    @SuppressWarnings("resource")  
    public static String getContent(File file) throws Exception{
    	
        FileInputStream fis=new FileInputStream(file);  
        InputStreamReader isr=new InputStreamReader(fis);  
        BufferedReader br=new BufferedReader(isr);  
        StringBuffer sb=new StringBuffer();  
        String line=null;  
        while((line=br.readLine())!=null){  
            sb.append(line+"\n");
        }  
        return sb.toString();  
    }  
    
    /**
     * 
     * @author songhui
     * @date 2014年1月14日
     * @param query
     * @throws Exception
     */
    private static void search(Query query) throws Exception {  
    	
    	//通过该方法指定搜索目录
        Directory dire=FSDirectory.open(new File(INDEX_STORE_PATH));  
        //创建IndexReader将搜索目录读取到内存
        IndexReader ir=DirectoryReader.open(dire); 
        //创建IndexSearcher准备搜索
        IndexSearcher is=new IndexSearcher(ir); 
        //获取搜索结果
        TopDocs td=is.search(query, 1000);  
        System.out.println("共为您查找到"+td.totalHits+"条结果");  
        ScoreDoc[] sds =td.scoreDocs;  
        for (ScoreDoc sd : sds) {   
            Document d = is.doc(sd.doc);   
            System.out.println(d.get("path") + ":["+d.get("path")+"]");   
        }  
    }  
	
	public static void main(String[] args) throws Exception {
		
		//通过Analyzer 的创建指定索引语言词汇的分析器
		Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);  
        LuceneIndex.createIndex(analyzer);  
        QueryParser parser = new QueryParser(Version.LUCENE_46, "content", analyzer);   
        Query query = parser.parse("人民");  
        LuceneIndex.search(query);  
	}
}
MyBatis获取插入记录的自增长字段值 mytatis MyBatis获取插入记录的自增长字段值
MyBatis获取插入记录的自增长字段值
分类: MySQL J2EE Java Spring 2011-09-19 15:38 3878人阅读 评论(2) 收藏 举报
insertgetterjava
第一步:

    在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名!

[html] view plaincopy
<insert id="insert" parameterType="Spares"   
        useGeneratedKeys="true" keyProperty="id">  
        insert into spares(spares_id,spares_name,  
            spares_type_id,spares_spec)  
        values(#{id},#{name},#{typeId},#{spec})  
    </insert>  
第二步:

    Mybatis执行完插入语句后,自动将自增长值赋值给对象Spares的属性id。因此,可通过Spares对应的getter方法获取!

[java] view plaincopy
/** 
 * 新增备件 
 * @author hellostory 
 * @param spares 
 * @return 
 */  
@RequestMapping(value = "/insert")  
@ResponseBody  
public JsonResponse insert(Spares spares) {  
    int count = sparesService.insert(spares);  
    System.out.println("共插入" + count + "条记录!"  
            + "\n刚刚插入记录的主键自增长值为:" + spares.getId());  
JS 实现 腾讯、新浪、网易、搜狐微博,开心网,人人网转帖分享功能 js JS 实现 腾讯、新浪、网易、搜狐微博,开心网,人人网转帖分享功能
update
JS 实现 腾讯、新浪、网易、搜狐微博,开心网,人人网转帖分享功能
其中官方提供的代码如下:
分享到SNS一键分享按钮代码介绍:
·腾讯微博:http://open.t.qq.com/apps/share/explain.php
·新浪微博:http://open.weibo.com/sharebutton
·开心网:http://www.kaixin001.com/platform/?app=repaste
·人人网:http://widget.renren.com/?widget=freeshare
·搜狐微博:http://open.t.sohu.com/en/%E4%B8%80%E9%94%AE%E5%88%86%E4%BA%AB%E5%88%B0%E6%90%9C%E7%8B%90%E5%BE%AE%E5%8D%9A
·网易微博:http://open.t.163.com/components/onekey
 
但是有些站点提供的api不是很好用喔。大概的简化一下。部分可以实现 标题,内容,图片,连接的分享。
var title = encodeURIComponent("【翻版东风破的百度空间】");
var url = encodeURIComponent(document.location);
var content = encodeURIComponent("如果害怕别人繁华背影后的陷阱,那么刻苦铭心的也不一定是爱情");
 var pic = encodeURI('http://localhost/v2008/images/index/banner.gif');
var site = "http://hi.baiud.com/fbdfp";
===========================================================================================
//获得大图URL连接,splitStr分隔符: 新浪用"||"分隔,腾讯用"|"分隔
function getImageUrls(splitStr){
	var urlStr="";
	$("input[name=pictureSrc]").each(function(){
		urlStr+=$(this).val()+splitStr;
	});
	urlStr=urlStr.substring(0,urlStr.lastIndexOf(splitStr));
	return urlStr;
}

//分享到腾讯围脖
    function postToQQ() {
        var _t = title;
        var _url = url;
        var _appkey = encodeURI('appkey');//你从腾讯获得的appkey
        //var _pic = pic;//(例如:var _pic='图片url1|图片url2|图片url3....)
        var _pic=getImageUrls("|");
        var _txt = content;
        var _site = site;//你的网站地址
        var _u = 'http://v.t.qq.com/share/share.php?url=' + _url + '&appkey='
                + _appkey + '&site=' + _site + '&pic=' + _pic + '&title=' + _t
                + _txt;
        window.open(_u,'','width=700, height=680, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no');
    }
   //分享到开心网 
    function postToKaixin() {
        window.open('http://www.kaixin001.com/repaste/share.php?rtitle='
                + title + '&rurl=' + url + '&rcontent=' + content);
    }
//分享到网易围脖
    function postTo163() {
        var url = '&info=' +title+ content + ' ' + encodeURIComponent(document.location.href)
        +'&images='+pic;
        window.open('http://t.163.com/article/user/checkLogin.do?' + url + '&' + new Date().getTime(), 'newwindow', 'height=330,width=550,top=' + (screen.height - 280) / 2 + ',left=' + (screen.width - 550) / 2 + ', toolbar=no, menubar=no, scrollbars=no,resizable=yes,location=no, status=no');
    }
//分享到新浪围脖
    function postToSina() {
        var pic=getImageUrls("||");
        window.open('http://service.weibo.com/share/share.php?url='+url+'&appkey=&title='+title+content+'&pic='+pic+'+&ralateUid=');
        //window.open('http://v.t.sina.com.cn/share/share.php?title=' + title+ content+'&url=' + url);
    }

//分享到人人网
    function postToRenRen() {
        window.open('http://share.renren.com/share/buttonshare.do?link='+url+'&title='+title);
    }
//分享到搜狐围脖
    function postToSoho() {
        window.open('http://t.sohu.com/third/post.jsp?&title='+encodeURIComponent(title)+'&url='+encodeURIComponent(content)+location.href);
    }
 
===============================================================================
已经通过测试,mark一下。省的老是做这样的需求还要找资料。
Global site tag (gtag.js) - Google Analytics