需求:
提供一个index.html页面,页面中有个省份,下拉列表
2.当页面加载完成时,发送ajax请求。加载所有省份
前期项目初步搭建:
index.html
$(function(){
$.get("url",{},funtion(data){
//url-查询servlet路径,{}不要参数,data为返回的数据,填充到省份列表中
//因为有查询数据库因此有三层架构service(findProvinceServlet)-service(ProvinceService)-dao(ProviniceDao)查询数据库
})
})
ProvinceDao
声明JDBCTemplate,查询数据库
2.返回查询的值(List集合)
ProvinceService
声明dao
2.通过Dao返回查询所有的结果集
findProvinceServlet
调用service完成查询,返回List
2.将数据返回,因为采用的是Ajax请求,因此需要将数据序列化Json
3.响应
整体代码:
index.html
Title
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 findAll() {
String sql="select * from province";
List 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 findAll() {
return dao.findAll();
}
@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 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 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);
}
}
完整代码