数据少的话 模糊查找穗基,多的吵判话,用lucene
创新互联公司是一家专业提供裕华企业网站建设,专注与成都网站设计、成都做网站、H5场景定制、小程序制作等业务。10年已为裕华众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
建立索引,从索引里面搜索。
索引搜索是这么猜碰谨做的:
package method;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.LockObtainFailedException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import pojo.user;
import Interface.dao;
public class CreateAndUse extends HibernateDaoSupport implements dao{
private static String INDEX_PATH = "D:/use/";//索引路径
public void create(){
Listuser list = getHibernateTemplate().find("from user");
try{
IndexWriter writer = new IndexWriter(CreateAndUse.INDEX_PATH, new StandardAnalyzer(),true);
for(int i=0;ilist.size();i++){
Document doc = new Document();
Field f1 = new Field("id",String.valueOf(list.get(i).getId()),Field.Store.YES,Field.Index.UN_TOKENIZED);
Field f2 = new Field("username",list.get(i).getUsername(),Field.Store.YES,Field.Index.UN_TOKENIZED);
Field f3 = new Field("password",list.get(i).getPassword(),Field.Store.YES,Field.Index.TOKENIZED);
doc.add(f1);
doc.add(f2);
doc.add(f3);
writer.addDocument(doc);
}
writer.close();
}catch(Exception e){
e.printStackTrace();
}
}
public Listuser search(String str,String type) throws CorruptIndexException, IOException, ParseException{
Listuser list = new ArrayList();
user u = null;
IndexSearcher index = new IndexSearcher(INDEX_PATH);
QueryParser q = new QueryParser(type,new StandardAnalyzer());
q.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query = q.parse(str);
Hits hits = index.search(query);
// System.out.println("hits的长度为:"+hits.length());
for(int i = 0;i hits.length();i++){
u = new user();
u.setCount(hits.length());
u.setId(Integer.parseInt(hits.doc(i).get("id")));
u.setUsername(hits.doc(i).get("username"));
u.setPassword(hits.doc(i).get("password"));
list.add(u);
}
return list;
}
}
lucene是个java的搜索库,你要自己下载配置一下这个库。
你这需求太简洁了,设定几个常量,或悄数作为升级所需的经验值。然后做一个变量,作为获衫首得的经验值,再来个变量作为当前的等级,然后在经运手验值累加的时候,进行判断是否达到升级标准,不就好了?
1、搜索java群或是程序员群;
2、上中国开源网
3、java吧