采用异步读取数据(使用gson将集合转换json数据),并使用redis缓存技术实现
1.Category类
public class Category {
private String cid;
private String cname;
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
2.CategoryDao类
public class CategoryDao {
// 查找所有类型
public List<Category> findAllCategory() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from category";
return runner.query(sql, new BeanListHandler<Category>(Category.class));
}
}
3.CategoryService类
public class CategoryService {
private CategoryDao categoryDao=new CategoryDao();
//查找所有类型
public List<Category> findAllCategory(){
try {
return categoryDao.findAllCategory();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
4.导包
jedis-2.7.0.jar
5.新建redis.properties放入src目录下
redis.maxIdle=30
redis.minIdle=10
redis.maxTotal=100
redis.url=localhost
redis.port=6379
6.新建JedisPoolUtils类
public class JedisPoolUtils {
private static JedisPool pool = null;
static{
//加载配置文件
InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
Properties pro = new Properties();
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
}
//获得池子对象
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));//最大闲置个数
poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));//最小闲置个数
poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));//最大连接数
pool = new JedisPool(poolConfig,pro.getProperty("redis.url") , Integer.parseInt(pro.get("redis.port").toString()));
}
//获得jedis资源的方法
public static Jedis getJedis(){
return pool.getResource();
}
public static void main(String[] args) {
Jedis jedis = getJedis();
System.out.println(jedis.get("xxx"));
}
}
7.启动redis-server.exe
8.CategoryListServlet类
public class CategoryListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private CategoryService categoryService = new CategoryService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//1.获取jedis对象 连接redis数据库
Jedis jedis= JedisPoolUtils.getJedis();
String categoryListJSon= jedis.get("categoryListJson");
if(categoryListJSon==null){
//准备数据
List<Category> categoryList=categoryService.findAllCategory();
//创建gson对象
Gson gson=new Gson();
//将集合转换为json对象
categoryListJSon= gson.toJson(categoryList);
//将json数据放入jedis缓存中
jedis.set("categoryListJson", categoryListJSon);
}
response.getWriter().println(categoryListJSon);
}
}
9.head.jsp文件
<script type="text/javascript">
//使用ajax异步请求后台加载商品类型
$(document).ready(function(){
$.post(
"${pageContext.request.contextPath}/categoryList",
function(data){
var content="";
for(var i=0;i<data.length;i++){
content+="<li class='active'><a href='${pageContext.request.contextPath}/product?method=findProductByCategory&cid="+data[i].cid+"'>"+data[i].cname+"</a></li>";
}
$("#categoryContent").html(content);
},
"json"
);
});
</script>