Page query case (using redis database)

Time:2021-8-28

Requirements:

1. Provide an index.html page with a drop-down list of provinces
2. When the page is loaded, send an Ajax request. Load all provinces

 

Preliminary construction of preliminary project:

index.html

$(function(){
	$.get("url",{},funtion(data){
	//URL - query the servlet path, {} do not use parameters. Data is the returned data and is filled into the province list
	//Because there is a query database, there is a three-tier architecture servlet (findprovinceservlet) - Service (provinceservice) - Dao (provinciedao) to query the database
	})
})

 

ProvinceDao

1. Declare the JDBC template and query the database
2. Return the query value (list set)

 

ProvinceService

1. Declaration Dao
2. Query all result sets through Dao

 

findProvinceServlet

1. Call service to complete the query and return list
2. Return the data. Because the Ajax request is used, the data needs to be serialized to JSON
3. Response

 

Overall Code:

index.html

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


    
        ---Please select a province---

 

ProvinceDao.java

package cn.stormtides.dao.impl;

import cn.stormtides.dao.ProvinceDao;
import cn.stormtides.domain.Province;
import cn.stormtides.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class ProvinceDaoImpl implements ProvinceDao {

    //Declare member variables
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List findAll() {
        String sql="select * from province";

        List list = template.query(sql, new BeanPropertyRowMapper<>(Province.class));

        return list;
    }
}

 

ProvinceService.java

package cn.stormtides.service.impl;

import cn.stormtides.dao.ProvinceDao;
import cn.stormtides.dao.impl.ProvinceDaoImpl;
import cn.stormtides.domain.Province;
import cn.stormtides.jedis.util.JedisPoolUtils;
import cn.stormtides.service.ProvinceService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import redis.clients.jedis.Jedis;

import java.util.List;

public class ProvinceServiceImpl implements ProvinceService {
    //Declaration Dao
    private ProvinceDao dao=new ProvinceDaoImpl();
    @Override
    public List findAll() {
        return dao.findAll();
    }

    /**
     *Using redis cache
     * @return province_json
     */
    @Override
    public String findAllJson() {
        Jedis jedis= JedisPoolUtils.getJedis();
        String province_json = jedis.get("province");

        if (province_json==null || province_json.length()==0){
            System. Out. Println ("redis has no data, query the database");
            List ps=dao.findAll();
            ObjectMapper mapper=new ObjectMapper();
            try {
                province_json=mapper.writeValueAsString(ps);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jedis.set("province",province_json);
            jedis.close();
        }else {
            System.out.println ("data in redis");
        }

        return province_json;
    }
}

  

findProvinceServlet.java

package cn.stormtides.web.servlet;

import cn.stormtides.domain.Province;
import cn.stormtides.service.ProvinceService;
import cn.stormtides.service.impl.ProvinceServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

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.List;

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

        //Call service query
        ProvinceService service=new ProvinceServiceImpl();

//// use normal procedures
//        List list = service.findAll();
//// serialize the list to JSON
//        ObjectMapper mapper=new ObjectMapper();
//        String json=mapper.writeValueAsString(list);

        //Using redis cache
        String json=service.findAllJson();


        System.out.println(json);

        //Response results
        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);
    }
}

  

 

   Complete code