今天學了Jedis的相關內容,而後作了一個案例,可是出現了錯誤,而後我百度了一夜沒有解決,想到看看發個博客能不能有大佬幫我看一下問題出如今哪裏,百度了一夜有點懵逼。求大佬幫我解決,在這小弟我先萬分感謝大佬們點進來幫我看,感謝大佬們~html
下面是案例需求:java
案例需求:
1. 提供index.html頁面,頁面中有一個省份 下拉列表
2. 當 頁面加載完成後 發送ajax請求,加載全部省份mysql
出現的錯誤:爲啥不同的瀏覽器會出現不同的錯誤,我都被搞懵了,這是要學到放棄啊~可是我不會放棄的,求大佬幫我看看哪裏出問題了🙏🙏jquery
chrome瀏覽器出現的錯誤web
Microsoft edge瀏覽器出現的錯誤ajax
下面是個人代碼redis
1 package cn.local.domain; 2
3 public class Province { 4 private int id; 5 private String name; 6
7 public Province() { 8 } 9
10 public Province(int id, String name) { 11 this.id = id; 12 this.name = name; 13 } 14
15 public int getId() { 16 return id; 17 } 18
19 public void setId(int id) { 20 this.id = id; 21 } 22
23 public String getName() { 24 return name; 25 } 26
27 public void setName(String name) { 28 this.name = name; 29 } 30
31 @Override 32 public String toString() { 33 return "Province{" +
34 "id=" + id +
35 ", name='" + name + '\'' +
36 '}'; 37 } 38 }
數據庫類:spring
1 package cn.local.dao; 2
3 import cn.local.domain.Province; 4
5 import java.util.List; 6
7 public interface ProvinceDao { 8 public List<Province> findAll(); 9
10 }
1 package cn.local.dao.impl; 2
3 import cn.local.dao.ProvinceDao; 4 import cn.local.domain.Province; 5 import cn.local.util.JDBCUtils; 6 import org.springframework.jdbc.core.BeanPropertyRowMapper; 7 import org.springframework.jdbc.core.JdbcTemplate; 8
9 import java.util.List; 10
11 public class ProvinceDaoImpl implements ProvinceDao { 12 //1.聲明成員變量 jdbctemplement
13 private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); 14
15 @Override 16 public List<Province> findAll() { 17 //1.定義sql
18 String sql = "select * from province "; 19 //2.執行sql
20 List<Province> list = template.query(sql, new BeanPropertyRowMapper<Province>(Province.class)); 21 return list; 22 } 23 }
JDBC工具類sql
1 package cn.local.util; 2
3 import com.alibaba.druid.pool.DruidDataSourceFactory; 4
5 import javax.sql.DataSource; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.sql.Connection; 9 import java.sql.SQLException; 10 import java.util.Properties; 11
12 public class JDBCUtils { 13 private static DataSource ds ; 14
15 static { 16
17 try { 18 //1.加載配置文件
19 Properties pro = new Properties(); 20 //使用ClassLoader加載配置文件,獲取字節輸入流
21 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); 22 pro.load(is); 23
24 //2.初始化鏈接池對象
25 ds = DruidDataSourceFactory.createDataSource(pro); 26
27 } catch (IOException e) { 28 e.printStackTrace(); 29 } catch (Exception e) { 30 e.printStackTrace(); 31 } 32 } 33
34 /**
35 * 獲取鏈接池對象 36 */
37 public static DataSource getDataSource(){ 38 return ds; 39 } 40
41
42 /**
43 * 獲取鏈接Connection對象 44 */
45 public static Connection getConnection() throws SQLException { 46 return ds.getConnection(); 47 } 48 }
Jedis工具類chrome
1 package cn.local.jedis; 2
3 import redis.clients.jedis.Jedis; 4 import redis.clients.jedis.JedisPool; 5 import redis.clients.jedis.JedisPoolConfig; 6
7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.util.Properties; 10
11 public class JedisPoolUtils { 12 private static JedisPool jedisPool; 13
14 static{ 15 //讀取配置文件
16 InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties"); 17 //建立Properties對象
18 Properties pro = new Properties(); 19 //關聯文件
20 try { 21 pro.load(is); 22 } catch (IOException e) { 23 e.printStackTrace(); 24 } 25 //獲取數據,設置到JedisPoolConfig中
26 JedisPoolConfig config = new JedisPoolConfig(); 27 config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal"))); 28 config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle"))); 29
30 //初始化JedisPool
31 jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port"))); 32
33
34
35 } 36
37
38 /**
39 * 獲取鏈接方法 40 */
41 public static Jedis getJedis(){ 42 return jedisPool.getResource(); 43 } 44 }
service類
1 package cn.local.service; 2
3 import cn.local.domain.Province; 4
5 import java.util.List; 6
7 public interface ProvinceService { 8 public List<Province> findAll(); 9
10 public String findAllJson(); 11 }
1 package cn.local.service.impl; 2
3 import cn.local.dao.ProvinceDao; 4 import cn.local.dao.impl.ProvinceDaoImpl; 5 import cn.local.domain.Province; 6 import cn.local.jedis.JedisPoolUtils; 7 import cn.local.service.ProvinceService; 8 import com.fasterxml.jackson.core.JsonProcessingException; 9 import com.fasterxml.jackson.databind.ObjectMapper; 10 import redis.clients.jedis.Jedis; 11
12
13 import java.util.List; 14
15 public class ProvinceServiceImpl implements ProvinceService { 16 //聲明dao
17 private ProvinceDao dao = new ProvinceDaoImpl(); 18
19 @Override 20 public List<Province> findAll() { 21 return dao.findAll(); 22 } 23
24 /**
25 使用redis緩存 26 */
27
28 @Override 29 public String findAllJson() { 30 //1.先從redis中查詢數據 31 //1.1獲取redis客戶端鏈接
32 Jedis jedis = JedisPoolUtils.getJedis(); 33 String province_json = jedis.get("province"); 34
35 //2判斷 province_json 數據是否爲null
36 if(province_json == null || province_json.length() == 0){ 37 //redis中沒有數據
38 System.out.println("redis中沒數據,查詢數據庫..."); 39 //2.1從數據中查詢
40 List<Province> ps = dao.findAll(); 41 //2.2將list序列化爲json
42 ObjectMapper mapper = new ObjectMapper(); 43 try { 44 province_json = mapper.writeValueAsString(ps); 45 } catch (JsonProcessingException e) { 46 e.printStackTrace(); 47 } 48
49 //2.3 將json數據存入redis
50 jedis.set("province",province_json); 51 //歸還鏈接
52 jedis.close(); 53
54 }else{ 55 System.out.println("redis中有數據,查詢緩存..."); 56 } 57
58
59 return province_json; 60 } 61 }
servlet類
1 package cn.local.web.servlet; 2
3
4 import cn.local.service.ProvinceService; 5 import cn.local.service.impl.ProvinceServiceImpl; 6
7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import java.io.IOException; 13
14 @WebServlet("/provinceServlet") 15 public class ProvinceServlet extends HttpServlet { 16 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 17 /* //1.調用service查詢 18 ProvinceService service = new ProvinceServiceImpl(); 19 List<Province> list = service.findAll(); 20 //2.序列化list爲json 21 ObjectMapper mapper = new ObjectMapper(); 22 String json = mapper.writeValueAsString(list);*/
23
24 //1.調用service查詢
25 ProvinceService service = new ProvinceServiceImpl(); 26 String json = service.findAllJson(); 27
28
29 System.out.println(json); 30 //3.響應結果
31 response.setContentType("application/json;charset=utf-8"); 32 response.getWriter().write(json); 33
34 } 35
36 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 37 this.doPost(request, response); 38 } 39 }
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 () { //發送ajax請求,加載全部省份數據
$.get("provinceServlet",{},function (data) { //[{"id":1,"name":"北京"},{"id":2,"name":"上海"},{"id":3,"name":"廣州"},{"id":4,"name":"陝西"}]
//1.獲取select
var province = $("#province"); //2.遍歷json數組
$(data).each(function () { //3.建立<option>
var option = "<option name='"+this.id+"'>"+this.name+"</option>"; //4.調用select的append追加option
province.append(option); }); }); }); </script>
</head>
<body>
<select id="province">
<option>--請選擇省份--</option>
</select>
</body>
</html>
配置文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///jedis
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
host=localhost
port=6379
maxTotal=50
maxIdle=10