Servlet获取redis数据输出到HTML页面

前台代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script>
        $(function () {
            //发送ajaxqingq
            $.get("provinceServlet",{},function (data) {
                //[{"id":1,"name":"北京"},{"id":2,"name":"上海"},{"id":3,"name":"广州"},{"id":4,"name":"陕西"}]
                //1.获取select
                var property=$("#province")
                //2.遍历json数组
                $(data).each(function () {
                    //3.创建<option>
                    var option="<option name=‘"+this.id+"‘>"+this.name+"</option>"
                    //4.调用select的append追加option
                    property.append(option);
                });




            });
        })
    </script>
</head>
<body>
<select id="province">
    <option>---请选择---</option>
</select>
</body>
</html>

实体类代码

package cn.jztd.domain;


public class Province {
    private  int id;
    private  String name;


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }
}
dao层接口代码
 
package cn.jztd.dao.impl;


import cn.jztd.domain.Province;


import java.util.List;


public interface ProvinceDao {
    public List<Province> findAll();
}
dao层实现接口代码
 
package cn.jztd.dao.impl;


import cn.jztd.domain.Province;
import cn.jztd.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;


import java.util.List;


public class ProvinceDaoimpl implements ProvinceDao {
    //1.声明成员变量 jdbctemplement
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Province> findAll() {
       //1.定义sql
        String sql="select * from province";
        //2.执行sql,查询赋值到实体类
        List<Province> list = template.query(sql, new BeanPropertyRowMapper<Province>(Province.class));
        return  list;
    }
}
service接口代码,实现redis缓存
 
package cn.jztd.service.impl;


import cn.jztd.domain.Province;


import java.util.List;


public interface ProcinceService {
    public List<Province> findAll();
    public String faindojosn();
}
serice实现接口代码
 
package cn.jztd.service.impl;


import cn.jztd.dao.impl.ProvinceDaoimpl;
import cn.jztd.domain.Province;
import cn.jztd.redis.util.JediPoolUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import redis.clients.jedis.Jedis;


import java.util.List;


public class ProcinceServiceimpl implements ProcinceService {
    ProvinceDaoimpl pr=new ProvinceDaoimpl();
    @Override
    public List<Province> findAll() {
        return  pr.findAll();
    }


    @Override
    public String faindojosn() {
        //连接redis
        Jedis jedis= JediPoolUtils.getJed();
        //查询procince的值
        String per=jedis.get("procince");
        if(per==null||per.length()==0)//判断该键在redis中存在吗
        {
            System.out.println("没有查询到数据");
            //不存在查询数据库
            List<Province> list = pr.findAll();
            //序列化为json
            ObjectMapper mapper=new ObjectMapper();
            try {
                per = mapper.writeValueAsString(list);
                jedis.set("procince",per);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            //把json添加到redis中


        }
        else
        {
            System.out.println("查询到数据");


        }
        return per;
    }
}

servlet代码

package cn.jztd.web.servlet;


import cn.jztd.dao.impl.ProvinceDao;
import cn.jztd.dao.impl.ProvinceDaoimpl;
import cn.jztd.domain.Province;
import cn.jztd.service.impl.ProcinceService;
import cn.jztd.service.impl.ProcinceServiceimpl;
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 {
        //1.调用查询
        ProcinceService provinceDao=new ProcinceServiceimpl();
    /*    List<Province> list = provinceDao.findAll();
        //2.序列化list为json
        ObjectMapper objectMapper=new ObjectMapper();
        String json = objectMapper.writeValueAsString(list);
        System.out.println(json);*/


        //redis缓存方式查询
        String json=provinceDao.faindojosn();
        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);
    }
}

 

Servlet获取redis数据输出到HTML页面

上一篇:Android控件之RadioGroup与RadioButton(单选控件)


下一篇:64位centos下安装python的PIL模块