页面查询案例(使用redis数据库)
需求:
1.提供一个index.html页面,页面中有个省份,下拉列表
2.当页面加载完成时,发送ajax请求。加载所有省份
前期项目初步搭建:
index.html
$(function(){$.get("url",{},funtion(data){
//url-查询servlet路径,{}不要参数,data为返回的数据,填充到省份列表中
//因为有查询数据库因此有三层架构service(findProvinceServlet)-service(ProvinceService)-dao(ProviniceDao)查询数据库
})
})
ProvinceDao
1.声明JDBCTemplate,查询数据库
2.返回查询的值(List集合)
ProvinceService
1.声明dao
2.通过Dao返回查询所有的结果集
findProvinceServlet
1.调用service完成查询,返回List<Province>
2.将数据返回,因为采用的是Ajax请求,因此需要将数据序列化Json
3.响应
整体代码:
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);
}
}
完整代码
以上是 页面查询案例(使用redis数据库) 的全部内容, 来源链接: utcz.com/z/533907.html