Ajax + Redis + Mysql to implement drop-down box data loading

personal blog

Servlet writing

Get the returned json data containing the content of the drop-down box from service and send it to the client.

@WebServlet("/provinceServlet")
public class ProvinceServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.调用service查询
        ProvinceService service = new ProvinceServiceImpl();
        String json = service.findAllJson();

        System.out.println(json);
        //3.响应结果
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

Service writing

findAll () gets data from mysql, and findAllJson () gets data from redis. When the user visits the page, first check whether redis contains the required data, if there is, and return json format data, if not, query from mysql.

public class ProvinceServiceImpl implements ProvinceService {
    //声明dao
    private ProvinceDao dao = new ProvinceDaoImpl();

    @Override
    public List<Province> findAll() {
        return dao.findAll();
    }

    /**
        使用redis缓存
     */

    @Override
    public String findAllJson() {
        //1.先从redis中查询数据
        //1.1获取redis客户端连接
        Jedis jedis = JedisPoolUtils.getJedis();
        String province_json = jedis.get("province");

        //2判断 province_json 数据是否为null
        if(province_json == null || province_json.length() == 0){
            //redis中没有数据
            System.out.println("redis中没数据,查询数据库...");
            //2.1从数据中查询
            List<Province> ps = dao.findAll();
            //2.2将list序列化为json
            ObjectMapper mapper = new ObjectMapper();
            try {
                province_json = mapper.writeValueAsString(ps);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

            //2.3 将json数据存入redis
            jedis.set("province",province_json);
            //归还连接
            jedis.close();

        }else{
            System.out.println("redis中有数据,查询缓存...");
        }


        return province_json;
    }
}

JQuery writing

After loading the page, use the ajax asynchronous request method to send the request, and use the CURUD operation append method to append the child element option to the parent element to implement the loading of the drop-down box.

<script>
    $(function () {
        $.get("findProvinceServlet",{},function (data) {
            var province = $("#province");
            $(data).each(function () {
                var option = "<option name='"+this.id+"'>"+this.name+"</option>"
                province.append(option);
            })
        })
    })
</script>

Note: The subsequent addition, deletion and modification operations need to delete the corresponding data in redis and re-store it, and add the method in the corresponding function.

Published 28 original articles · praised 0 · visits 722

Guess you like

Origin blog.csdn.net/William_GJIN/article/details/105083417