需求:html
1.提供一個index.html頁面,頁面中有個省份,下拉列表
2.當頁面加載完成時,發送ajax請求。加載全部省份java
前期項目初步搭建:jquery
index.htmlgit
$(function(){ $.get("url",{},funtion(data){ //url-查詢servlet路徑,{}不要參數,data爲返回的數據,填充到省份列表中 //由於有查詢數據庫所以有三層架構service(findProvinceServlet)-service(ProvinceService)-dao(ProviniceDao)查詢數據庫 }) })
ProvinceDaogithub
1.聲明JDBCTemplate,查詢數據庫
2.返回查詢的值(List集合)web
ProvinceServiceajax
1.聲明dao
2.經過Dao返回查詢全部的結果集redis
findProvinceServletspring
1.調用service完成查詢,返回List<Province>
2.將數據返回,由於採用的是Ajax請求,所以須要將數據序列化Json
3.響應sql
總體代碼:
index.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 () { $.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>
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 { //聲明成員變量 private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<Province> findAll() { String sql="select * from province"; List<Province> 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 { //聲明dao private ProvinceDao dao=new ProvinceDaoImpl(); @Override public List<Province> findAll() { return dao.findAll(); } /** * 使用redis緩存 * @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沒數據,查詢數據庫"); List<Province> 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("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 { //調用service查詢 ProvinceService service=new ProvinceServiceImpl(); // //使用普經過程 // List<Province> list = service.findAll(); // //序列化list爲json // ObjectMapper mapper=new ObjectMapper(); // String json=mapper.writeValueAsString(list); //使用redis緩存 String json=service.findAllJson(); System.out.println(json); //響應結果 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); } }