redis
redis特点:
- redis命令是nosql的,什么是nosql:
不按照标准规定的sql语句来操作数据库 - redis能把数据存在内存中(eg:将数据库中的数据存在内存中,防止再次查询数据时消耗大量的资源和时间)
数据库中的信息
uid uname
1 呵呵
2 小黄
3 小明
《--------------------------------------------------------------------》
//1.从redis中查询
//1.1获取jedis客户端
Jedis jedis = JedisUtil.getJedis();
//1.2可使用sortedset排序查询
Set<String> categorys = jedis.zrange("category", 0, -1);
System.out.println(categorys);
//2.判断查询的集合是否为空
List<Category> list = null;
if (categorys==null || categorys.size()==0){
//3.如果为空,从数据库查询,再将数据库存入redis
//3.1从数据库查询
list = categoryDao.findAll();
//3.2将集合数据存到redis中的 catefory的key
for (int i = 0; i < list.size(); i++) {
jedis.zadd("category",list.get(i).getCid(),list.get(i).getCname());
}
jedis
操作redis的一个jar包
下面的连接工具其实是为了有更好的代码体验
其实可以只需要创建
public Jedis() {
}
public Jedis(String host) {
super(host);
}
public Jedis(String host, int port) {
super(host, port);
}
public Jedis(String host, int port, int timeout) {
super(host, port, timeout);
}
public Jedis(String host, int port, int connectionTimeout, int soTimeout) {
super(host, port, connectionTimeout, soTimeout);
}
package cn.itcast.travel.util;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Jedis工具类
*/
public final class JedisUtil {
private static JedisPool jedisPool;
static {
//读取配置文件
InputStream is = JedisPool.class.getClassLoader().getResourceAsStream("jedis.properties");
//创建Properties对象
Properties pro = new Properties();
//关联文件
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//获取数据,设置到JedisPoolConfig中
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
//初始化JedisPool
jedisPool = new JedisPool(config, pro.getProperty("host"), Integer.parseInt(pro.getProperty("port")));
}
/**
* 获取连接方法
*/
public static Jedis getJedis() {
return jedisPool.getResource();
}
/**
* 关闭Jedis
*/
public static void close(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}
BaseServlet
由于Servlet写多了之后会出现代码冗余等问题,所以就可以直接通过一个路径名来调用方法,达到简化代码的效果
BaseServlet的实现其实是:反射
当CategoryServlet类 extends BaseServlet
输入的方法名会加入在路径后面,反射就可以获取这个名字,通过这个名字来调用CategoryServlet类方法
其次反射中如果用暴力反射,就要加上
method.setAccessible(true);
什么是暴力反射呢?
getDeclaredMethod
(String name,...obj.class(可以为空)
):获取该名类的所有方法
Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
package cn.itcast.travel.web.servlet;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取请求路径
String uri = req.getRequestURI();
// System.out.println("UserServlet路径:"+uri);// /travel/user/add
//2.获取方法名
String methodName = uri.substring(uri.lastIndexOf('/') + 1);//add
// System.out.println(methodName);
//3.获取方法对象Method
//谁调用我,this就是谁
// System.out.println(this);
try {
//忽略权限修饰符,获取方法对象
Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
//暴力反射
//4.执行方法
method.invoke(this,req,resp);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
package cn.itcast.travel.web.servlet;
import cn.itcast.travel.domain.Category;
import cn.itcast.travel.service.CategoryService;
import cn.itcast.travel.service.impl.CategoryServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/category/*")
//继承于BaseServlet
<------------------------------------------------------------------>
//当访问这个路径时候就会给与findAll方法名
<------------------------------------------------------------------>
//例如/category/findAll
public class CategoryServlet extends BaseServlet {
private CategoryService service = new CategoryServiceImpl();
/**
* 查询所有
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.调用service查询所有
List<Category> cs = service.findAll();
//2.序列化json返回
/* ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString("cs");*/
writerValue(cs,response);
}
}
req.getRequestURI与req.getContextType区别
//1.获取请求的URI路径
String uri = req.getRequestURI();
输出:/travel/user/add
//2.获取请求的虚拟路径
String contentPath = req.getContextType();
输出:/travel