maven/springboot
pom文件写依赖
json依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>redis依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
controller层
从数据库查出放入缓存中
@RequestMapping("showAll")
public String showAll(HttpServletRequest request){
Jedis jedis = new Jedis("虚拟机端口号", 6379);
//先从缓存中查询
String str = jedis.get("selectShopping");
System.out.println(str);
//如果缓存中没有,则查询数据库
List<Goods> selectAll = null;
if(str==null){
selectAll = service.SelectAll();
for (Goods goods : selectAll) {
System.out.println(goods);
}
String jsonString = JSON.toJSONString(selectAll);
jedis.set("selectShopping", jsonString);
}
else { people = JSON.parseArray(str,Person.class); }
request.setAttribute("all", selectAll);
return "showAll";
}
先将数据放入缓存中,然后从缓存中查出来,在页面以下拉菜单的形式展示出来
@Resource
private Jedis jedis = new Jedis("192.168.134.20",6379);
@RequestMapping("toadd")
public String toadd(){
jedis.set("type","电脑");
jedis.set("type1","手机");
jedis.set("type2","水果");
return "add";
}
@RequestMapping("types")
@ResponseBody
public List<String> types(){
List<String > list = new ArrayList<String >();
String type = jedis.get("type");
String type1 =jedis.get("type1");
String type2 =jedis.get("type2");
list.add(new String(type));
list.add(new String(type1));
list.add(new String(type2));
return list;
}
页面
<script>
$(function(){
$.post("/goods/types",function (data){
$.each(data,function (index,obj) {
$("select").append("<option value="+obj+">"+obj+"</option>")
})
});
});
</script>
<div class="form-group">
<label >类别</label>
<select class="form-control" name="gtype">
<option >请选择</option>
</select>
</div>
给下拉菜单添加ajax事件
$("#s").change(function(){
var s = $(this).val();
window.location.href="${pageContext.request.contextPath }/stu/show?cid="+s+"";
});
(#s为select标签ID)