微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jedis案例

案例:

  • 案例需求:
ProvinceDao
package com.ailyt.dao;

import com.ailyt.pojo.Province;

import java.util.List;

public interface ProvinceDao {
    public List<Province> findAll();
}

ProvinceDaoImpl
package com.ailyt.dao.impl;

import com.ailyt.dao.ProvinceDao;
import com.ailyt.pojo.Province;
import com.ailyt.utils.JDBCUtils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.stat.JdbcDataSourceStat;
import lombok.SneakyThrows;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.InputStream;
import java.util.List;
import java.util.Properties;

public class ProvinceDaoImpl implements ProvinceDao {

    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
    @SneakyThrows
    @Override
    public List<Province> findAll() {
        String sql = "select * from province";
        List<Province> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Province>(Province.class));
        return query;
    }
}

Province
package com.ailyt.pojo;

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

    @Override
    public String toString() {
        return "Province{" +
                "id=" + id +
                ", name='" + 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;
    }

    public Province() {
    }

    public Province(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

ProvinceService
package com.ailyt.service;

import com.ailyt.pojo.Province;

import java.util.List;

public interface ProvinceService {
    public List<Province> findAll();

}

ProvinceServiceImpl
package com.ailyt.service.impl;

import com.ailyt.dao.impl.ProvinceDaoImpl;
import com.ailyt.pojo.Province;
import com.ailyt.service.ProvinceService;

import java.util.List;

public class ProvinceServiceImpl implements ProvinceService {
    private ProvinceDaoImpl provinceDao = new ProvinceDaoImpl();

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

ProvinceServlet
package com.ailyt.servlet;

import com.ailyt.pojo.Province;
import com.ailyt.service.impl.ProvinceServiceImpl;
import com.ailyt.utils.JedisPoolUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import redis.clients.jedis.Jedis;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "provinceServlet", value = "/provinceServlet")
public class ProvinceServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws servletexception, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws servletexception, IOException {
        ProvinceServiceImpl servlet = new ProvinceServiceImpl();
        List<Province> list = servlet.findAll();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsstring(list);

        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);


    }
}

JDBCUtils
package com.ailyt.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.sqlException;
import java.util.Properties;

/**
 * JDBC工具类 使用Durid连接池
 */
public class JDBCUtils {

    private static DataSource ds ;

    static {

        try {
            //1.加载配置文件
            Properties pro = new Properties();
            //使用ClassLoader加载配置文件获取字节输入流
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsstream("druid.properties");
            pro.load(is);

            //2.初始化连接池对象
            ds = DruidDataSourceFactory.createDataSource(pro);

        } catch (IOException e) {
            e.printstacktrace();
        } catch (Exception e) {
            e.printstacktrace();
        }
    }

    /**
     * 获取连接池对象
     */
    public static DataSource getDataSource(){
        return ds;
    }


    /**
     * 获取连接Connection对象
     */
    public static Connection getConnection() throws sqlException {
        return  ds.getConnection();
    }
}

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <title>Title</title>
  <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(function () {
            //发送ajax请求,加载所有省份数据
            $.get("provinceServlet",{}, function (data) {
                var province = $("#province");
                $(data).each(function () {
                    var option = "<option name='"+this.id+"'>"+this.name+"</option>";
                    province.append(option);
                });

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

    </select>
</body>
</html>

案例redis缓存优化

 @SneakyThrows
    @Override
    public String findAllJson() {

        Jedis jedis = JedisPoolUtils.getJedis();
        String province_json = jedis.get("province");

        final boolean condition = province_json == null || province_json.length() ==0;

        if (condition) {
            System.out.println("redis中没有数据从数据库查询");
            List<Province> ps = provinceDao.findAll();
            ObjectMapper mapper = new ObjectMapper();
            province_json = mapper.writeValueAsstring(ps);
            jedis.set("province", province_json);

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

        jedis.close();

        return province_json;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐