Java代码操作Redis
-
在 pom.xml 中下载 redis 的依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
-
连接数据库
//输入你的 ip 和 端口 Jedis jedis = new Jedis("192.168. .",6379); jedis.auth(" "); //设置的密码 System.out.println(jedis.ping());
效果:
-
操作字符串
jedis.set("ht","墙头马上遥相顾");
在客户端中查看
System.out.println(jedis.get("ht"));
服务端测试查看
-
操作哈希
jedis.hset("user","uname","黄大娘");
edis.hset("user","usex","女");
效果
在服务端查看
//得到哈希值 System.out.println(jedis.hgetAll("user")); // 方式一 System.out.println(jedis.hget("user", "uname"));
-
操作列表
jedis.lpush("shici","青灯","不归客","山有木兮","木有之","墙头","马上");
效果
服务端测试
System.out.println(jedis.lpop("shici"));//桟结构,所以是先进后出的道理
- 模拟数据存在 redis 缓存中
HuangSevlet
package com.huang; import redis.clients.jedis.Jedis; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Map; /** * @author 黄大娘 * @company dogson 有限公司 * @create 2019-09-18 15:46 */ @WebServlet("/gethuang") public class HuangServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); }@Override </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> doPost(HttpServletRequest req, HttpServletResponse resp) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ServletException, IOException { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第一次读取数据库,后从缓存中读取数据</span> Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("192.168.80.128",6379<span style="color: rgba(0, 0, 0, 1)">); jedis.auth(</span>"613613"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从缓存中读取用户</span> Map cretauser =jedis.hgetAll("cretauser"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (cretauser !=<span style="color: rgba(0, 0, 255, 1)">null</span> && cretauser.size()>0<span style="color: rgba(0, 0, 0, 1)"> ){ req.setAttribute(</span>"msg","从缓存中拿数据"<span style="color: rgba(0, 0, 0, 1)">); }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第一次登陆获取首页数据</span> req.setAttribute("msg","从数据库中拿数据"<span style="color: rgba(0, 0, 0, 1)">); String uname</span>="huangting"<span style="color: rgba(0, 0, 0, 1)">; String upas</span>="123456"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把数据库中对应的对象存到缓存中</span> jedis.hset("cretauser","uname","huangting"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"cretauser","upas","123456"<span style="color: rgba(0, 0, 0, 1)">); cretauser </span>= jedis.hgetAll("cretauser"<span style="color: rgba(0, 0, 0, 1)">); req.setAttribute(</span>"cretauser"<span style="color: rgba(0, 0, 0, 1)">,cretauser); } req.getRequestDispatcher(</span>"/index.jsp"<span style="color: rgba(0, 0, 0, 1)">).forward(req,resp); }
}
优化项目
第一次去数据库中拿数据后期在从 Redis 缓存中拿数据,减少访问数据库的次数提高性能
package com.huangting.blog.web;import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.servlet.http.HttpServletRequest;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
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.index.Term;
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.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.Parameter.Request;import com.huangting.blog.dao.BlogDao;
import com.huangting.blog.util.JsonUtils;
import com.huangting.blog.util.LuceneUtil;
import com.huangting.blog.util.PropertiesUtil;
import com.huangting.blog.util.StringUtils;import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;/**
- 专门将网页静态化的类,这里不涉及到 lucene
- @author Administrator
*/
public class FreemarkerBlogAction {
private String title;
private String bid;
private BlogDao blogDao = new BlogDao();
private static Jedis jedis;
static {
jedis =new Jedis("192.168. .",6379);
jedis.auth(" ");
jedis.select(0); //选择要操作的数据库
}
public String getTitle() {
return title;
}</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setTitle(String title) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.title =<span style="color: rgba(0, 0, 0, 1)"> title; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getBid() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> bid; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setBid(String bid) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.bid =<span style="color: rgba(0, 0, 0, 1)"> bid; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String list() { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { HttpServletRequest request </span>=<span style="color: rgba(0, 0, 0, 1)"> ServletActionContext.getRequest(); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (StringUtils.isBlank(title)) {
// 从 Redis 中获取数据
String bolgListALL =jedis.get("blogList");
if(bolgListALL != null && bolgListALL.length()>0) {
System.out.println("使用 redis 缓存查询");
request.setAttribute("blogList", JSON.parse(bolgListALL));
}else {
// 从数据库中查询数据
List<Map<String, Object>> blogList = this.blogDao.freemarker_list(title, null);
// 放入缓存
jedis.set("blogList", JSON.toJSONString(blogList));
//传到 jsp 页面
request.setAttribute("blogList", blogList);
System.out.println("从数据中拿数据");
}
} else {
SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
IndexReader indexReader = DirectoryReader
.open(FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexPath"))));
IndexSearcher searcher = new IndexSearcher(indexReader);
// 拿一句话到索引目中的索引文件中的词库进行关键词碰撞
Query query = new QueryParser("title", analyzer).parse(title);
TopDocs topDocs = searcher.search(query, 100);
// 将碰撞出来的关键词给点亮
QueryScorer queryScorer = new QueryScorer(query);
// 以什么形式点亮关键词
Formatter formatter = new SimpleHTMLFormatter("<span style='color:red;'><b>", "</span></b>");
Highlighter highlighter = new Highlighter(formatter, queryScorer);
List<Map<String, Object>> blogList = new ArrayList<>();
Map<String, Object> map = null;
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
map = new HashMap<>();
Document doc = searcher.doc(scoreDoc.doc);
map.put("bid", doc.get("bid"));
map.put("summary", doc.get("summary"));
String titleHighlighter = doc.get("title");
if (StringUtils.isNotBlank(titleHighlighter)) {
titleHighlighter = highlighter.getBestFragment(analyzer, "title", titleHighlighter);
}
map.put("title", titleHighlighter);
blogList.add(map);
}
indexReader.close();
request.setAttribute("blogList", blogList);
}
} catch (Exception e) {
e.printStackTrace();
}
return "blogList";
}</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 这是刷新全局索引调用的方法 * * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String refreshIndex() { IndexWriterConfig conf </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> IndexWriterConfig(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SmartChineseAnalyzer()); Directory d; IndexWriter indexWriter </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 先将索引库中的索引文件清空</span> delTempChild(<span style="color: rgba(0, 0, 255, 1)">new</span> File(PropertiesUtil.getValue("indexPath"<span style="color: rgba(0, 0, 0, 1)">))); d </span>= FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexPath"<span style="color: rgba(0, 0, 0, 1)">))); indexWriter </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> IndexWriter(d, conf); List</span><Map<String, Object>> list = blogDao.freemarker_list(<span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (Map<String, Object><span style="color: rgba(0, 0, 0, 1)"> map : list) { Document doc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document(); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> StringField("id", String.valueOf(map.get("bid"<span style="color: rgba(0, 0, 0, 1)">)), Field.Store.YES)); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> StringField("bid", String.valueOf(map.get("bid"<span style="color: rgba(0, 0, 0, 1)">)), Field.Store.YES)); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> TextField("title", (String) map.get("title"<span style="color: rgba(0, 0, 0, 1)">), Field.Store.YES)); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> TextField("summary", (String) map.get("summary"<span style="color: rgba(0, 0, 0, 1)">), Field.Store.YES)); indexWriter.addDocument(doc); } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (InstantiationException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IllegalAccessException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (indexWriter != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { indexWriter.close(); } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated catch block</span>
e.printStackTrace();
}
}
return "blogList";
}</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> delTempChild(File file) { </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (file.isDirectory()) { String[] children </span>= file.list();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取文件夹下所有子文件夹 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 递归删除目录中的子目录下</span> <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < children.length; i++<span style="color: rgba(0, 0, 0, 1)">) { delTempChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> File(file, children[i])); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 目录空了,进行删除</span>
file.delete();
}</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 修改前期 * * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String perEidt() { HttpServletRequest request </span>=<span style="color: rgba(0, 0, 0, 1)"> ServletActionContext.getRequest(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { Map</span><String, Object> map = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.blogDao.getBlogById(bid); request.setAttribute(</span>"map"<span style="color: rgba(0, 0, 0, 1)">, map); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span> (InstantiationException | IllegalAccessException |<span style="color: rgba(0, 0, 0, 1)"> SQLException e) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated catch block</span>
e.printStackTrace();
}
return "blogEdit";
}</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 添加博客 * * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> SQLException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> IllegalAccessException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> IllegalArgumentException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> SecurityException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> NoSuchFieldException </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String add() { HttpServletRequest request </span>=<span style="color: rgba(0, 0, 0, 1)"> ServletActionContext.getRequest(); Map parameterMap </span>=<span style="color: rgba(0, 0, 0, 1)"> request.getParameterMap(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将博客添加到数据库中 </span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.blogDao.add(parameterMap); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">清除Redis缓存</span> jedis.del("blogList"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取当前博客的id</span> <span style="color: rgba(0, 0, 255, 1)">int</span> maxId = (<span style="color: rgba(0, 0, 255, 1)">int</span>) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.blogDao.maxId(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加到lucene 索引库中</span> addIndex(maxId + ""<span style="color: rgba(0, 0, 0, 1)">, parameterMap); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将这篇博客进行网页静态化</span> addStaticPage(maxId + ""<span style="color: rgba(0, 0, 0, 1)">, parameterMap); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO: handle exception</span>
e.printStackTrace();
}
return "blogList";
}</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 这是删除博客的方法 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String del() { HttpServletRequest request </span>=<span style="color: rgba(0, 0, 0, 1)"> ServletActionContext.getRequest(); Map parameterMap </span>=<span style="color: rgba(0, 0, 0, 1)"> request.getParameterMap(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库中删除博客</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.blogDao.del(request.getParameterMap()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">清除Redis缓存</span> jedis.del("blogList"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除lucene中对应的文档</span> IndexWriter indexWriter =<span style="color: rgba(0, 0, 0, 1)"> getIndexWriter(); indexWriter.deleteDocuments(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Term("id"<span style="color: rgba(0, 0, 0, 1)">, bid)); indexWriter.forceMergeDeletes(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 强制删除</span>
indexWriter.commit();
indexWriter.close();
// 删除页面
new File("F:\java2\javaxl_lunece_freemarker\src\main\webapp\freemarker" + bid
+ ".html").delete();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "blogList";
}</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 这是修改的方法 * * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String edit() { HttpServletRequest request </span>=<span style="color: rgba(0, 0, 0, 1)"> ServletActionContext.getRequest(); Map parameterMap </span>=<span style="color: rgba(0, 0, 0, 1)"> request.getParameterMap(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改数据库中的值</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.blogDao.edit(request.getParameterMap()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">清除Redis缓存</span> jedis.del("blogList"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改lucene中的文档值</span> IndexWriter writer =<span style="color: rgba(0, 0, 0, 1)"> getIndexWriter(); Document doc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document(); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> StringField("id", JsonUtils.getParamVal(parameterMap, "bid"<span style="color: rgba(0, 0, 0, 1)">), Field.Store.YES)); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> StringField("bid", JsonUtils.getParamVal(parameterMap, "bid"<span style="color: rgba(0, 0, 0, 1)">), Field.Store.YES)); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> TextField("title", JsonUtils.getParamVal(parameterMap, "title"<span style="color: rgba(0, 0, 0, 1)">), Field.Store.YES)); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> TextField("summary", JsonUtils.getParamVal(parameterMap, "summary"<span style="color: rgba(0, 0, 0, 1)">), Field.Store.YES)); writer.updateDocument(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Term("id", JsonUtils.getParamVal(parameterMap, "bid"<span style="color: rgba(0, 0, 0, 1)">)), doc); writer.close(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改静态页(相同id会之间覆盖)</span> addStaticPage(JsonUtils.getParamVal(parameterMap, "bid"<span style="color: rgba(0, 0, 0, 1)">), parameterMap); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO: handle exception</span>
e.printStackTrace();
}
return "blogList";
}</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 获取写入对象 * * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> IOException </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> IndexWriter getIndexWriter() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException { IndexWriterConfig conf </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> IndexWriterConfig(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SmartChineseAnalyzer()); Directory d </span>= FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexPath"<span style="color: rgba(0, 0, 0, 1)">))); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> IndexWriter(d, conf); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 添加索引文件 * * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> parameterMap * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> IOException </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> addIndex(String id, Map parameterMap) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException { IndexWriter indexWriter </span>=<span style="color: rgba(0, 0, 0, 1)"> getIndexWriter(); Document doc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document(); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> StringField("id"<span style="color: rgba(0, 0, 0, 1)">, id, Field.Store.YES)); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> StringField("bid"<span style="color: rgba(0, 0, 0, 1)">, id, Field.Store.YES)); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> TextField("title", JsonUtils.getParamVal(parameterMap, "title"<span style="color: rgba(0, 0, 0, 1)">), Field.Store.YES)); doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> TextField("summary", JsonUtils.getParamVal(parameterMap, "summary"<span style="color: rgba(0, 0, 0, 1)">), Field.Store.YES)); indexWriter.addDocument(doc); indexWriter.close(); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 这是添加静态页 * * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> IOException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> TemplateException </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> addStaticPage(String id, Map parameterMap) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException, TemplateException { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1.创建配置类</span> Configuration configuration = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Configuration(Configuration.getVersion()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2.设置模板所在的目录</span>
configuration.setDirectoryForTemplateLoading(
new File("F:\java2\javaxl_lunece_freemarker\src\main\webapp\freemarker"));
// 3. 设置字符集
configuration.setDefaultEncoding("utf-8");
// 4. 加载模板 (这是在 刚刚设置好的 目录下面去找)
Template template = configuration.getTemplate("blogDetail.ftl");
Map map = new HashMap<>();
Map<String, Object> blog = new HashMap<>();
blog.put("bid", id);
blog.put("title", JsonUtils.getParamVal(parameterMap, "title"));
blog.put("releaseDate", new Date());
blog.put("btid", JsonUtils.getParamVal(parameterMap, "btid"));
blog.put("clickHit", JsonUtils.getParamVal(parameterMap, "clickHit"));
blog.put("content", JsonUtils.getParamVal(parameterMap, "content"));
map.put("blog", blog);
// // 6. 创建 Writer 对象
Writer out = new FileWriter(
new File("F:\java2\javaxl_lunece_freemarker\src\main\webapp\freemarker" +blog.get("bid")
+ ".html"));
// 7. 输出
template.process(map, out);
// 8. 关闭 Writer 对象
out.close();
}
}
运行速度快了蛮多
谢谢观看!