如何设计Servlet容器启动时加载多个数据到内存

1.这是代码方面的设计

package com.fanli.mall.platform.freemarker;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.fanli.mall.service.ClassifyService;
import com.fanli.mall.vo.ClassifyVo;

public class ClassifySelectListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        ClassifyVo classifyVo = new ClassifyVo();
        classifyVo.setParentId(0);
        ApplicationContext ctx = WebApplicationContextUtils
                .getWebApplicationContext(servletContext);
        ClassifyService classifyService = (ClassifyService) ctx
                .getBean("classifyService");
        List<ClassifyVo> list = classifyService.findList(classifyVo);

        Map<Integer,List<ClassifyVo>> map = new HashMap<Integer,List<ClassifyVo>>();
        ClassifyVo classifyVoChild = new ClassifyVo();
        for (ClassifyVo classifyVo2 : list) {
            classifyVoChild.setParentId(classifyVo2.getId());
            classifyVoChild.setIsEnable(1);
            List<ClassifyVo> listChildren = classifyService.findList(classifyVoChild);
            for (ClassifyVo classifyVo3 : listChildren) {
                if(classifyVo3.getIsEnable()==0){
                    listChildren.remove(classifyVo3);
                }
            }
            map.put(classifyVo2.getId(), listChildren);

        }
        servletContext.setAttribute("classifyVos", map);
    }

}

2.这是数据库方面的设计:
这是数据库的设计,要想加载多的数据到内存中就必须要在一个表中

猜你喜欢

转载自blog.csdn.net/zoumin123456/article/details/47421127
今日推荐