项目结构:
package com.redis.pojo; /** * 1.pojo对应的数据库表的实体类 * 2.Shop对应数据库shop表对应的实体类 */ public class Shop { private Integer id; private Double price; private Integer stock; public Shop(Integer id, Double price, Integer stock) { this.id = id; this.price = price; this.stock = stock; } public Shop() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Integer getStock() { return stock; } public void setStock(Integer stock) { this.stock = stock; } @Override public String toString() { return "Shop{" + "id=" + id + ", price=" + price + ", stock=" + stock + '}'; } }
package com.redis.servlet; import com.redis.utils.GsonListMapUtil; import com.redis.utils.JedisUtil; import redis.clients.jedis.Jedis; 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.sql.SQLException; import java.util.List; @WebServlet("/helloServlet") public class HelloServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); //设置对客户端请求和数据库取值时的编码 resp.setContentType("text/html;charset=utf-8");//指定服务器响应给浏览器的编码 Jedis jedis = JedisUtil.getJedis(); /** * 1.查询redis中的所有数剧 * 2.list!=null(相当于先判断有没有杯子)和list.size()>0(判断杯子里面有没有水)的区别: */ List<String> shopIdList = jedis.lrange("shopId", 0, -1); if (shopIdList != null && shopIdList.size() != 0) { for (String shopIdListString : shopIdList) { resp.getWriter().write(shopIdListString); System.out.println("redis中有数据"); } } else { String gsonString = null; try { gsonString = GsonListMapUtil.gsonString(); } catch (SQLException e) { e.printStackTrace(); } jedis.rpush("shopId", gsonString); resp.getWriter().write(gsonString); System.out.println("程序先在Mysql中查询数据然后在存放到redis中!"); } JedisUtil.closeJedis(jedis); } }
package com.redis.utils; import com.google.gson.Gson; import java.sql.*; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * 1.将数据库中的数据转换成json形式 */ public class GsonListMapUtil { public static PreparedStatement preparedStatement = null; public static Connection connection = null; public static ResultSet resultSet = null; /** * 1.1.将数据库中的数据转换成json形式 */ public static String gsonString() throws SQLException { connection = JdbcUtil.getConnection(); String sql = "select * from shop".trim(); preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); ResultSetMetaData keyName = resultSet.getMetaData();//获取键名 int columnCount = keyName.getColumnCount();//获取行的数量 ArrayList arrayList = new ArrayList<Object>();// 定义一个list,用来存放数据 while (resultSet.next()) { Map map = new HashMap(); for (int i = 1; i <= columnCount ; i++) { map.put(keyName.getColumnName(i), resultSet.getObject(i)); } arrayList.add(map); } Gson gson = new Gson(); String gsonString = gson.toJson(arrayList); JdbcUtil.close(preparedStatement,connection,resultSet); return gsonString; } }
package com.redis.utils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; public class JdbcUtil { private static DataSource dataSource; static { try { Class.forName("com.mysql.cj.jdbc.Driver"); Properties properties = new Properties(); InputStream inputStream= JdbcUtil.class.getClassLoader().getResourceAsStream("com/redis/properties/druid.properties"); properties.load(inputStream); dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } /** * 1.获取一个 Connection 对象 */ public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } /** * 1.关闭资源 */ /** * 1.关闭资源 */ public static void close(PreparedStatement preparedStatement2, Connection connection, ResultSet resultSet) throws SQLException { if (preparedStatement2 != null) { preparedStatement2.close(); } if (connection != null) { connection.close(); } if (resultSet != null) { resultSet.close(); } } }
package com.redis.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class JedisUtil { private static final JedisPool JEDIS_POOL; static { /** * 1.getResourceAsStream()从resource目录读取文件变成输入流 * 2. prop.load()将输入流中转化复制到Properties中 */ InputStream resourceAsStream = JedisUtil.class.getClassLoader().getResourceAsStream("com/redis/properties/jedis.properties"); Properties pro = new Properties(); try { pro.load(resourceAsStream); } catch (IOException e) { e.printStackTrace(); } JedisPoolConfig config =new JedisPoolConfig(); config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal"))); config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle"))); JEDIS_POOL =new JedisPool(config,pro.getProperty("host"), Integer.parseInt(pro.getProperty("port"))); } public static Jedis getJedis() { return JEDIS_POOL.getResource(); } public static void closeJedis(Jedis jedis) { if (jedis != null) { jedis.close(); } } }